#!/bin/sh

#-----------------Script Actions (Modify at your own risk)-------------------------------

#flash led when starting installation
ccsettingsconsole --led --color=red --frequency=5 --dutycycle=20

# check for identity file
install=0

pattern="VERSION_*"
versionFile="/media/usb/${pattern}"

if [ -e $versionFile ];
then
	echo "Found version file: ${versionFile}"

	installerModelName=$(awk -F " " '/commander_model/ {print $2}' $versionFile)
	installerModelVersion=$(awk -F "=" '/commander_model/ {print $2}' $versionFile)
	
	echo "Found installerModelName in file : ${installerModelName}"
	echo "Found installerModelVersion in file : ${installerModelVersion}"
fi
	
if [ -e /opt/identity ];
then
	echo "Found identity file."

	currentModelName=$(awk -F " " '/commander_model/ {print $2}' /opt/identity)
	currentModelVersion=$(awk -F "=" '/commander_model/ {print $2}' /opt/identity)

	echo "Found currentModelName in file : ${currentModelName}"
	echo "Found currentModelVersion in file : ${currentModelVersion}"
	
	echo "Current model  : $currentModel"
	echo "Installer model: $installerModel"
	
	# model must match model in version file
	if [ "$currentModelName" == "$installerModelName" ];
	then
		version_cmp() { test "$(printf '%s\n' "$@" | sort -n | head -n 1)" != "$1"; }
		
		if version_cmp $installerModelVersion $currentModelVersion ; 		
		then
			echo "Installer model is greater than present $installerModel"
			install=1
			echo "commander_model $installerModelName =$installerModelVersion" > /opt/identity
		fi 
	fi
else	
	# assume this is a new display.  Proceed with install.
	echo "No identity file."
	echo "commander_model $installerModelName =$installerModelVersion" > /opt/identity
	install=1
fi

if [ $install -eq 1 ];
then
	echo "OK to install."
else
	echo "Not OK to install."
	
	# beep 3 times if fail
	ccsettingsconsole --buzzer --frequency=1000 --volume=40 --status=enable
	sleep 0.2
	ccsettingsconsole --buzzer --frequency=1000 --volume=40 --status=disable
	sleep 0.2
	ccsettingsconsole --buzzer --frequency=1000 --volume=40 --status=enable
	sleep 0.2
	ccsettingsconsole --buzzer --frequency=1000 --volume=40 --status=disable
	sleep 0.2
	ccsettingsconsole --buzzer --frequency=1000 --volume=40 --status=enable
	sleep 0.2
	ccsettingsconsole --buzzer --frequency=1000 --volume=40 --status=disable
	exit

fi


# stop the current app if currently running
/opt/etc/init.d/StartupGui stop

# Show nice background image
cat /dev/zero > /dev/fb0
fbv /media/usb/logo.png &

for i in `seq 700 100 3000`;
do
	ccsettingsconsole --buzzer --frequency=$i --volume=40 --status=enable
	sleep 0.02
done

for i in `seq 2900 -100 700`;
do
	ccsettingsconsole --buzzer --frequency=$i --volume=40 --status=enable
	sleep 0.02
done

ccsettingsconsole --buzzer --status=disable

fbv /media/usb/logo.png &

#start the watchdog timer back up
/usr/bin/wdt &

# preserve settings file if present
SETTINGS_BACKUP=/media/usb/Commander.conf.bak
cp /opt/Commander/settings/Subsite\ Electronics/Commander.conf $SETTINGS_BACKUP

cd /opt/etc/init.d/
rm StartupGui
sync

cd /opt/

rm -rf lib*
sync

mkdir /opt/lib

rm -rf crosscontrol*
sync

rm -rf /opt/lib/fonts
sync

# install linuX  qt data first stuff
cd /media/usb/
./LinX-Base_VA_device_v2.2.8.sh &
pid_nr=$!
wait $pid_nr
sync

#load the font files
mkdir /opt/lib/fonts
cd /opt/lib/fonts
tar zxvf /media/usb/fonts.tgz -C .
sync

rm -rf /opt/Commander/bin
tar zxvf /media/usb/Commander.tgz -C /
sync

rm /opt/etc/init.d/SplashScreen
sync
#cp /media/usb/SplashScreen /opt/etc/init.d/
#chmod +x /opt/etc/init.d/SplashScreen
#ln -s /opt/etc/init.d/SplashScreen /opt/etc/rc3.d/S04SplashScreen
#sync

if [ -e $SETTINGS_BACKUP ];
then
  mkdir -p /opt/Commander/settings/Subsite\ Electronics/
  cp $SETTINGS_BACKUP /opt/Commander/settings/Subsite\ Electronics/Commander.conf
  rm $SETTINGS_BACKUP
fi

#configure lib files
#sudo ldconfig -C /opt/etc/ld.so.cache 
#sync
 
cd /opt/etc/init.d/
rm -r StartupGui
cp -r /media/usb/StartupGui /opt/etc/init.d/
chmod +x /opt/etc/init.d/StartupGui
sync


# copying ./CAN run file for OS
cp -r /media/usb/canfix.sh /opt
chmod +x /opt/canfix.sh
sync

while ! grep -q '#flexcan' /etc/late-loaded-modules 
do
	echo "not found."

	/opt/canfix.sh
	sync

	sleep 1
done

rm /opt/canfix.sh
sync

# set the local timezone to central to set the clock automatically at the factory
ln -sf /usr/share/zoneinfo/America/Chicago /opt/etc/localtime
sync

cp -r /media/usb/mtcc/images/* /opt/crosscontrol/images/
sync

# Make screen black to indicate installation is finished
cat /dev/zero > /dev/fb0
fbv /media/usb/complete.png &
sync

sleep 5

# beep when finished as well. Remove usb stick
ccsettingsconsole --buzzer --frequency=1000 --volume=40 --status=enable
sleep 1
ccsettingsconsole --buzzer --frequency=1000 --volume=40 --status=disable

#Reboot
reboot

