#!/usr/athena/bin/perl -w

eval 'exec /usr/athena/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell

use strict;
use vars qw($dbh);
use DBI;
use Carp;
use Thokbook;
use Getopt::Declare;


$dbh = Thokbook::connect_db;

my $spec = q(
      [strict]
      init_db					Initialize the database
		{Thokbook::init_db($::dbh);}
      add_asin <code>				Add a ASIN record 
	       {Thokbook::add_asin($::dbh, $code);}
      add_item <code> <shelf>			Add a item to the database
	       {my $copy =1;
	      Thokbook::add_item($dbh, $code, $shelf, $copy) &&Thokbook::add_asin($dbh, $code);}
      add_items <filename>		  	Add a list of items from a file
		{add_items($filename);}
      add_comment <code> <comment>		Add a comment about a item
		  {Thokbook::add_comment($::dbh, $code, $comment);}
      check_in <code>				Check in a item
	       {my $copy = 1;
		Thokbook::check_in($::dbh, $code, $copy);}
      check_out <code> <who>			Check out a item
		{my $copy = 1; my $date = time();    
		  Thokbook::check_out($::dbh, $code, $copy, $who, $date);}
      del_item <code>				Delete a item
	       {Thokbook::del_item($::dbh, $code);}
      del_comment <code>			Delete the comment from a item
		  {Thokbook::del_comment($::dbh, $code);}
      find_author <author>			Find an author
		  {my @results = Thokbook::find_author($::dbh, $author);
		      for my $asin (@results) {
		      	  Thokbook::print_info($::dbh, @$asin[0]);}}
      find_publisher <publisher>		Find a publisher
		  {my @results = Thokbook::find_publisher($::dbh, $publisher);
		      for my $asin (@results) {
		      	  Thokbook::print_info($::dbh, @$asin[0]);}}
      find_release <release>		        Find by release date
		  {my @results = Thokbook::find_release($::dbh, $release);
		      for my $asin (@results) {
		      	  Thokbook::print_info($::dbh, @$asin[0]);}}
      find_shelf <shelf>			Find all the items on a shelf
		  {my @results = Thokbook::find_shelf($::dbh, $shelf);
		      for my $item (@results) {
		      	  Thokbook::print_info($::dbh, @$item[0]);}}
      find_title <title>			Find a title
		 {my @results = Thokbook::find_title($::dbh, $title);
		     for my $asin (@results) {
		     	Thokbook::print_info($::dbh, @$asin[0]);}}
      list_items				List all the items 
		{Thokbook::list_items($::dbh);}
      list_shelves				List all the shelves
	        {my %results = Thokbook::list_shelf($::dbh);
		 while ((my $k, my $v) = each %results) {
		     print "$k has $v items\n";}}
      move_item <code> <shelf>			Move the current location
		{my $copy = 1; Thokbook::move_item($::dbh, $code, $copy, $shelf);}
      move_shelf <code> <shelf>			Move the permanent shelf
		 { my $copy = 1;Thokbook::move_shelf($::dbh, $code, $copy, $shelf);}
      move_all <oldshelf> <newshelf>		Move all the items on a shelf
		 {Thokbook::move_all($::dbh, $oldshelf, $newshelf);}
      print_info <code>				Print the information on a item
		 {Thokbook::print_info($::dbh, $code);}
      print_isbn <isbn>			        Print the barcode for an ISBN
		    {print_isbn($isbn);}
      print_upc <upc>			        Print the barcode for an UPC
		    {print_upc($upc);}
      export_palm				Export to a Palm ListDB
		    {Thokbook::export_palm($::dbh);}
      destroy_db				Destroy the contents of the db
		{Thokbook::destroy_db($::dbh);}
      check_isbn <isbn>				[undocumented]
		 {$isbn = Thokbook::check_isbn($isbn);print "ISBN is $isbn\n";}
      check_upc <upc>				[undocumented]
		 {$upc = Thokbook::check_upc($upc);print "UPC is $upc\n";}
      del_asin <code>				[undocumented]
	       {Thokbook::del_asin($::dbh, $code);}
      list_tables				[undocumented] 
		 {Thokbook::list_tables($::dbh);}
      print_asin <code>				[undocumented]
		 {my @asin = Thokbook::find_asin($::dbh, $code);Thokbook::print_asin(@asin);}
      print_item <code>				[undocumented]
		 {my @item = Thokbook::find_item($::dbh, $code);Thokbook::print_item(@item);}
	);



my $args = new Getopt::Declare($spec);





sub add_items($) {
    my ($file) = @_;

    open INPUT, $file or croak "Cannot open input file $file: $!";
    
    while (<INPUT>) {
	chomp;
	my @line = split /,/;
	my $code = $line[0];		#substr($line[0], 1, -1);
	my $shelf = $line[1];	     #substr($line[2], 1, -2);

	my $copy = 1;
	
      Thokbook::add_item($dbh, $code, $shelf, $copy)&&Thokbook::add_asin($dbh, $code);
    }
}





sub print_isbn($) {
    my ($isbn) = @_;

    open(PICFILE, ">$isbn.png")|| croak "Could not open file $isbn.png: $!";

    my $pic = Thokbook::print_isbn($isbn);

    print PICFILE $pic;
    close(PICFILE);

}


sub print_upc($) {
    my ($upc) = @_;

    open(PICFILE, ">$upc.png")|| croak "Could not open file $upc.png: $!";

    my $pic = Thokbook::print_upc($upc);

    print PICFILE $pic;
    close(PICFILE);

}

# disconnect from the database

$dbh->disconnect;
