#!/usr/bin/perl
use lib './lib';
use Device::ScanShare;
use strict;
use vars qw($VERSION $scanshare $o );
use YAML;
use Getopt::Std;
$VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)/g;


CheckEntries();
ShowEntries();






# BEGIN CHECKS

(defined $$o{a} or defined $$o{D} ) or die("Missing arguments.\n");

if ( $$o{a} and $$o{D} ){
	die("Can't tell me to add AND delete at the same time, consult manual.\n");	
}
if ( $$o{a} and ! $$o{l} ){
	die("If you want to add, you must provide -l label as well, consult manual.\n");	
}

# END CHECKS



if ($$o{a}) {

	$scanshare->user_add({path=>$$o{a}, label=>$$o{l}}) 
      or warn("Already present.\n")
      and exit;
	
	$scanshare->save;
	warn("Added. Changes Saved.\n");	
   exit;
}
elsif ($$o{D}) {

	$scanshare->user_delete($$o{D}) 
      or warn("Path $$o{D} is not an entry.\n")
      and exit;
	
	$scanshare->save;
	warn("Removed $$o{D}. Changes Saved.\n");	
   exit;
}


die( usage() );

exit;

sub ShowEntries {
   defined $o->{s} or return;

	print "\nUSERDIRS.TXT entries: \n-----------\n(label) -> (destination) -> (host)\n-----------\n";
	for (@{$scanshare->get_users}){
		print $_->{label}." -> ".$_->{path}." -> ".$_->{host}."\n";
	}
	print "\n-------------------\nTotal Entries: ".$scanshare->count."\n";
	exit;
}

sub CheckEntries {
   $o->{k} or return;

   my $bad =0;
   
	for (@{$scanshare->get_users}){
      my $win = $_->{path};
      (printf STDERR "\n%s\n# KEY: $win\n", '-'x60) if $o->{d};
      $win=~/\\/ or warn("ERROR: entry makes no sense, no backslashes '$win'\n")
         and next;
      
      my $abs = $_->{abs_unixpath} or debug("no abs_unixpath");
      
      my $ondisk = -d $abs ? 1 : 0;
      debug("ondisk '$abs'? $ondisk");

      $ondisk and next;
      $bad++;
      
      print "$win\n";      

      if ($o->{K}){
         $scanshare->user_delete($win)
            or confess("Could not remove $win entry");
         debug("removed.");         
      }
      
	}


   warn("Total no longer on disk: $bad\n");
   if ($o->{K} and $bad ){
      $scanshare->save;
      warn("Saved with $bad changes.\n");
   }
   
	exit;
}





sub usage {
	qq{$0 [OPTIONS]
Manipulate ecopy USERDIRS.TXT file for scanner management.

  -s           view all USERDIRS.TXT entries
  -v           version and exit
  -d           debug
  -h           help
  -a path      dir to add
  -l string    label for this dir to add
  -D path      delete this dir from USERDIRS.TXT
  -k           check entries, show those no longer on disk
  -K           same as -k, but if entry not on disk, remove it

See man scanshare for more information and examples.
};
}

INIT {

   $o={}; 
   getopts('hsva:dD:l:kK',$o);
   $o->{h} and print usage() and exit;
   $o->{v} and print $VERSION and exit;
   $o->{d} and $Device::ScanShare::DEBUG=1;
   
   $o->{K} and $o->{k} = 1;

   my $abs_conf = '/etc/scanshare.conf';
   -f $abs_conf or die("Missing conf '$abs_conf'\n");
   my $conf = YAML::LoadFile($abs_conf);

   $scanshare = Device::ScanShare->new($conf) 
      or die("Can't instance Device::ScanShare");
}
sub debug { $o->{d} and warn "# @_\n"; 1 }





__END__

=pod

=head1 NAME

scanshare - manipulate ecopy USERDIRS.TXT file for scanner management

=head1 DESCRIPTION

This is a cli interface to L<Device::ScanShare>.
Note that after making changes, you must

=head1 OPTIONS

  -s           view all USERDIRS.TXT entries
  -v           version and exit
  -d           debug
  -h           help
  -a path      dir to add
  -l string    label for this dir to add
  -D path      delete this dir from USERDIRS.TXT

=head1 USAGE EXAMPLES

=head2 EXAMPLE 1

imagine we want to add sfarrow to the list of people that come up in the scanner,
make sure userfiles/sfarrow/incoming  exists

        # scanshare -a userfiles/sfarrow/incoming -l "Samantha Farrow"
       
=head2 EXAMPLE 2

to delete the above entry from USERDIRS.TXT:

        # scanshare -D userfiles/sfarrow/incoming

=head2 NOTES

To add a new path, it must reside relative to where USERDIRS.TXT resides. 
if USERDIRS.TXT is in /var/this/USERDIRS.TXT, then the above entry must be in
/var/this/userfiles/sfarrow/incoming

To change this, edit /etc/scanshare.conf

=head1 /etc/scanshare.conf

	---
	userdirs_abs_path: /var/doc/USERDIRS.TXT
	default_host: Dyer04
   server: '192.168.0.145'

Default host is when you make an entry, the USERDIRS.TXT file needs it
Formatted for L<YAML>

=head1 SEE ALSO

L<Device::ScanShare> - parent package.
L<YAML>

=head1 AUTHOR

Leo Charre leocharre at cpan dot org

=head1 COPYRIGHT

Copyright (c) 2009 Leo Charre. All rights reserved.

=head1 LICENSE

This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself, i.e., under the terms of the "Artistic License" or the "GNU General Public License".

=head1 DISCLAIMER

This package is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the "GNU General Public License" for more details.

=cut


