#!/bin/sh

##################################################################
# Program: sshpics
# Purpose: Grab pictures from my Olympus C960-zoom digital camera
#          & pipe them into a remote host via ssh.
# Author : Stuart Winter <stuart@polplex.co.uk>
# Date...: 11-May-2003
##################################################################
#
#
# Please note: These are not intended to be full programs/scripts
#              but rather a quick script to get the job done.
#              If you improve these please feel free to submit
#              your new version to me!
###################################################################


SSHHOST="you@your.host.org.uk"
REMOTEDIR="PICS-INCOMING"
PHOTOPC=photopc 

# Find camera.  We'll say the machine has 4 serial ports 
# although it probably only has 2.
SERIALPORT=
echo -n "Probing for camera ... "
for ((cnt=0; cnt<=4; cnt++)); do
    photopc -ql/dev/ttyS${cnt} list >/dev/null 2>&1
    if [ $? -eq 0 ]; then
       SERIALPORT=${cnt} # we found a camera 
       echo "/dev/ttyS${cnt}"
       cnt=4 # break out of for
    fi
done
# No camera found?
if [ -z "${SERIALPORT}" ]; then
   echo "unable to find camera on any ttyS0 - ttyS4"
   exit 2
fi

total_pics="$( photopc -l/dev/ttyS${SERIALPORT} list 2>/dev/null | grep -c .JPG )"
if [ "${total_pics}" -eq 0 ]; then
   echo "No JPEGs found on the camera"
   exit 3
 else
   echo "Number of pictures: ${total_pics}"
fi

for ((cnt=1; cnt<=${total_pics}; cnt++)); do
     # make numbers sort nicely in a dir display.
     if [ "${cnt}" -le 10 ];  then cnt="0${cnt}" ; fi
     if [ "${cnt}" -le 100 ]; then cnt="0${cnt}" ; fi
     echo "Uploading picture $cnt" >&2
     if ${PHOTOPC} -qf3 \
                -l/dev/ttyS${SERIALPORT} \
                -s115200 image ${cnt} ${cnt}.jpg > /dev/null 2>&1 ; then
         echo ${cnt}.jpg | cpio -o
     fi
     rm -f ${cnt}.jpg
done | ssh ${SSHHOST} "cd ${REMOTEDIR} && while cpio -i; do true; done"

echo "Done."
echo "To erase pictures: photopc -l/dev/ttyS${SERIALPORT} -s115200 erase all"

#exit


