program only tag requested on command-line
[Biblio-RFID.git] / scripts / program.pl
index 755a5ea..565bb2a 100755 (executable)
@@ -10,9 +10,10 @@ use Biblio::RFID::Reader;
 use Biblio::RFID::RFID501;
 
 my $reader;
-my $afi;
+my $afi = 0x00;
 my $debug = 0;
 my $hash;
+my $blank;
 
 GetOptions(
        'reader=s', => \$reader,
@@ -23,21 +24,36 @@ GetOptions(
        'type=i',       => \$hash->{type},
        'branch=i',     => \$hash->{branch},
        'library=i'     => \$hash->{library},
+       '3mblank'       => \$blank->{blank_3m},
+       'blank'         => \$blank->{blank},
 ) || die $!;
 
 my ( $sid, $content ) =  @ARGV;
-die "usage: $0 [--reader regex_filter] [--afi 214] [--type 1] E0_RFID_SID [barcode]\n" unless $sid && ( $content | $afi );
+if ( $sid =~ m/.+,.+/ && ! defined $content ) {
+       ( $sid, $content ) = split(/,/, $sid);
+}
+
+die "usage: $0 [--reader regex_filter] [--afi 214] [--type 1] E0_RFID_SID [barcode]\n" unless $sid && ( $content || $afi || $blank );
 
 $hash->{content} = $content if defined $content;
 
 my $rfid = Biblio::RFID::Reader->new( $reader );
 $Biblio::RFID::debug = $debug;
 
-foreach my $tag ( $rfid->tags ) {
+foreach my $tag ( $rfid->tags, $sid ) {
        warn "visible $tag\n";
        next unless $tag eq $sid;
-       warn "PROGRAM $tag with $content\n";
-       $rfid->write_blocks( $tag => Biblio::RFID::RFID501->from_hash($hash) );
-       $rfid->write_afi(    $tag => chr($afi) ) if $afi;
+       if ( grep { defined $_ } values %$blank ) {
+               my $type = ( grep { $blank->{$_} } keys %$blank )[0];
+               warn "BLANK $type $tag\n";
+               $rfid->write_blocks( $tag => Biblio::RFID::RFID501->$type );
+       } elsif ( $content ) {
+               warn "PROGRAM $tag with $content\n";
+               $rfid->write_blocks( $tag => Biblio::RFID::RFID501->from_hash($hash) );
+       }
+       if ( $afi ) {
+               warn "AFI $tag with $afi\n";
+               $rfid->write_afi( $tag => chr($afi) );
+       }
 }