#!/bin/sh
#
# Copy photos from camera to Yvonne's photo directory.
#
# $Id: getphotos,v 1.7 2010/09/06 02:11:30 grog Exp $
#
# Set this to echo for testing
ECHO=
CAMERADIRS="/camera/DCIM/[1-9]* /camera/dcim/[1-9]*"
echo Copying images from $CAMERADIRS
PHOTOS=/home/yvonne/Photos
logger getphotos started
for CAMERADIR in $CAMERADIRS; do
  logger Trying $CAMERADIR;
  if [ -d $CAMERADIR ]; then
    cd $CAMERADIR
    for i in *; do
       # Kludge: there are three "Date and Time" lines, but only the first
       # is followed by multiple spaces.
       DATE=`exif $i | grep 'Date and Time  ' | sed 's:.*|::; s/://g; s: .*::'`
       # Time in format for touch(1)
       TIME=`exif $i |grep 'Date and Time  ' | sed 's:.*|[^ ]* ::; s/://g; s:\(....\):\1.:'`
       if [ "$DATE" = "" ]; then   # probably not JPEG
         DATE=`ls -lD "%Y%m%d" $i | awk '{print $6}'`
         TIME=
       fi
       DEST=$PHOTOS/$DATE/$i
       if [ ! -e $DEST ]; then 
         $ECHO mkdir -p $PHOTOS/$DATE
         $ECHO chown yvonne $PHOTOS/$DATE
         echo cp -p $i $PHOTOS/$DATE
         $ECHO cp -p $i $PHOTOS/$DATE
         if [ "$TIME" = "" ]; then
           # This should be done properly
           $ECHO touch -A -100000 $DEST
         else
           $ECHO touch -t $DATE$TIME $DEST
         fi
         $ECHO chmod 644 $DEST
         $ECHO chown yvonne $DEST
       fi
    done
  fi
done
cd
echo
echo All new files copied.
invalid=YES
while [ "$invalid" = "YES" ]; do
  echo Enter 'd' to delete all files on the camera and then unmount it.
  echo Enter 'u' to unmount the camera to turn it off.
  echo -n 'Your choice, brave woman -> '
  read response
  if [ "$response" = "d" ]; then
    for d in $CAMERADIRS; do
      $ECHO rm -f $d/*
    done
    echo All files deleted.
    $ECHO umount /camera
    echo
    echo Camera unmounted.  You may now turn it off or remove the card reader.
    invalid=
  else if [ "$response" = "u" ]; then
      $ECHO umount /camera
      echo
      echo Camera unmounted.  You may now turn it off or remove the card reader.
      invalid=
    else 
        echo Please enter 'd' or 'u', not \'$response\'
    fi
  fi
done
sleep 10


