#!/bin/sh
#
#
#

XDATA_PICTURES_PATH=/mnt/XDATA/pictures
CAMERA_POWER_SWITCH=1
ZOOM_LEVEL=0

while [ "$1" ]
do
   if [ "$1" = "-z" ]; then
      ZOOM_LEVEL="$2"
      shift 2
   else
      echo "Program $0 does not recognize option $1"
   fi
done

if [ ! -d "${XDATA_PICTURES_PATH}" ] ; then
	sudo mkdir ${XDATA_PICTURES_PATH}
	sudo chown smc ${XDATA_PICTURES_PATH}
	sudo chgrp smc ${XDATA_PICTURES_PATH}
fi

#set -x

echo "Turning Camera On"
sudo SetPowerSwitch ${CAMERA_POWER_SWITCH} 1

# Give the camera some time to enumerate on USB
sleep 10 

echo "Extracting Lens, Setting Flash Off, and Setting Zoom"
sudo gphoto2 --quiet			\
	--set-config capture=1		\
	--set-config flashmode=4	\
	--set-config zoom=$ZOOM_LEVEL

# Create a time-based file name for the file
FILENAME=$(date +"IMG_%Y%m%d%H%M%S.JPG")

echo "Taking Picture..."
sudo gphoto2 --quiet			\
	--capture-image-and-download	\
	--filename ${FILENAME} 

cp ${FILENAME} ${XDATA_PICTURES_PATH}/${FILENAME}
rm -f ${FILENAME}

echo "Retracting Lens"
sudo gphoto2 --quiet			\
	--set-config capture=0

sleep 5

echo "Turning Camera Off"
sudo SetPowerSwitch ${CAMERA_POWER_SWITCH} 0

sudo chown smc ${XDATA_PICTURES_PATH}/${FILENAME}
sudo chgrp smc ${XDATA_PICTURES_PATH}/${FILENAME}

# Delete the photo so we don't fill the partition
###rm ${XDATA_PICTURES_PATH}/${FILENAME}

printf 'Image at: %s/%s\n' ${XDATA_PICTURES_PATH} ${FILENAME} 
