#!/bin/bash

########################
# LiveSnapMovie        #
# By Miha Ambrož       #
# miha.ambroz@amis.net #
# V0.2, 16.8.2007      #
########################

# This script captures and downloads a snapshot from a v4l-compatible capture device,
# creates two scaled-down images, superimposes a timestamp to the small version and a
# caption to the large one, then saves the small version to a local numbered file
# (useful for encoding a movie) and uploads the large version to a FTP server.

# The script requires streamer, convert and ncftp. It assumes that the capture device
# is working, properly set up and capable of recording still images at 640x480 pixel
# resolution and that its device is /dev/video0.

echo "LiveSnapMovie running..."
number=0

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

# First we delete the current livecam snap.
	rm -f ~/.livecam.ppm

# Then we capture one image from camera into a temporary file
	streamer -s 640x480 -c /dev/video0 -o ~/.livecam.ppm 1>/dev/null 2>/dev/null

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

# Then we resize the temporary image for the archive image.
# We also add the timestamp (we do it twice - once in black and once in white - to get the shadow effect).
	convert -resize 320x240 -font helvetica -fill black -pointsize 12 -draw "text 10,22 $STAMP" -font helvetica -fill white -pointsize 12 -draw "text 9,21 $STAMP" ~/.livecam.ppm ~/livecam/movie$ADDDM.jpg

# Then we resize the temporary image for the live image.
# Instead of timestamp we add the caption here (also twice to get the shadow effect).
	convert -resize 480x360 -font helvetica -fill black -pointsize 12 -draw "text 10,22 $STAMPCAPTION" -font helvetica -fill white -pointsize 12 -draw "text 9,21 $STAMPCAPTION" ~/.livecam.ppm ~/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 19 seconds to avoid clogging the FTP connection.
	sleep 19

done