#!/usr/bin/haserl -u262144 --upload-handler=/ross/bin/upload_handler.sh
<%
if test "$REQUEST_METHOD" != "POST"; then
echo -en "Content-type: text/html\r\n\r\n"
cat << EOF
<h3>Software Upgrade</h3>
<p>Please use DashBoard to perform software upgrade</p>
<form action="$SCRIPT_NAME" method="POST" enctype="multipart/form-data">
<input type="file" name="uploaded_file">
<input type="submit" name="submitButton" value="Upload Firmware">
</form>
EOF
exit
else
echo -en "Content-type: text/plain\r\n\r\n"
echo "Version: 1"
echo "Timeout: 300000"
fi

LOG=/tmp/upgrade.log
DIR=/tmp/upgrade

umask 022
export LANG=C
export LC_ALL=C
PATH=/bin:/sbin:/usr/bin:/usr/sbin

error() {
	# check the log of the upload handler
	cat $LOG
	test -z "$1" || echo "Status: ERROR $1"
	rm -rf $DIR
	exit
}

# The $DIR is created by the /.upload_handler.sh
test -e $DIR		|| error
cd $DIR			|| error "Failed to change dir"

# Tarball must include an upgrade script
test -e ross/bin/doinstall	|| error "Invalid upgrade file"

# Prevent a race between redirect and tail
touch $LOG

# Run the upgrade in a separate process, in case browser socket closes
sh -x ross/bin/doinstall $DIR </dev/null >>$LOG 2>&1 &

# Send output from the upgrade process back the browser/dashboard.
tail -f $LOG
%>
