#!/bin/sh

########################
# LiveSnapMovie        #
# By Miha Ambrož       #
# miha.ambroz@amis.net #
# V0.3, 12.11.2005     #
########################

# This script captures and downloads a snapshot from a Kodak DC-2xx digital camera,
# superimposes a caption, then saves the raw version to a local numbered file
# (useful for encoding a movie) and uploads the captioned version to a FTP server.

# The script requires gphoto2, convert and ncftp. The DC-2xx should be connected to USB port
# and has to have an empty memory card in it (the data on the card are erased each time the
# snapshot is taken). The camera settings should be set manually and the mode dial has to be
# on "CONNECT" before running the script.

echo "LiveSnapMovie running..."
number=0

# Do forever
while [ $number -lt 1 ]; do

# First we delete the current livecam snap.
	rm -f ~/.livecam.jpg
# Then we delete all the files from the memory card in the camera...
	gphoto2 -R -D 1>/dev/null 2>/dev/null
# ...capture the snapshot...
	gphoto2 --capture-image 1>/dev/null 2>/dev/null
# ...and download all files from the camera (as we don't know the exact filename of the last taken picture)
# to a temporary JPEG file.
	gphoto2 --filename ~/.livecam.jpg --get-all-files 1>/dev/null 2>/dev/null

# Then we create the stamp for the picture caption and the filename suffix for the raw version.
	STAMP=" \"$(date +%d.%m.%Y' '%T) http://home.amis.net/mambroz\""
	ADDDM="$(date +%Y%m%d%H%M%S)"

# Then we resize the temporary image for the raw version...
	convert -resize 480x320 ~/.livecam.jpg ~/livecam/movie$ADDDM.jpg	

# ...and for FTP upload.
# Here we also add the caption (we do it twice - once in black and once in white - to get the shadow effect).
	convert -resize 480x320 -font helvetica -fill black -pointsize 12 -draw "text 10,22 $STAMP" -font helvetica -fill white -pointsize 12 -draw "text 9,21 $STAMP" ~/.livecam.jpg ~/website/livecam.jpg	

# Finally, we upload the captioned version to a FTP server
# (set/modify $USERNAME, $PASSWORD, $FTPSERVER and $REMOTEDIR as applies).
	ncftpput -DD -u $USERNAME -p $PASSWORD $FTPSERVER $REMOTEDIR ~/website/livecam.jpg 1>/dev/null 2>/dev/null

# Before we do the next cycle, we sleep for 10 seconds to let the camera recharge the flash.
	sleep 10 1>/dev/null 2>/dev/null
done