#!/bin/sh

# Another version of webcam-upload.  This script grabs a pic from the
# video input (the camera) and copies it to an exported NFS dir (or just
# a dir - doesn't matter whether it's exported or not).  That way, data
# is only transmitted across the network when the image is read.  There
# is a slight overhead with NFS, of course.

# The file to copy the webcam picture to
COPYFILE=${COPYFILE:-/tmp/nfs-cam.png}
# Time to wait between grabbing pictures
INTERVAL=${INTERVAL:-60}
# Remote dir to CD to before uploading
REMDIR="${REMDIR:-gfx}"
# Filename of the temporary pic
TEMP="${TEMP:-uploading.png}"
# Filename for the final pic
NAME="${NAME:-webcam.jpg}"
# Temporary dir to store pics in
TDIR="${TDIR:-/tmp}"
# Special message file
MSGFILE="${MESSAGE:-${TDIR}/message}"
# The position of the uptime annotation
# UPTPOS="10,10"
# The position of the date annotation
DATEPOS="${DATEPOS:-10,275}"
# Position of the text message
MSGPOS="${MSGPOS:-10,48}"
# Position of the title message
TITLEPOS="${TITLEPOS:-10,10}"
# Colour of the title
TITLECOL="${TITLECOL:-darkblue}"
# The title message
TITLEMSG="${TITLEMSG:-Brooknet Webcam}"
# The colour of the date legend (usually 'grey80')
DATECOL="${DATECOL:-grey80}"
# The colour of the text message (used to be 'grey60')
MSGCOL="${MSGCOL:-grey90}"
# The font of the date legend
DATEFONT="${DATEFONT:--adobe-times-*-*-*-*-14-*}"
# The font of the text message
# (old: "-adobe-times-*-*-*-*-14-*")
MSGFONT="${MSGFONT:--adobe-courier-medium-r-normal-*-14-*}"
# Width of the text message (usually '60')
MSGWIDTH="${MSGWIDTH:-39}"
# Width of the uptime display (usually '50')
UPTWIDTH="${UPTWIDTH:-50}"
# The filename of a temporary pic to replace the camera pic (see comment).
TESTPIC="${TESTPIC:-/tmp/testcard.png}"
# Initial brightness and contrast settings
BRIGHT=${BRIGHT:-50%}
CONTRAST=${CONTRAST:-50%}

echo "$0 running.  Press Ctrl-C to quit."

# Setup V4L.  For some reason, I need to set stupidly high contrast and
# brightness values to get anything at all.  This needs investigating.
# Okay.. contrast = shutter speed.  brightness = gain.
# brightness range = 256-64768
# contrast (shutter) range = 256-65535
# *** Only one value can be set at once ***
# I now use v4lctl here, instead of setv4l.
# 21-Nov-02: with quickcam-tt driver, contrast and brightness can't be set.
v4lctl bright $BRIGHT
v4lctl contrast $CONTRAST

while [ 1 ]; do

CAMPIC="${TDIR}/webcam-`date +%y-%m-%d-%H%M%S`.png"

# Note: the '-s' (size) option seems to have no effect
# Also, sometimes streamer creates two output files (annoying!)
# As of 04-Jun-02, I am using 'vgrabj' instead of streamer.
# streamer -s 352x288 -o $CAMPIC
# 18-Jun-02: the camera pic can now be replaced by a picture of your choice.
# If present, the pic will be used and a grab will not be done.
# From 21-Nov-02, I'm temporarily using streamer again, cos' I've lost
# vgrabbj [got vgrabbj later].
if [ ! -f "$TESTPIC" ]; then
  # streamer -o $CAMPIC
  # vgrabbj -d /dev/video0 > $CAMPIC
  # Experimental vgrabbj PNG format (01-Jan-03)
  vgrabbj -g -o png -e -t /usr/share/fonts/default/TrueType/cogb____.ttf \
  -a 2 -S > $CAMPIC
else
  cp $TESTPIC $CAMPIC
fi

if [ ! -f "$CAMPIC" ]; then
  echo "Oops - no $CAMPIC pic (webcam pic) to upload!"
  exit 1
fi

set -v

#---
# Example of use of 'convert':
#           convert -font helvetica -fill blue
#                   -draw "text 100,100 Cockatoo"
#                   bird.jpg bird.miff
#
#---

# 21-Nov-02: Make the pic a bit brighter, cos' with the current quickcam-tt
# driver, I can't modify the contrast or brightness with setv4l or v4lctl,
# though it works with xawtv.
# 01-Jan-02: probably not required any more, since vgrabbj can do this sort
# of thing itself.
if [ -f "/tmp/bright" ]; then
  convert -modulate 250 $CAMPIC $CAMPIC
fi
# Annotate the pic (25-Mar-02, 03:03)
# As of 01-Jan-03, I'm using vgrabbj to add a timestamp, so this stuff
# is not required.
# convert -font "-adobe-times-*-*-*-*-14-*" -fill grey80 -draw "text 10,235 '`date`'" $CAMPIC $CAMPIC
# Uptime display disabled on 05-Jun-02.
# convert -font "${DATEFONT}" -fill $DATECOL -draw "text $DATEPOS \"`date`\" text $UPTPOS \"`uptime | fold --width=${UPTWIDTH} --spaces`\"" $CAMPIC $CAMPIC
# convert -font "${DATEFONT}" -fill $DATECOL -draw "text $DATEPOS \"`date`\"" -fill $TITLECOL -draw "text $TITLEPOS \"$TITLEMSG\"" $CAMPIC $CAMPIC

# Display a special message, if available
if [ -f "$MSGFILE" ]; then
  MESSAGE="`cat $MSGFILE | fold --width=${MSGWIDTH} --spaces`"
  convert -font "${MSGFONT}" -fill $MSGCOL -draw "text $MSGPOS \"$MESSAGE\"" $CAMPIC $CAMPIC
fi

# At this point, $CAMPIC is the newly-grabbed pic, $TEMP is the name
# of the temporary upload file and $NAME is the 'final name' (webcam.jpg,
# usually)

cp -v $CAMPIC $COPYFILE

# 01-Jan-03: vgrabbj can do this as well, but it currently segfaults
# when I use this facility, so I'll leave this bit intact.
if [ -d "${TDIR}/webcam-archive/" ]; then
  mv -v $CAMPIC ${TDIR}/webcam-archive/
else
  rm -f $CAMPIC
fi

# Delete all .jpeg files in the temporary dir - this is a kluge to
# fix the bug in 'webcam' that creates two files occasionally.
# 01-Jan-03: Do I need this thing?
# rm -v ${TDIR}/webcam-??????.png || echo "(This is nothing to worry about)"

set +v

echo -n "Zzz..."
sleep $INTERVAL
echo

done
