#!/bin/sh
#
# installation script for wwwoffle2.4
#

set -e
echo $0 $1 $2
PREFIX=${PKG_PREFIX:-/usr/local}
SPOOLDIR=/var/spool/wwwoffle
CONFDIR=${SPOOLDIR}
LOCALHOST=localhost:8080
INSTALL="install -c"

case $2 in
POST-INSTALL)

	if [  -d ${SPOOLDIR}/http ]
	then
		echo "not upgrading spool directory"
	else
		echo "upgrading spool directory"
		${PREFIX}/bin/upgrade-cache ${SPOOLDIR}
	fi
	
	if [  -d ${SPOOLDIR}/http ] 
	then
		if ${PREFIX}/bin/endian-cache > /dev/null 2>&1 
		then
			echo "no cache conversion needed"
		else
			echo "converting cache"
			${PREFIX}/bin/endian-cache ${SPOOLDIR}
		fi
	fi

	echo "creating directories and files as needed"
	[ -x ${SPOOLDIR} ] || ${INSTALL} -d ${SPOOLDIR}
	[ -x ${SPOOLDIR}/http ] || ${INSTALL} -d ${SPOOLDIR}/http
	[ -x ${SPOOLDIR}/outgoing ] || ${INSTALL} -d ${SPOOLDIR}/outgoing
	[ -x ${SPOOLDIR}/monitor ] || ${INSTALL} -d ${SPOOLDIR}/monitor
	[ -x ${SPOOLDIR}/lasttime ] || ${INSTALL} -d ${SPOOLDIR}/lasttime
	[ -x ${SPOOLDIR}/prevtime1 ] || ${INSTALL} -d ${SPOOLDIR}/prevtime1
	echo 'GET http://www.gedanken.demon.co.uk/wwwoffle/version-2.4/user.html HTTP/1.0'        > ${SPOOLDIR}/outgoing/Owwwoffle-user
	echo 'Host: www.gedanken.demon.co.uk'                                                    >> ${SPOOLDIR}/outgoing/Owwwoffle-user
	echo 'Accept: */*'                                                                       >> ${SPOOLDIR}/outgoing/Owwwoffle-user
	echo ''                                                                                  >> ${SPOOLDIR}/outgoing/Owwwoffle-user
	awk 'BEGIN{printf("http://www.gedanken.demon.co.uk/wwwoffle/version-2.4/user.html");}'    > ${SPOOLDIR}/outgoing/Uwwwoffle-user < /dev/null
	echo 'GET http://www.gedanken.demon.co.uk/wwwoffle/version-2.4/browser.html HTTP/1.0'     > ${SPOOLDIR}/outgoing/Owwwoffle-browser
	echo 'Host: www.gedanken.demon.co.uk'                                                    >> ${SPOOLDIR}/outgoing/Owwwoffle-browser
	echo 'Accept: */*'                                                                       >> ${SPOOLDIR}/outgoing/Owwwoffle-browser
	echo ''                                                                                  >> ${SPOOLDIR}/outgoing/Owwwoffle-browser
	awk 'BEGIN{printf("http://www.gedanken.demon.co.uk/wwwoffle/version-2.4/browser.html");}' > ${SPOOLDIR}/outgoing/Uwwwoffle-browser < /dev/null

	echo "installing configuration file"
	if [ -f ${CONFDIR}/wwwoffle.conf ]
	then
		echo "upgrading configuration file"
		perl ${SPOOLDIR}/hold/upgrade-config.pl ${CONFDIR}/wwwoffle.conf
	else
		${INSTALL} -m 644 ${CONFDIR}/wwwoffle.conf.install ${CONFDIR}/wwwoffle.conf
	fi

        if [ -x ${SPOOLDIR}/html ] 
	then
		echo "preserving old html files"
		rm -rf ${SPOOLDIR}/html.old
		mv ${SPOOLDIR}/html ${SPOOLDIR}/html.old
        fi

	echo "moving html files into place"
	tar cf - -C ${SPOOLDIR}/hold html | tar xf - -C ${SPOOLDIR}
	echo "fixing up html files"
	(cd ${SPOOLDIR}/html && ./fixup-install.sh ${SPOOLDIR} ${LOCALHOST} && rm ./fixup-install.sh)

#	echo "removing holding directory"
#	rm -rf ${SPOOLDIR}/hold

	;;

*)
	echo "$0: $1 - doing nothing"
	;;
esac

exit 0
