#!/bin/sh

# VERSION=4

if [ -x /usr/local/bin/beroconf ] ; then
	BEROCONF=/usr/local/bin/beroconf
else
	BEROCONF=/usr/fallback/beroconf
fi

TFTP_UPDATE=/usr/local/bin/update-tftp.sh

PROD_SERVERS="berofixprod1.middle-earth.local berofixprod2.middle-earth.local berofixprod3.middle-earth.local"

case "${1}" in
	start)
		# leave if card is already produced
		NEED_PROD="`${BEROCONF} get root need_prod | grep -v failed`"
		if [ "${NEED_PROD}" != 1 ]; then
			echo " * bero*fix is already produced, leaving"
			exit 0
		fi

		# wuerg-around for ttyS0-mystery on 6400-cards
		if [ "`expr match "\`cat /proc/cmdline\`" ".*[0-9]-\(64\)-[0-9]\{10\}.*"`" = "64" ]; then
			if [ -c /dev/ttyS0 ]; then
				echo " * Found bero*fix 6400, fixing ttyS0 bug, rebooting"
				mount -oremount,rw /
				mv /dev/ttyS0 /dev/disabled_ttyS0
				mount -oremount,ro /
				reboot
				exit 1
			fi
		fi

		# check if IP-address comes from a dhcp, reboot if it's standard 10.0.0.2
		test_ip=`expr match "\`ifconfig eth0\`" ".*inet addr:\([0-9\.]*\)"`
		if [ "${test_ip}" = "10.0.0.2" ]; then
			echo " * Couldn't retrieve address from DHCP, rebooting"
			reboot
			exit 1
		fi

		echo " * Finishing production process: "

		# retrieve card information
		CARD_INFO=`cat /proc/cmdline`
		CARD_UUID=`expr match "${CARD_INFO}" ".*\([0-9]\-[0-9]\{2\}\-[0-9]\{10\}\)"`

		# check if UUID is valid
		if [ "${CARD_UUID}" = "" ]; then
			echo "   => Failed. (uuid is invalid)"
			exit 1
		fi

		# install pre-installed appfs
		mount -oremount,rw /usr/local
		chown -R root:root /usr/local/*
		[ -x /usr/local/setup/post_install.sh ] && /usr/local/setup/post_install.sh
		sleep 1; sync; sync; sleep 1
		cd / && mount -oremount,o /usr/local

		# check if TFTP update script is there
		if [ ! -x "${TFTP_UPDATE}" ]; then
			echo "   => Failed. (tftp update script not found)"
			exit 1
		fi

		# Update AppFS & config
		${TFTP_UPDATE} firmware
		if [ ${?} -ne 0 ]; then
			echo "   => Failed. (could not update firmware, rebooting)"
			reboot
			exit 1
		fi

		${TFTP_UPDATE} config
		if [ ${?} -ne 0 ]; then
			echo "   => Failed. (could not update configuration, rebooting)"
			reboot
			exit 1
		fi

		# retrieve further card information
		if [ ! -f "/usr/local/FILENAME" ]; then
			echo "   => Failed. (AppFS version file not found, rebooting)"
			reboot
			exit 1
		fi

		APP_INFO=`cat /usr/local/FILENAME`
		MAC_INFO=`ifconfig | grep eth0`

		CARD_REV=`expr match "${CARD_INFO}" ".*revision=\([1-9]\{1,2\}\.[0-9]\{2\}\)"`
		CARD_MAC=`expr match "${MAC_INFO}" ".*\(\([0-9A-F]\{2\}\:\)\{5\}\([0-9A-F]\{2\}\)\)"`
		CARD_APP=`expr match "${APP_INFO}" ".*appfs\-\([a-z0-9\.\-\_]*\)\.tar\.gz"`

		PROD_SET_URL="produce_setstatus.php?uuid=${CARD_UUID}&revision=${CARD_REV}&appfs=${CARD_APP}&mac=${CARD_MAC}"

		# tell production system we're done
		for server in ${PROD_SERVERS}; do
			ping -c 1 ${server} &> /dev/null
			if [ ${?} -eq 0 ]; then
				prod_url="http://${server}/${PROD_SET_URL}"
				echo "`wget ${prod_url} -qO -`" | grep "confirmed" &> /dev/null
				if [ ${?} -eq 0 ]; then
					${BEROCONF} set root lan-dhcp no
					${BEROCONF} set root lan-ipaddr 10.0.0.2
					${BEROCONF} set root lan-netmask 255.0.0.0
					${BEROCONF} set root lan-gateway 10.0.0.1
					${BEROCONF} delete root provisioning-option
					${BEROCONF} delete root tftp-host
					${BEROCONF} delete root tftp-url
					${BEROCONF} set root use-boot-prov no
					${BEROCONF} set root need_prod 0
					rm -f /usr/conf/ari.db
					if [ -f /usr/local/modules/berofix.ko ]; then
						insmod /usr/local/modules/berofix.ko > /dev/null
						for i in `seq 0 7`; do
							echo 1 > /proc/led${i}
						done
					fi
					echo "   => Done."
					exit 0
				fi
			fi
		done

		echo "   => Failed. (no production system was reachable, rebooting)"
		reboot
		exit 1
		;;
	*)
		echo "Usage: ${0} {start}" >&2
		exit 1
		;;
esac
