#!/bin/sh
appOTAFile="/etc/AppManage/ManageAPPsh/true"
appManageOTADir="/etc/AppManage/ManageAPPsh/AppManage"
appManageDir="/etc/AppManage/ManageAPPsh/"
if [ `whoami` != "root" ];then  
    echo "Error: This command has to be run under the root user." 
    exit 1
fi

if [ -f "$appOTAFile" ] ; then
	cp -rf $appManageOTADir/* $appManageDir../
	rm -f $appOTAFile
	echo "update AppManage success"
fi

if [ -f $appManageDir/../AliCloudAppManage ] ; then
	rm -rf $appManageOTADir
	rm $appOTAFile
	echo "update AppManage success"
fi

passengerFlowApp="/home/firmware/PassengerFlow"
appmanagerApp="/home/firmware/AliCloudAppManage"
libmovesense="/home/firmware/libMoveSenseCamera.so"

if [ -f "$passengerFlowApp" ] ; then
	cp -rf $passengerFlowApp /home/root/APP/
	echo "update passengerFlowApp success"
fi

if [ -f "$appmanagerApp" ] ; then
	cp -rf $appmanagerApp /home/root/
	echo "update appmanagerApp success"
fi

if [ -f "$libmovesense" ] ; then
	cp -rf $libmovesense /lib/
	echo "update libmovesense success"
fi

echo "nameserver 119.29.29.29" > /etc/resolv.conf
echo "nameserver 223.6.6.6" >> /etc/resolv.conf

if [ -f "/etc/TZSET" ];
then
	source /etc/TZSET
fi

customerType=$(cat /home/Param/customerType)
if [ "$customerType" = 2 ];then
	./xzUpgrade.sh
	sleep 3
	./mrd_search
	sleep 3
fi

curMac=$(ifconfig | awk 'NR==1 {print $5}')

if [[ "${curMac:0:2}" != '4C' ]];then
      mac=$(cat /etc/network/interfaces | grep hwaddress | awk 'END{print $3}')
      ifconfig eth0 down
      sleep 1
      ifconfig eth0 hw ether "$mac"
      sleep 1
      ifconfig eth0 up
fi

/usr/sbin/avahi-autoipd -k eth0
/etc/init.d/networking restart
monitorDHCP &

/bin/gst-inspect-1.0
date >> rtsp.log
./rtspServer >> rtsp.log 2>&1 &

export LD_PRELOAD=/lib/preloadable_libiconv.so
sysctl -w vm.min_free_kbytes=20480

cd /home/root/APP
chmod +x LocalApp
chmod +x PassengerFlow*
cd /home/root
chmod +x AliCloudAppManage

grep -c "oIREaQC2Ha40A" "/etc/passwd"
if [ $? -eq 1 ]
then
    #echo "not find"
    #sed -i '/^root/s/:[^:]*:/:oIREaQC2Ha40A:/' /etc/passwd
    sync
fi

./AliCloudAppManage >> stdout-stderr.log 2>&1 &
./watchDog AliCloudAppManage PassengerFlow




