#!/bin/sh
echo "**********************************"
echo "*******    Update run...	******** "
echo "**********************************"

gpio dirout 80
gpio dirout 81
gpio dirin 82


cd /home/CAPELEC/DATA/update/dirtmp/
killall 1830
cp 1830 /usr/bin/

VERSION_BACK=`cat "/etc/info" | grep "VERSION" | cut -d"=" -f2 `


if [ $? = 0 ]; then
	echo "update OK"	
	echo "export VERSION=V1.08" > /etc/info
	echo "export DATE=13/07/2016" >> /etc/info
	echo "export INFORMATION=" >> /etc/info

gpio clear 80

sleep 1
gpio set 80

sleep 1
gpio clear 80
sleep 1

rm /etc/init.d/S54openvpn
mv /etc/openvpn/client.conf clientold.conf

cp root /var/spool/cron/crontabs/
cp crontabs.1358 /var/spool/cron/


cp SAVE_PARAM.cgi /var/www/CONFIGURATION/
cp SAVE_PARAM.cgi /var/wwwcapelec/CONFIGURATION/

killall 1830_prod
cp 1830_prod /usr/bin/

SERIAL=`cat "/home/CAPELEC/DATA/PARAM.txt" | grep "#020#" | cut -d"#" -f3 | tr -d " "`
echo "SERIAL=$SERIAL"
if [ -z $SERIAL ]; then
	echo "ERROR: SERIAL is null"	
	exit
fi

sed "s/200/$SERIAL/" /etc/board_id > /etc/board_id_TMP

cp /etc/board_id_TMP  /etc/board_id

#pour parametrage auto BT
#sed "s/RS232/BLUETOOTH/" /home/CAPELEC/DATA/PARAM.txt > /home/CAPELEC/DATA/PARAMTMP.txt
#cp /home/CAPELEC/DATA/PARAMTMP.txt  /home/CAPELEC/DATA/PARAM.txt


BRAND=`cat "/home/CAPELEC/DATA/PARAM.txt" | grep "#005#" | cut -d"#" -f3 | tr -d "1 "`
echo "BRAND=$BRAND"
if [ -z $BRAND ]; then
	echo "ERROR: BRAND is null"	
	exit
fi
#SERIAL
SERIAL=`cat "/home/CAPELEC/DATA/PARAM.txt" | grep "#020#" | cut -d"#" -f3 | tr -d " "`
echo "SERIAL=$SERIAL"
if [ -z $SERIAL ]; then
	echo "ERROR: SERIAL is null"	
	exit
fi
#MODEL
MODEL="IS010"
echo "MODEL=$MODEL"

NAME_FILE=$BRAND-$MODEL-$SERIAL.txt

#DATE
DATE=`tail "/home/CAPELEC/DATA/log/log.txt" | grep -m 1 "SEND:" | cut -b 6-13`


echo -e "DATE=$DATE HEURE=`date '+%H:%M:%S'` VERSIONBACKUP=$VERSION_BACK VERSIONUPDATE=V1.08 MAJ OK LOCALY \r" >>  /home/CAPELEC/DATA/log/$NAME_FILE


echo "MISE A JOUR OK LE SYSTEME VA s'arreter DANS 3 SECS"


touch /mnt/usbflash/done_Config_BT_CAPELEC_$NAME_FILE



else
	echo "update NOK!!!!"	
	VERSION=`cat /etc/info | grep VERSION`
	DATE=`cat /etc/info | grep DATE`
	
	echo "$VERSION" > /etc/info
	echo "$DATE" >> /etc/info
	echo "export INFORMATION=Update Error" >> /etc/info
fi


			
#halt pour MAJ localy
#halt
#reboot pour MAJ A DISTANCE
reboot


