#!/bin/sh

echo 8192 > /proc/sys/vm/min_free_kbytes
echo 200 > /proc/sys/vm/vfs_cache_pressure

cd /mnt/mtd/modules/extdrv/

NET_CHIP_TYPE=`cat "/etc/init.d/NetChip.dat"`
if [ "$NET_CHIP_TYPE" = "1" ] ; then
	echo "current net is 100M+++++++"
	insmod stmmac.ko macsorts=1 phyid0=2 phyid1=1 phytype=1
else
	insmod stmmac.ko macsorts=1 phyid0=2 phyid1=1
fi

insmod ehci-hcd.ko
insmod ohci-hcd.ko


UpdateStatus=/mnt/mtd/Update.dat
PreUpgradeSh=/upgrade/preupgrade.sh

rm /var/* -rf
mount -t tmpfs tmpfs /var
mkdir /var/run
mkdir /var/lock
rm /nfsdir -rf
mkdir /nfsdir
mount -t tmpfs tmpfs /nfsdir


# 检查文件是否存在和可读
if [ ! -e "$UpdateStatus" -o ! -r "$UpdateStatus" ] ; then
	#为空，表示刚才在升级时，进行了重启。所以需要在次升级
	echo 1 > ${UpdateStatus}
	chmod 666 ${UpdateStatus}
fi

RESULT=`cat "$UpdateStatus"`
if [ "$RESULT" = "1" ] ; then
	ls ${PreUpgradeSh}
	if [ "$?" != 0 ] ; then 
		echo 0 > ${UpdateStatus}
		/mnt/mtd/dep2.sh		
	else
		sh ${PreUpgradeSh}			
	fi		
else
	echo "No need to recovery program from USB"
	/mnt/mtd/dep2.sh
fi

