#!/usr/bin/perl
# grab, jim akens 2000.04.17

$version = 1.0;		# initial version, no comand line options
$version = "1.1, jim akens";		# added comand line options	
$version = "1.2, jim akens";		# added recursive copy     	
$version = "1.21, jim akens";		# minor details            	

#$Debug = 1;

# grab, a program to grab images from a smartmedia card in a pcmcia adaptor.
# In it's simplest form, grab is called with no arguments.  It mounts 
# (if required) the first pcmcia ide drive.  It then copies all files to a
# new directory off the target directory, and calls thumb, which makes an
# html index page and the required thumbnails.  These programs are intended
# to make it easy to bring camera pictures directly into a web server.

# $dev and $target must be set beforehand, and an entry for $dev must 
# exist in /etc/fstab.  $dev should be set to the pcmcia device.  This will be 
# the first device after the last permanent ide device.

$dev = "/dev/hde1";	# last permanent drive is /dev/hdd
$target = "/home/data/pictures/"; # new directory and pictures here
#$target = "/home/ftp/pub/images/319/"; # new directory and pictures here

$erase = 1; # default is to erase camera pictures


while ($#ARGV > -1)
{
 local($arg) = shift(@ARGV);

 if ($arg eq "-D")
 {
  $Debug = 1 ;
 }
 elsif ($arg eq "-d")
 {
  $target = shift(@ARGV);
  $nodate = 1;
 }
 elsif ($arg eq "-dd")
 {
  $target = shift(@ARGV);
 }
 elsif ($arg eq "-s")
 {
  $size =  shift(@ARGV);
 }
 elsif ($arg eq "-t")
 {
  $title =  shift(@ARGV);
 }
 elsif ($arg eq "-n")
 {
  $name =  shift(@ARGV);
 }
 elsif ($arg eq "-i")
 {
  $indexfile =  shift(@ARGV);
 }
 elsif ($arg eq "-e")
 {
  $erase = 0;
  print STDERR "\$erase = $erase\n" if ($Debug);
 }
 elsif ($arg eq "-v")
 {
  print "grab, $version \n";
  system "/usr/local/bin/thumb -v";
  exit;
 }
 else
 {
  print STDERR "Unkown option $arg\n";
  print STDERR "Options are:\n";
  print STDERR "-D  turns debug messages on\n";
  print STDERR "-d <new target directory> new target directory\n";
  print STDERR "-dd <new base directory> new base directory\n";
  print STDERR "-s <WWWxHHH> thumbnail size WWWxHHH pixels\n";
  print STDERR "-e don't erase images from camera disk\n";
  print STDERR "-t <new title> new title for html page\n";
  print STDERR "-i <new index file name> new index.html for html page\n";
  print STDERR "-n <new root name for files> new root name for files\n";
  exit;
 }
}
#$statusgrab = system "test` ps -auwx|grep /usr/local/bin/grab|grep -v grep|wc -l` -gt 1";
#if (!($statusgrab))
# {
#  print STDOUT "Another instance of grab is running, exiting\n";
#  sleep 5;
#  exit;
# }

if (! -d $target)
{
 if ($nodate == 0)
 {
  print STDERR "The base target directory: $target does not exist; exiting\n";
  exit;
 }
}

print STDERR "\$status will be 0 if $dev is already mounted; otherwise 256\n" if ($Debug);

$mounted = $status = system "mount|grep $dev > /dev/null";
print STDERR "\$status = $status\n" if ($Debug);

if ($status != 0) 	# not mounted
{
 print STDERR "mounting $dev\n" if ($Debug);
 system "mount $dev";
 $status = system "mount|grep $dev > /dev/null";
print STDERR "\$status will be 0 if $dev was successfully mounted; otherwise 256\n" if ($Debug);
 print STDERR "\$status = $status\n" if ($Debug);
}


if ($status != 0)	# unable to mount 
{
 print STDERR "Unable to mount $dev; exiting\n";
 print STDERR "Make sure there is an entry for $dev in /etc/fstab\n";
 exit;
}

system"mount|grep $dev >~/mnt.tmp";
open (MNT, "<$ENV{HOME}/mnt.tmp") or die "Can't write temporary file; exiting\n";
$tmp = <MNT>;
chomp $tmp;
print STDERR "\$tmp = $tmp\n" if ($Debug);
close MNT;
system "rm ~/mnt.tmp";

($junk1, $junk2, $mnt, $junk) = split / /, $tmp; # find where $dev is mounted
print STDERR "\$mnt = $mnt\n" if ($Debug);

$statusread = system "test` cd $mnt;find . -name *jpg -print|wc -w` -gt 0";

print STDERR "\$statusread = $statusread; 0 means there are pictures\n" if ($Debug);

if ($statusread == 0)
{
  $date = `date '+%Y_%m_%d' -u`;
  chomp $date;
  print STDERR "date = $date\n" if ($Debug);
 $num = 0;
 if ($nodate == 0)
 {
  $targetdir = "$target"."$date"."_"."0"."$num";
  while ( -d $targetdir)
  {
   $num += 1;
   $targetdir = "$target"."$date"."_"."0"."$num";
  }
  $mkdirstatus = system "mkdir $targetdir &> /dev/null";
  print STDERR "\$mkdirstatus = $mkdirstatus; 0 means directory creation was successful.\n" if ($Debug);
 }
 else
 {
  $targetdir = $target;
  if (!( -d $targetdir))
  {
   $mkdirstatus = system "mkdir $targetdir &> /dev/null";
   print STDERR "\$mkdirstatus = $mkdirstatus; 0 means directory creation was successful.\n" if ($Debug);
  }
 }

 if (!( -d $targetdir) )
  {
   checkmount($mounted);
   print STDOUT "Unable to create target directory, exiting\n";
   sleep 10;
   exit;
  }
 print STDERR "The (new) directory name is $targetdir\n" if ($Debug);
 $copystatus = 0; 	# assume copy successful initially;

 system "cd $mnt; find . -name *jpg -print |grep -v thm>~/pix.tmp";
 open (PIX, "<$ENV{HOME}/pix.tmp") or die "Can't write temporary file; exiting\n";

 while ($pix = <PIX>)
 {
  chomp $pix;
  print STDERR "\$mnt/\$pix = $mnt/$pix\n" if ($Debug);
  $cpstatus = system "cp -a $mnt/$pix $targetdir/." ;
  if ($cpstatus == 0)
  {
   if ($erase == 1)
   {
    $newfile = $pix;
    $newfile =~ s/\/.*\///g;   # get just the file name
    $newfile = substr $newfile, 1; # remove leading . from name
    print STDERR "New file name = $targetdir/$newfile\n";
    if (-e "$targetdir/$newfile")
    {
     system "rm $mnt/$pix";
     print STDOUT "Removing $mnt/$pix\n";
    } 
    else
    {
     print STDOUT "picture not removed from camera, copy unsuccessful\n";
    }
   }
  }
  else
  {
   $copystatus = $cpstatus;
  }
  print STDERR "\$cpstatus = $cpstatus; 0 is successful\n" if ($Debug);
 }
 close PIX;
 system "rm ~/pix.tmp";
}
else 
{
 print STDERR "No pictures to copy; exiting\n";
 checkmount($mounted);
 exit;
}

checkmount($mounted);

print "Making thumbnails; this will take several minutes.\n";
system "cd $targetdir;/usr/local/bin/thumb @ARGV";
system "cd $targetdir;cd ..;/usr/local/bin/dirindex";

sub checkmount ($mounted)
{
 # print STDERR "\$mounted = $mounted\n" if ($Debug);
 if ($mounted != 0)
 {
  print STDERR "unmounting $dev\n" if ($Debug);
  system "umount $dev";
  print "It is ok to remove the smartmedia diskette now.\n\n";
 }
 else
 {
  print STDERR "media still mounted, don't remove without unmounting\n";
 }
}
