#!/bin/sh

REALDIR=${1:-/home/pe1rxq/src/motion/shots}
PUBLICDIR=${2:-/shots}

# echo Content-type: text/html
# echo max-age: 0
# echo expires: 0
# echo pragma: no-cache
# echo
echo "<HTML>"
echo "<HEAD>"
echo "<TITLE>"
echo Motion detector
echo "</TITLE>"
echo "<H1>Motion detector</H1>"
echo -n "Last updated: "; date
echo "<HR>"
echo "</HEAD>"
echo "<BODY>"
cd $REALDIR

# ls -1 *.jpg | tail -n 50| awk -v dir=$PUBLICDIR -- '{printf ("<IMG SRC=%s/%s>%s<BR>\n", dir, $0, substr($0,1,index($0,".")-1))}'
ls -1 *.jpg | sed  -e 's/\.jpg$//' -e 's/m$//' | sort -u | tail -n 50 | while read filebase ; do
    if [ -f ${filebase}.jpg ] ; then
        echo "<IMG SRC=\"$PUBLICDIR/${filebase}.jpg\">"
    fi
    if [ -f ${filebase}m.jpg ] ; then
        echo "<IMG SRC=\"$PUBLICDIR/${filebase}m.jpg\">"
    fi
    echo "$filebase<BR>"
done
echo "</BODY>"
echo "</HTML>"
