#!/bin/sh
EXE=guiderII-arm
KERNEL=uImage-guider*
LOG=/data/log/guiderII-arm.log
M3HEX=guiderII.hex
NATIONM3=Guider_N32.bin

/usr/bin/killall rcS
/usr/bin/killall guiderII-arm

rm -f /data/log/finder*
rm -f /data/log/inventor*

rm -rf /opt/backup

rm -rf /data/pics
mkdir /data/pics

echo "Gpio WiFi Power Off" >> $LOG
echo 195 >/sys/class/gpio/export
echo out >/sys/class/gpio/gpio195/direction
echo 0 > /sys/class/gpio/gpio195/value
echo 1 > /sys/class/gpio/gpio195/value

echo 194 >/sys/class/gpio/export
echo out >/sys/class/gpio/gpio194/direction
echo 0 > /sys/class/gpio/gpio194/value

WORKDIR=$(cd `dirname $0`; pwd)
echo "WORKDIR="$WORKDIR >> $LOG

MACHINE_ARCH=armv5tejl

CHECK_ARCH=`uname -m`
if [ "${MACHINE_ARCH}" != "${CHECK_ARCH}" ];then
	echo "Machine architecture error. ARCH="$CHECK_ARCH >> $LOG
	sync
	cat $WORKDIR/failure.bmp > /dev/fb0
	exit 1
fi

VERSION_CONTROL=""
VERSION_KERNEL=""
VERSION_LIBRARY=""
VERSION_SOFTWARE=""

CONTROL=`cat $WORKDIR/version.cfg | grep "control" | tr -s ' ' | cut -d '=' -f2`
if [ ! $CONTROL == "" ] && [ ${#CONTROL} != 0 ]; then
	VERSION_CONTROL=$CONTROL
fi
echo "VERSION_CONTROL="$VERSION_CONTROL >> $LOG

KERNEL_V=`cat $WORKDIR/version.cfg | grep "kernel" | tr -s ' ' | cut -d '=' -f2`
if [ ! $KERNEL_V == "" ] && [ ${#KERNEL_V} != 0 ]; then
	VERSION_KERNEL=$KERNEL_V
fi
echo "VERSION_KERNEL="$VERSION_KERNEL >> $LOG

LIBRARY=`cat $WORKDIR/version.cfg | grep "library" | tr -s ' ' | cut -d '=' -f2`
if [ ! $LIBRARY == "" ] && [ ${#LIBRARY} != 0 ]; then
	VERSION_LIBRARY=$LIBRARY
fi
echo "VERSION_LIBRARY="$VERSION_LIBRARY >> $LOG

SOFTWARE=`cat $WORKDIR/version.cfg | grep "software" | tr -s ' ' | cut -d '=' -f2`
if [ ! $SOFTWARE == "" ] && [ ${#SOFTWARE} != 0 ]; then
	VERSION_SOFTWARE=$SOFTWARE
fi
echo "VERSION_SOFTWARE="$VERSION_SOFTWARE >> $LOG

if [ -f $WORKDIR/pointercal ]; then
    echo "pointercal to board" >> $LOG
    cp -vf $WORKDIR/pointercal /opt/tslib-1.4-none/etc/
    sync
fi

if [ -f $WORKDIR/shadow ]; then
    echo "shadow to board" >> $LOG
    cp -vf $WORKDIR/shadow /etc/
    sync
fi

echo "dispaly start on lcd" >> $LOG
if [ -f $WORKDIR/start.bmp ]; then
    cat $WORKDIR/start.bmp > /dev/fb0
fi

if [ -f $WORKDIR/wpa_supplicant.conf ]; then
    cp -f $WORKDIR/wpa_supplicant.conf /opt
fi

if [ ! -d /data/log ]; then
	mkdir /data/log
fi

if [ ! -d /data/pics ]; then
	mkdir /data/pics
fi

if [ ! -d /data/update ]; then
	mkdir /data/update
fi

if [ ! -d /media ]; then
	mkdir /media
fi

if [ ! -d /PROGRAM ]; then
	mkdir /PROGRAM /PROGRAM/control /PROGRAM/kernel /PROGRAM/library /PROGRAM/software
fi

echo "FlashForge Cloud Switch OFF" >> $LOG
sed -i '1c OFF' /opt/flashforge/cloud-ff
sync

if [ -f $WORKDIR/mac_addrs ]; then
	echo "Ethernet MAC Set" >> $LOG
	find $WORKDIR/mac_addrs -type f -print0 | xargs -0 dos2unix
	Ethernet_MAC=`sed -n '/ifconfig eth0 hw ether/p' /etc/init.d/rcS | cut -d ' ' -f5 | grep -E '[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}'`
	echo "Ethernet_MAC="$Ethernet_MAC >> $LOG

	if [ ! $Ethernet_MAC ]; then
		MAC_ADDR_H=`sed -n '1p' $WORKDIR/mac_addrs`
		echo "None MAC MAC_ADDR_H="$MAC_ADDR_H >> $LOG
	
		if [ $MAC_ADDR_H ]; then
			sed -i '/ifconfig/d' /etc/init.d/rcS
			sed -i '3a ifconfig eth0 hw ether '"$MAC_ADDR_H" /etc/init.d/rcS
			sync
		else
			echo "None MAC Address or \"mac_addrs\" file" >> $LOG
		
	#		if [ -f $WORKDIR/no-mac.bmp ]
	#		then
	#			cat $WORKDIR/no-mac.bmp > /dev/fb0
	#		fi
		
	#		sleep 1
	#		halt -f
		fi
	
		MAC_ADDR_R=`sed -n '2p' $WORKDIR/mac_addrs`
		echo "None MAC MAC_ADDR_R="$MAC_ADDR_R >> $LOG
	
		if [ "$MAC_ADDR_R" \> "$MAC_ADDR_H" ]; then
			machex=`awk 'BEGIN{FS=OFS=":";$0=ARGV[1];$NF=sprintf("%.2X",("0x"$NF)+1);print}' $MAC_ADDR_H`
			echo "machex="$machex >> $LOG
			sed -i '1c '"$machex" $WORKDIR/mac_addrs
			sync
		else
			rm -f $WORKDIR/mac_addrs
			sync
		fi
	
		find /etc/init.d/rcS -type f -print0 | xargs -0 dos2unix
		echo "Ethernet MAC Set Success" >> $LOG
	else
		MAC_ADDR_H=`sed -n '1p' $WORKDIR/mac_addrs`
		echo "Has MAC MAC_ADDR_H="$MAC_ADDR_H >> $LOG
	
		MAC_ADDR_R=`sed -n '2p' $WORKDIR/mac_addrs`
		echo "Has MAC MAC_ADDR_R="$MAC_ADDR_R >> $LOG
	
		if [ "$Ethernet_MAC" \> "$MAC_ADDR_H" ]; then
			machex=`awk 'BEGIN{FS=OFS=":";$0=ARGV[1];$NF=sprintf("%.2X",("0x"$NF)+1);print}' $Ethernet_MAC`
			echo "machex="$machex >> $LOG
			sed -i '1c '"$machex" $WORKDIR/mac_addrs
			sync
		fi
		if [ "$Ethernet_MAC" \> "$MAC_ADDR_R" ]; then
			rm -f $WORKDIR/mac_addrs
			sync
		fi
	fi
fi

echo "RSA cryptographic keys" >> $LOG
sed -i '1c OFF' /opt/flashforge/cloud-username-pin
sed -i '4d' /opt/flashforge/cloud-username-pin
sync

if [ ! -f /etc/ssl/key.priv ]; then
	echo 'abcde' | ssh-keygen -t rsa -b 2048 -f /etc/ssl/key.priv
fi

if [ ! -f /etc/ssl/key.pub ]; then
	ssh-keygen -e -m PEM -f /etc/ssl/key.priv > /etc/ssl/key.pub
fi

sync

if [ -f $WORKDIR/$KERNEL ] && [ ! $VERSION_KERNEL == "" ]; then
	UIMAGE=`ls -1t $WORKDIR/$KERNEL | head -n 1`
	if [ -f $UIMAGE ]; then
		VER_KERNEL=`ls /PROGRAM/kernel | head -n 1`
		if [ ! $VER_KERNEL ] || [ ! $VER_KERNEL == $VERSION_KERNEL ] || [ $WORKDIR == "/mnt" ]; then
			if [ $WORKDIR == "/mnt" ]; then
				echo "rm -rf /PROGRAM/kernel/*" >> $LOG
				rm -rf /PROGRAM/kernel/*
			fi
			
			echo "create /PROGRAM/kernel/"$VERSION_KERNEL >> $LOG
			mkdir -p /PROGRAM/kernel/$VERSION_KERNEL
			cp -vf $UIMAGE /PROGRAM/kernel/$VERSION_KERNEL
			sync
			
			if [ ! -d /tmp/disk1 ]; then
				mkdir /tmp/disk1
			fi
			mount -t vfat -o codepage=936,iocharset=utf8 /dev/mmcblk0p1 /tmp/disk1
			if [ $? -ne 0 ]; then
				echo "mount mmcblk0p1 failed" >> $LOG
				sync
				exit -1
				halt -f
			fi
			
			echo "update uImage "$UIMAGE >> $LOG
			rm -f /tmp/disk1/uImage
			cp -vf /PROGRAM/kernel/$VERSION_KERNEL/* /tmp/disk1/uImage
			sync
			echo "update uImage success" >> $LOG

			umount /tmp/disk1
			rm -r /tmp/disk1
			
			cd /PROGRAM/kernel
			ls | grep -v $VERSION_KERNEL | xargs rm -rf
		fi
	fi
fi

sync

if [ -f $WORKDIR/auto_run.sh ]; then
	AUTORUN=$WORKDIR/auto_run.sh
#	SRCMD5=`md5sum $AUTORUN | cut -d ' ' -f 1`
#	DSTMD5=`md5sum /opt/auto_run.sh | cut -d ' ' -f 1`
#	echo "SRC_AUTORUN_MD5="$SRCMD5 >> $LOG
#	echo "DST_AUTORUN_MD5="$DSTMD5 >> $LOG

#	if [ "$SRCMD5" != "$DSTMD5" ]; then
		echo "update auto_run.sh "$AUTORUN >> $LOG
#		while [ "$SRCMD5" != "$DSTMD5" ];do
			rm -f /opt/auto_run.sh
			cp -vf $AUTORUN /opt
			sync
			chmod a+x /opt/auto_run.sh
#			DSTMD5=`md5sum /opt/auto_run.sh | cut -d ' ' -f 1`
#			echo "SRC_AUTORUN_MD5="$SRCMD5 >> $LOG
#			echo "DST_AUTORUN_MD5="$DSTMD5 >> $LOG
#		done
		echo "update auto_run.sh success" >> $LOG
#	fi
fi

sync

if [ -f $WORKDIR/$EXE ] && [ ! $VERSION_SOFTWARE == "" ]; then
	ARM=$WORKDIR/$EXE
	
	VER_SOFTWARE=`ls /PROGRAM/software | head -n 1`
	if [ ! $VER_SOFTWARE ] || [ ! $VER_SOFTWARE == $VERSION_SOFTWARE ] || [ $WORKDIR == "/mnt" ]; then
		if [ $WORKDIR == "/mnt" ]; then
			echo "rm -rf /PROGRAM/software/*" >> $LOG
			rm -rf /PROGRAM/software/*
		fi
		
		echo "create /PROGRAM/software/temp" >> $LOG
		mkdir -p /PROGRAM/software/temp

		echo "update EXE "$EXE >> $LOG
		rm -f /data/log/core*
		rm -f /data/log/strace*

		cp -vf $ARM /PROGRAM/software/temp
		sync
		chmod a+x /PROGRAM/software/temp/$EXE

		mv /PROGRAM/software/temp /PROGRAM/software/$VERSION_SOFTWARE
		
	#	cd /PROGRAM/software
	#	ls | grep -v $VERSION_SOFTWARE | xargs rm -rf

		cd /data/log
		rm -f `ls | grep -v $EXE`

		echo "update EXE success" >> $LOG
	fi
fi

sync

if [ -f $WORKDIR/update/env.cfg ] && [ ! $VERSION_LIBRARY == "" ]; then
	VER_LIBRARY=`ls /PROGRAM/library | head -n 1`
	if [ ! $VER_LIBRARY ] || [ ! $VER_LIBRARY == $VERSION_LIBRARY ] || [ $WORKDIR == "/mnt" ]; then
		if [ $WORKDIR == "/mnt" ]; then
			echo "rm -rf /PROGRAM/library/*" >> $LOG
			rm -rf /PROGRAM/library/*
		fi
		
		echo "create /PROGRAM/library/temp" >> $LOG
		mkdir -p /PROGRAM/library/temp
		
		cp -vf $WORKDIR/update/env.cfg /PROGRAM/library/temp
		sync

		if [ -f $WORKDIR/update/fonts/code2000.ttf ]; then
			echo "update fonts" >> $LOG
			cp -vPrf $WORKDIR/update/fonts /PROGRAM/library/temp
			sync
			echo "update code2000.ttf success" >> $LOG
		fi

		sync

		if [ -f $WORKDIR/update/arm-jsoncpp.tar ]; then
			ARM_JSONCPP=$WORKDIR/update/arm-jsoncpp.tar
			echo "update arm-jsoncpp" >> $LOG
			echo "tar arm-jsoncpp.tar" >> $LOG 
			rm -rf /PROGRAM/library/temp/arm-jsoncpp
			tar -xvf $WORKDIR/update/arm-jsoncpp.tar -C /PROGRAM/library/temp
			echo "tar arm-jsoncpp.tar ret=$?" >> $LOG
			if [ $? == 0 ] || [ $? == 141 ]; then
				echo "update arm-jsoncpp success" >> $LOG
			else
				echo "update arm-jsoncpp failure" >> $LOG
				sync
				cat $WORKDIR/failure.bmp > /dev/fb0
				exit 1
			fi
			sync
		fi

		sync

		if [ -f $WORKDIR/update/camera.tar ]; then
			CAMERA=$WORKDIR/update/camera.tar
			echo "update camera" >> $LOG
			echo "tar camera.tar" >> $LOG
			tar -xvf $WORKDIR/update/camera.tar -C /PROGRAM/library/temp
			echo "tar camera.tar ret=$?" >> $LOG
			if [ $? == 0 ] || [ $? == 141 ]; then
				echo "update camera success" >> $LOG
			else
				echo "update camera failure" >> $LOG
				sync
				cat $WORKDIR/failure.bmp > /dev/fb0
				exit 1
			fi
			sync
		fi

		sync

		if [ -f $WORKDIR/update/openssl-1.0.2d-none.tar ]; then
			OPENSSLLIB=$WORKDIR/update/openssl-1.0.2d-none.tar
			echo "update openssllib" >> $LOG
			echo "tar openssl-1.0.2d-none.tar" >> $LOG
			tar -xvf $WORKDIR/update/openssl-1.0.2d-none.tar -C /PROGRAM/library/temp
			echo "tar openssl-1.0.2d-none.tar ret=$?" >> $LOG
			if [ $? == 0 ] || [ $? == 141 ]; then
				echo "update openssllib success" >> $LOG
			else
				echo "update openssllib failure" >> $LOG
				sync
				cat $WORKDIR/failure.bmp > /dev/fb0
				exit 1
			fi
			sync
		fi
		
		
		if [ -f $WORKDIR/update/curl.tar ]; then
			OPENSSLLIB=$WORKDIR/update/curl.tar
			echo "update curl" >> $LOG
			echo "tar curl.tar" >> $LOG
			tar -xvf $WORKDIR/update/curl.tar -C /PROGRAM/library/temp
			echo "tar curl.tar ret=$?" >> $LOG
			if [ $? == 0 ] || [ $? == 141 ]; then
				echo "update curl success" >> $LOG
			else
				echo "update curl failure" >> $LOG
				sync
				cat $WORKDIR/failure.bmp > /dev/fb0
				exit 1
			fi
			sync
		fi

		mv /PROGRAM/library/temp /PROGRAM/library/$VERSION_LIBRARY
	fi

#	cd /PROGRAM/library
#	ls | grep -v $VERSION_LIBRARY | xargs rm -rf
fi

sync

HEX=""
HEX_NAME=""
if [ -f $WORKDIR/$M3HEX -a -f $WORKDIR/ISPFinderPlusISP ]; then
	HEX=$WORKDIR/$M3HEX
	HEX_NAME=$M3HEX
fi

echo "HEX="$HEX >> $LOG
echo "HEX_NAME="$HEX_NAME >> $LOG

if [ -f $HEX ] && [ $HEX_NAME ]; then
	VER_CONTROL=`ls /PROGRAM/control | head -n 1`
	if [ ! $VER_CONTROL ] || [ ! $VER_CONTROL == $VERSION_CONTROL ] || [ $WORKDIR == "/mnt" ]; then
#		if [ $WORKDIR == "/mnt" ]; then
			echo "rm -rf /PROGRAM/control/*" >> $LOG
			rm -rf /PROGRAM/control/*
#		fi
		
		echo "update HEX "$HEX_NAME >> $LOG
		echo "create /control/"$VERSION_CONTROL >> $LOG
		mkdir -p /control/$VERSION_CONTROL

		cp -f $HEX /control/$VERSION_CONTROL
		sync

		chmod a+x $WORKDIR/ISPFinderPlusISP
		chmod a+x $WORKDIR/NationsCommand
		cp -f $WORKDIR/ISPFinderPlusISP /control/$VERSION_CONTROL
		cp -f $WORKDIR/NationsCommand /control/$VERSION_CONTROL
		cp -f $WORKDIR/$NATIONM3 /control/$VERSION_CONTROL
		cp -f $WORKDIR/control.sh /opt
		sync

		echo "burn M3 firmware..." >> $LOG
		/control/$VERSION_CONTROL/ISPFinderPlusISP /control/$VERSION_CONTROL/$HEX_NAME >> $LOG
		sync

		if [ $? == 0 ]; then
			echo "update $HEX_NAME success" >> $LOG
			mkdir -p /PROGRAM/control
			cp -rf /control/$VERSION_CONTROL /PROGRAM/control/
			sync
			sleep 30
			rm -rf /control
		else
			echo "update $HEX_NAME failure" >> $LOG
			sync
			cat $WORKDIR/failure.bmp > /dev/fb0
			exit 1
		fi

		cd /PROGRAM/control
		ls | grep -v $VERSION_CONTROL | xargs rm -rf
	fi
fi

sync

cp -f $WORKDIR/ffstartup* /PROGRAM
sync

chmod a+x /PROGRAM/ffstartup-arm

cp -f $WORKDIR/Test*.gx /data
sync

cp -f $WORKDIR/Test*.g /data
sync

sleep 3

if [ -d /opt/finder-plus ]; then
	echo "remove /opt/finder-plus" >> $LOG
	rm -rf /opt/finder-plus
fi

sync

echo "dispaly on lcd" >> $LOG
if [ -f $WORKDIR/complete.bmp ]; then
    cat $WORKDIR/complete.bmp > /dev/fb0
fi

rm -rf /data/update/flashforge_init.sh

echo "export tslib" >> $LOG

#if [ ${#VERSION_LIBRARY} == 0 ]; then
	VERSION_LIBRARY=`ls /PROGRAM/library | head -n 1`
#fi

echo "VERSION_LIBRARY="$VERSION_LIBRARY >> $LOG
sync

export QT_QWS_FONTDIR=/PROGRAM/library/$VERSION_LIBRARY/fonts
export LD_LIBRARY_PATH=/opt/tslib-1.4-none/lib:$LD_LIBRARY_PATH
if [ -f $WORKDIR/play ]; then
	echo "play complete music" >> $LOG
	sync
	chmod a+x $WORKDIR/play
#	$WORKDIR/play -qws
fi

rm -rf /data/update/*

halt -f
