#!/bin/bash
#
#

IN=/var/smei/new/TelemProcess/img
OUT=/home/mpc/Projects/SMEI/Code/web/TelemCGI/latest
CONV=/home/mpc/Projects/SMEI/Code/web/TelemProcess/img2png/img2png

# Find the latest images

for c in 1 2 3;
do
	TD=""
	for cd in `ls "$IN" | sort | tail -2`;
	do
		cdd=`ls "$IN/$cd" | grep "C$c" | tail -1`
		if [ "x$cdd" != "x" ];
		then
			TD="$IN/$cd/$cdd"
		fi
	done
	if [ "X$TD" == "X" ];
	then
		echo "Can't find camera $c latest image directory"
	else
		# Get the last image in the directory...
		li=`ls "$TD" | tail -1`
		if [ "x$li" == "x" ];
		then
			echo "Couldn't find an image in $TD"
		else
			# FP is the full path, FN is the tail part for the page
			FP="$TD/$li"
			FN=${FP//*img\//}
			# Convert FN into a space separated list without playing with IFS...
			DATE="${FN//_/ }"
			DATE="${DATE//./ }"
			DATE="${DATE//\// }"
			# Pull out the year/day/secs.  Note the use of 10# as secs can have leading 0's
			# so we can't use plain old 'let/set' as it would treat it as octal later on
			set $DATE
			YEAR=$1
			DAY=$2
			let SECS=10#$6
			# Calculate HH:MM:SS
			let HR=$SECS/3600
			let MIN=($SECS-$HR*3600)/60
			let SEC=($SECS-$HR*3600-$MIN*60)
			# Fix up the human output
			HR=0$HR
			MIN=0$MIN
			SEC=0$SEC
			TIMESTR="${HR:0-2:2}:${MIN:0-2:2}:${SEC:0-2:2}"

			echo "<h2>Camera $c</h2>"
			echo "<p>Image collected : $YEAR-$DAY $TIMESTR</p>"
			echo "<p>Image path: $FN</p>"
			echo "<p class=\"last\">"
			echo "<a href=\"latest/c${c}_raw.png\"><img src=\"latest/c${c}_raw.png\" alt=\"${li} raw\"£></a>Raw Data<br />"
			echo "<img src=\"latest/spc.png\" alt=\"spacer\"£><br />"
			echo "<a href=\"latest/c${c}_lin.png\"><img src=\"latest/c${c}_lin.png\" alt=\"${li} linear\"£></a>Linear Palette<br />"
			echo "<img src=\"latest/spc.png\" alt=\"spacer\"£><br />"
			echo "<a href=\"latest/c${c}_sqrt.png\"><img src=\"latest/c${c}_sqrt.png\" alt=\"${li} sqrt\"£></a>Sqrt Palette<br />"
			echo "<img src=\"latest/spc.png\" alt=\"spacer\"£><br />"
			echo "<a href=\"latest/c${c}_mv.png\"><img src=\"latest/c${c}_mv.png\" alt=\"${li} Median\"£></a>Median Linear Palette<br />"
			echo "<img src=\"latest/spc.png\" alt=\"spacer\"£><br />"
			echo "<a href=\"latest/c${c}_ms.png\"><img src=\"latest/c${c}_ms.png\" alt=\"${li} Median Sqrt\"£></a>Median Sqrt Palette<br />"
			echo "</p>"

			set `cat "$OUT/c${c}_stamp"` X
			ODATE="$1"
			if [ X"$DATE" != X"$ODATE" ];
			then
				$CONV --imgfile="${TD}/${li}" --pngfile="$OUT/c${c}_lin.png.$$"  --palette="linear"
				$CONV --imgfile="${TD}/${li}" --pngfile="$OUT/c${c}_sqrt.png.$$" --palette="sqrt"
				$CONV --imgfile="${TD}/${li}" --pngfile="$OUT/c${c}_raw.png.$$"  --palette="none"
				$CONV --imgfile="${TD}/${li}" --pngfile="$OUT/c${c}_mv.png.$$"   --palette="medvar"
				$CONV --imgfile="${TD}/${li}" --pngfile="$OUT/c${c}_ms.png.$$"   --palette="medsqr"
				echo "$DATE" > "$OUT/c${c}_stamp.$$"
				mv -f "$OUT/c${c}_lin.png.$$" "$OUT/c${c}_lin.png"
				mv -f "$OUT/c${c}_sqrt.png.$$" "$OUT/c${c}_sqrt.png"
				mv -f "$OUT/c${c}_raw.png.$$" "$OUT/c${c}_raw.png"
				mv -f "$OUT/c${c}_mv.png.$$" "$OUT/c${c}_mv.png"
				mv -f "$OUT/c${c}_ms.png.$$" "$OUT/c${c}_ms.png"
				mv -f "$OUT/c${c}_stamp.$$" "$OUT/c${c}_stamp"
				chmod 666 "$OUT/c${c}_sqrt.png" "$OUT/c${c}_raw.png" "$OUT/c${c}_lin.png" "$OUT/c${c}_mv.png" "$OUT/c${c}_ms.png" "$OUT/c${c}_stamp"
			fi
		fi
	fi
done
