| 1 | package ClarID::Tools::Command::qrcode; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use feature 'say'; | ||
| 5 | use Moo; | ||
| 6 | use MooX::Options | ||
| 7 | auto_help => 1, | ||
| 8 | auto_version => 1, | ||
| 9 | usage => 'USAGE', | ||
| 10 | config_from_hash => {}; | ||
| 11 | use Types::Standard qw(Enum Int Str); | ||
| 12 | use Text::CSV_XS; | ||
| 13 | use File::Path qw(make_path); | ||
| 14 | use Carp qw(croak); | ||
| 15 | # Tell App::Cmd this is a command | ||
| 16 | use App::Cmd::Setup -command; | ||
| 17 | use namespace::autoclean; | ||
| 18 | |||
| 19 | # CLI options | ||
| 20 | # NB: Invalid parameter values (e.g., --format=foo) trigger App::Cmd usage/help | ||
| 21 | # This hides the detailed Types::Standard error | ||
| 22 | # Fix by overriding usage_error/options_usage | ||
| 23 | option 'action' => ( | ||
| 24 | is => 'ro', | ||
| 25 | format => 's', | ||
| 26 | isa => Enum[qw/encode decode/], | ||
| 27 | required => 1, | ||
| 28 | doc => 'encode | decode', | ||
| 29 | ); | ||
| 30 | |||
| 31 | option 'input' => ( | ||
| 32 | is => 'ro', | ||
| 33 | format => 's', | ||
| 34 | isa => Str, | ||
| 35 | required => 1, | ||
| 36 | doc => 'CSV file (encode) or directory of PNGs, or single PNG file (decode)', | ||
| 37 | ); | ||
| 38 | |||
| 39 | option 'column' => ( | ||
| 40 | is => 'ro', | ||
| 41 | format => 's', | ||
| 42 | isa => Str, | ||
| 43 | doc => 'Column name to read (defaults to clar_id or stub_id, encode only)', | ||
| 44 | ); | ||
| 45 | |||
| 46 | option 'outdir' => ( | ||
| 47 | is => 'ro', | ||
| 48 | format => 's', | ||
| 49 | isa => Str, | ||
| 50 | default => sub { 'qrcodes' }, | ||
| 51 | doc => 'Where to write PNGs (encode only)', | ||
| 52 | ); | ||
| 53 | |||
| 54 | option 'outfile' => ( | ||
| 55 | is => 'ro', | ||
| 56 | format => 's', | ||
| 57 | isa => Str, | ||
| 58 | default => sub { 'decoded.csv' }, | ||
| 59 | doc => 'Where to write CSV (decode directory mode only)', | ||
| 60 | ); | ||
| 61 | |||
| 62 | option 'sep' => ( | ||
| 63 | is => 'ro', | ||
| 64 | format => 's', | ||
| 65 | isa => Str, | ||
| 66 | default => sub { ',' }, | ||
| 67 | doc => 'CSV separator', | ||
| 68 | ); | ||
| 69 | |||
| 70 | option 'size' => ( | ||
| 71 | is => 'ro', | ||
| 72 | format => 'i', | ||
| 73 | isa => Int, | ||
| 74 | default => sub { 3 }, | ||
| 75 | doc => 'module size for qrencode (-s flag)', | ||
| 76 | ); | ||
| 77 | |||
| 78 | sub execute { | ||
| 79 | my $self = shift; | ||
| 80 | if ($self->action eq 'encode') { | ||
| 81 | $self->_run_encode; | ||
| 82 | } else { | ||
| 83 | $self->_run_decode; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _run_encode { | ||
| 88 | my $self = shift; | ||
| 89 | |||
| 90 | # Check for qrencode | ||
| 91 | system("which qrencode >/dev/null 2>&1") == 0 | ||
| 92 | or croak "ERROR: 'qrencode' not found in PATH"; | ||
| 93 | |||
| 94 | # Prepare output directory | ||
| 95 | unless (-d $self->outdir) { | ||
| 96 | make_path($self->outdir) | ||
| 97 | or croak "ERROR: cannot create directory '$self->{outdir}'"; | ||
| 98 | } | ||
| 99 | |||
| 100 | # Open CSV file | ||
| 101 | my $csv = Text::CSV_XS->new({ sep_char => $self->sep, binary => 1, auto_diag => 1 }); | ||
| 102 | open my $fh, '<', $self->input | ||
| 103 | or croak "ERROR: Cannot open '$self->input': $!"; | ||
| 104 | |||
| 105 | # Read header | ||
| 106 | my $hdr_ref = $csv->getline($fh) | ||
| 107 | or croak "ERROR: CSV is empty"; | ||
| 108 | my @hdr = @$hdr_ref; | ||
| 109 | my %idx = map { $hdr[$_] => $_ } 0..$#hdr; | ||
| 110 | |||
| 111 | # Determine column | ||
| 112 | my $col = $self->column | ||
| 113 | || (exists $idx{clar_id} ? 'clar_id' | ||
| 114 | : exists $idx{stub_id} ? 'stub_id' | ||
| 115 | : croak "ERROR: No --column and neither clar_id nor stub_id in header"); | ||
| 116 | croak "ERROR: Column '$col' not found" unless exists $idx{$col}; | ||
| 117 | |||
| 118 | say "Encoding column '$col' into PNG files in directory '$self->{outdir}'"; | ||
| 119 | |||
| 120 | # Process rows | ||
| 121 | while (my $row = $csv->getline($fh)) { | ||
| 122 | my $val = $row->[$idx{$col}] // ''; | ||
| 123 | next unless length $val; | ||
| 124 | (my $safe = $val) =~ s/[^A-Za-z0-9_-]/_/g; | ||
| 125 | my $png = "$self->{outdir}/$safe.png"; | ||
| 126 | system("qrencode", "-s", $self->size, "-o", $png, $val) == 0 | ||
| 127 | or warn "WARNING: qrencode failed for value '$val'" and next; | ||
| 128 | say " - $val -> $png"; | ||
| 129 | } | ||
| 130 | close $fh; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub _run_decode { | ||
| 134 | my $self = shift; | ||
| 135 | |||
| 136 | # Check for zbarimg | ||
| 137 | system("which zbarimg >/dev/null 2>&1") == 0 | ||
| 138 | or croak "ERROR: 'zbarimg' not found in PATH"; | ||
| 139 | |||
| 140 | # Single-file decode mode | ||
| 141 | if (-f $self->input && $self->input =~ /\.png$/i) { | ||
| 142 | my $file = $self->input; | ||
| 143 | chomp(my $decoded = qx(zbarimg --raw "$file" 2>/dev/null)); | ||
| 144 | die "ERROR: no QR code found in '$file'\n" unless length $decoded; | ||
| 145 | say $decoded; | ||
| 146 | return 1; | ||
| 147 | } | ||
| 148 | |||
| 149 | # Directory decode mode | ||
| 150 | my $col_name = $self->column || 'clar_id'; | ||
| 151 | opendir my $dh, $self->input | ||
| 152 | or croak "ERROR: Cannot open directory '$self->input': $!"; | ||
| 153 | my @files = grep { /\.png$/i } readdir $dh; | ||
| 154 | closedir $dh; | ||
| 155 | |||
| 156 | open my $out, '>', $self->outfile | ||
| 157 | or croak "ERROR: Cannot write to '$self->outfile': $!"; | ||
| 158 | print $out "$col_name\n"; | ||
| 159 | |||
| 160 | for my $f (@files) { | ||
| 161 | my $path = "$self->{input}/$f"; | ||
| 162 | chomp(my $decoded = qx(zbarimg --raw "$path" 2>/dev/null)); | ||
| 163 | next unless length $decoded; | ||
| 164 | $decoded =~ s/\r?\n//g; | ||
| 165 | print $out "$decoded\n"; | ||
| 166 | } | ||
| 167 | close $out; | ||
| 168 | say "Decoded CSV written to '$self->{outfile}'"; | ||
| 169 | } | ||
| 170 | |||
| 171 | 1; |