#!/bin/bash
#
#	Sybok bootloader
#
#TODO PID_SYBOK=/var/run/sybok.pid
HTTPSERVER="http://fwup.dmd.it/sybok"
PLATFORM="armv7l"
VERSION="devel"

LOCAL_APP_FILE="/temp/sybok/sybok"
LOCAL_LOADER_FILE="/temp/sybok/sybok_loader"

#######################
# HWDEPENDENT functions
# TODO make these aware of target and modify their behaviour
#######################
 
do_prepare_hw()
{
	#TODO it is hardware dependent. make this dependent on board / os etc
	#FIXME raw ensure server up and running
	sleep 2
	
	#enable can (baud already configured by dmdhw)
	udpctrl out-set CAN1-EN 1
	udpctrl out-set CAN1-STANDBY 0
	udpctrl out-set CAN2-STANDBY 0
	udpctrl out-set CAN-RX-EN 1
	
	#enable GPS port mux
	
	tty_bus -d -s /temp/gpsmux0
        tty_attach -d -s /temp/gpsmux0 /dev/ttymxc5
        tty_fake -d -s /temp/gpsmux0 /dev/ttynmea1
        tty_fake -d -s /temp/gpsmux0 /dev/ttynmea2

	udpctrl watchdog-enable 60000	
}

do_config_env()
{
	mkdir /temp/sybok
	mount /dev/mmcblk1p2 /var/datalogger/	
	
	udpctrl version-list 2>&1 | grep power | cut -c 7- > /temp/sybok/pmrev
	ubootset -dmd rifsn > /temp/sybok/rifsn	
	
	#TODO this is hw dependent. make this dependent on board ecc
	export SCI0=/dev/ttynmea1
	export SCI1=/dev/ttymxc2
	export SCI2=/dev/ttyUSB1
	#export UNIQUEID=`ubootset ethaddr | sed s/://g`
#input (0 to 2 in power manager)
        export GPIO0=-1
        export GPIO1=-1
        export GPIO2=-1
        export GPIO3=113
        export GPIO4=114
        export GPIO5=110
        export GPIO6=68
        export GPIO7=46
        export GPIO8=-1
        export GPIO9=-1
        export GPIO10=-1
        export GPIO11=-1
        export GPIO12=-1
        export GPIO13=-1
        export GPIO14=-1
        export GPIO15=-1
#output
        export GPIO16=-1
        export GPIO17=-1
        export GPIO18=112
        export GPIO19=-1
        export GPIO20=-1
	export GPIO21=-1
	export GPIO22=-1
        export GPIO23=-1
        export GPIO24=-1
        export GPIO25=-1
        export GPIO26=-1
        export GPIO27=-1
        export GPIO28=-1
        export GPIO29=-1
        export GPIO30=84
	export GPIO31=116
}

do_startreboot()
{
        #TODO hw dependent. bypassed
        udpctrl modem-off 0
	udpctrl reboot
	return
}

do_startshutdown()
{
	#TODO read env_var with wakeupsources
	udpctrl shutdown
	return
}

#########################
# HWINDEPENDANT functions
#########################

do_update_backup()
{
	rm sybok-backup
	rm sybok-backup.md5
	cp sybok.md5 sybok-backup.md5
	cp sybok sybok-backup
}

do_restore_backup()
{
	if [ -f "sybok-backup" ] 
	then
	        md5_computed=`md5sum sybok-backup | sed -e 's/\s.*$//'`
        	md5_downloaded=`cat sybok-backup.md5 | sed -e 's/\s.*$//'`
        	echo md5_computed $md5_computed md5_downloaded $md5_downloaded
        	if [ "$md5_computed" = "$md5_downloaded" ]
        	then
                	echo restore backup ok
                	cp sybok-backup.md5 sybok.md5
                	cp sybok-backup sybok
                	chmod +x sybok
       	 	else
                	echo restore backup failed,wrong md5, try to download from server
			do_update
        	fi
	else
		echo restore backup failed,missing file, try do download from server
		do_update
	fi
}

do_update()
{
        local version
        if [ "$VERSION" = "" ]
        then
                wget $HTTPSERVER/sybok.version
                version=`cat sybok.version`
        else
                version=$VERSION
        fi

        rm sybok-$version
        rm sybok-$version.md5
        wget $HTTPSERVER/$PLATFORM/sybok-$version.md5
        wget $HTTPSERVER/$PLATFORM/sybok-$version
        md5_computed=`md5sum sybok-$version | sed -e 's/\s.*$//'`
        md5_downloaded=`cat sybok-$version.md5 | sed -e 's/\s.*$//'`
        echo md5_computed $md5_computed md5_downloaded $md5_downloaded
        if [ -f "sybok-$version" ]
	then
		if [ "$md5_computed" = "$md5_downloaded" ]
        	then
	                echo update completed
        	        cp sybok-$version.md5 sybok.md5
                	cp sybok-$version sybok
                	chmod +x sybok
			do_update_backup
        	else
                	echo update failed, wrong md5 or everythingelse
                	#TODO autoretry? now it relaunch sybok (if exists with md5 ok)
        	fi
	else
		echo update failed, missing file
		#TODO do something?
	fi
}


do_local_update()
{
        rm sybok-local
        rm sybok-local.md5
	cp $LOCAL_APP_FILE ./sybok-local
	cp $LOCAL_APP_FILE.md5 ./sybok-local.md5

	if [ -f "sybok-local" ]
	then 
	        md5_computed=`md5sum sybok-local | sed -e 's/\s.*$//'`
        	md5_downloaded=`cat sybok-local.md5 | sed -e 's/\s.*$//'`
	        echo md5_computed $md5_computed md5_downloaded $md5_downloaded
        	if [ "$md5_computed" = "$md5_downloaded" ]
        	then
	                echo update completed
        	        cp sybok-local.md5 sybok.md5
                	cp sybok-local sybok
			do_update_backup
        	        chmod +x sybok
			do_update_backup
        	else
                	echo local update failed, wrong md5 or everythingelse
                	#TODO autoretry? now it relaunch sybok if exists with md5 ok
        	fi
	else
		echo local update failed, missing files
		#TODO dosomething? at now it relaunch sybok
	fi
}


do_start()
{
	if [ -f "sybok" ]
	then
 	       	md5_computed=`md5sum sybok | sed -e 's/\s.*$//'`
        	md5_downloaded=`cat sybok.md5 | sed -e 's/\s.*$//'`
		if [ "$md5_computed" = "$md5_downloaded" ]
 		then
			#TODO handle PID to externally kill
			#./sybok < /dev/tty0 > log.txt #log to file
                	./sybok < /dev/tty0
			#./sybok  #foreground console	
			return $?
		else
			return 255
		fi
	else
		return 255
	fi
}

do_stop()
{
	stty sane
	udpctrl watchdog-enable 600000
	#TODO kill something? stop ppp?
}


do_threadrun()
{

continue=1
	while [ $continue = 1 ]
	do
		do_start
		ret=$?
		echo sybok do_start exited $ret
		if [ $ret = 0 ] 
		then
			echo exited ok, reboot
			do_startreboot
			continue=0
		elif [ $ret = 2 ]
		then
			echo shutdown with lowpower
			do_startshutdown
			continue=0
		elif [ $ret = 3 ]
		then
			echo warm reset, sybok restart
			continue=1
		elif [ $ret = 1 ]
		then
			echo update and reload, enterbootloader
			do_local_update
			continue=1
                elif [ $ret = 255 ]
                then
                        echo wrong md5 or missing files. trying to restore backup
                        do_restore_backup
                       	continue=1
		elif [ $ret = 130 ] 
		then
			echo ctrlc, exiting loader
			continue=0
                elif [ $ret = 137 ] || [ $ret = 143 ] 
                then
                        echo killed, exiting loader
                        continue=0
                else
			echo unhandled, reboot after 10 min
			udpctrl watchdog-enable 660000
			sleep 600
			do_startreboot
			continue=0
		fi
	done
}


#main
do_prepare_hw
do_config_env
do_threadrun
do_stop


