#!/bin/sh
#
# cgi-bin program to receive a water rocket simulation request
#
# (C)Copyright 1997 Clifford Heath, Open Software Associates Limited.
# ALL RIGHTS RESERVED

PATH=/usr/local/bin/:/usr/local/bin/pbm:$PATH

counter=`cat counter` 2>/dev/null
echo `expr $counter + 1` > counter 2>/dev/null

#-----------------------------------------------------------------
# Introduce the response HTML page, in case we have bad news later
#
cat <<END
Content-type: text/html

<HTML>
<HEAD>
<TITLE> Water Rocket Results </TITLE>
</HEAD>
<BODY BACKGROUND=../not_scientists_bg.jpg BGCOLOR="#D7E7E8" LINK=#2C54CA>
<center>
<H2> Water Rocket Simulation Results
</H2>
Simulation number $counter, for ${REMOTE_HOST-$REMOTE_ADDR} ${HTTP_X_FORWARDED_FOR+(for }${HTTP_X_FORWARDED_FOR}${HTTP_X_FORWARDED_FOR+)} 
</center>
END

#-----------------------------------------------------------------
# Debug stuff, uncomment to see what's happening
#set -x
#exec 2>/tmp/rocksim.out
# OR
#exec 2>&1
#echo '<PRE>'

#-----------------------------------------------------------------
# Copy the correct amount of input (or all of it) through a filter
# that changes it to one value per line, then store the result.
#
if [ x"$CONTENT_LENGTH" != x"" ]
then
	dd bs=1 count=$CONTENT_LENGTH 2>/dev/null
	echo ''
else
	cat
fi |
	sed '
		s/&/\
/g
		s/+/ /g
		s/%0A//g
		s/%0D//g
		s/%2B/+/g
		s:%2F:/:g
		s/%3D/=/g
		' |
	sed '
		s/=/="/
		s/$/"/
		' |
	(
	while read variable
	do
		eval $variable
	done

	# echo volume=${volume-unset}
	# echo water=${water-unset}
	# echo pressure=${pressure-unset}
	# echo nozzle=${nozzle-unset}
	# echo nozzle_K=${nozzle_K-unset}
	# echo diameter=${diameter-unset}
	# echo Cd=${Cd-unset}
	# echo mass=${mass-unset}
	# echo initial=${initial-unset}
	# echo tube_len=${tube_len-unset}
	# echo tube_diam=${tube_diam-unset}
	# echo density=${density-unset}
	# echo SubmitSimulation=${SubmitSimulation-unset}

	echo `date +'%Y%m%d %H:%M'` $counter ${REMOTE_HOST-$REMOTE_ADDR} ${HTTP_X_FORWARDED_FOR+(for }${HTTP_X_FORWARDED_FOR}${HTTP_X_FORWARDED_FOR+)} volume=${volume-unset} water=${water-unset} pressure=${pressure-unset} nozzle=${nozzle-unset} nozzle_K=${nozzle_K-unset} diameter=${diameter-unset} Cd=${Cd-unset} mass=${mass-unset} initial=${initial-unset} density=${density-unset} tube_len=${tube_len-unset} tube_diam=${tube_diam-unset} >> log

	tmpdir=sims/rocket$$
	trap "rm -rf $tmpdir; exit 0" 0 2 3 15

	mkdir $tmpdir && chmod 777 $tmpdir && cd $tmpdir
	if [ $? != 0 ]
	then
		echo "Server configuration error, cannot access temporary directory"
		exit 1
	fi

	../../rocket.`uname -s` -H -v"${volume-2}" -w"${water-0.8}" -p"${pressure-120p}" \
		-h"${nozzle-22}" -d"${diameter-110}" -c"${Cd-0.3}" \
		-m"${mass-100}" -u"${initial-0}" -g"${density-1}" \
		-l"${tube_len-0}" -t"${tube_diam-0}" -k"${nozzle_K-0.16}"

	if [ -s altitude ]
	then
		gnuplot ../../plot1 > plot1.png 2>/dev/null
		gnuplot ../../plot2 > plot2.png 2>/dev/null
		gnuplot ../../plot3 > plot3.png 2>/dev/null
		gnuplot ../../plot4 > plot4.png 2>/dev/null

		cat <<END
		<H2> Altitude (Metres) </H2>
		<a href="$tmpdir/altitude">
		<img src=$tmpdir/plot1.png>
		</a>

		<H2> Speed (Metres/second)</H2>
		<a href="$tmpdir/speed">
		<img src=$tmpdir/plot2.png>
		</a>

		<H2> Acceleration (Metres/second<sup>2</sup>)</H2>
		<a href="$tmpdir/acceleration">
		<img src=$tmpdir/plot3.png>
		</a>

		<H2> Thrust (N)</H2>
		<a href="$tmpdir/thrust">
		<img src=$tmpdir/plot4.png>
		</a>
		<br>
END
	fi
	# Arrange to have temporary files removed after two minutes
	cd ../..
	trap "" 0
	# (bash -c "sleep 300; cd sims; find . -type d -mmin +5 | grep -v '\.$'|xargs rm -rf " </dev/null >/dev/null 2>&1 & )
	)

#-----------------------------------------------------------------
# Finally, finish the response page by letting the requester know
# that their request seems ok so far
#
cat <<END
</BODY>
</HTML>
END
