#!/bin/sh

if [ $# == 3 ]
then
	if [ "$1" = "ls" ]
	then
		if [ ! -d $2 ]
		then
			mkdir $2
		fi
		
		/bin/ls $2 > $3
		exit 0
	fi
fi


#app_from_nfs=y

app_name=335x_dftu
app_jffs2_name=app_"$app_name".jffs2
image_flash_name=flash_"$app_name".bin
nfs_server=192.168.64.250

app_ko="$app_name".ko
app_nfs=/mnt/nfs/app/"$app_ko"
app_flash=/app/"$app_ko"
app_flash_bak=/app/"$app_ko".bak

env_file=/tmp/env.dat
app_disable=`fw_printenv app_disable`
PIDS=`pidof gprs_app`

lsmod | grep "gprs_driver" > /dev/null
if [ $? != 0 ]
then	
	insmod /app/gprs_driver.ko
fi

# sunxi 20191022 added start
pidof cat > /dev/null
if [ $? -eq 1 ]; then
	cat /proc/kmsg >> /tmp/kmsg &
fi
mkdir -p /app/IEC61850
mkdir -p /app/IEC61850/appdata
mkdir -p /app/IEC61850/cfg
stty cols 168
# sunxi 20191024 added end

fw_printenv updapp | grep "$app_jffs2_name"  > /dev/null

if [ $? != 0 ]
then
	setenv updapp "tftp \${loadaddr} $app_jffs2_name;erase 800000 ffffff;cp.b \${loadaddr} 800000 \${filesize}"
fi

fw_printenv updall | grep "$image_flash_name"  > /dev/null

if [ $? != 0 ]
then
	setenv updall "tftp \${loadaddr} $image_flash_name;prot off 0 7ffff;erase 0 ffffff;cp.b \${loadaddr} 0 \${filesize}"
fi

if test -n "`pidof rt_console`"; then
	kill -INT `pidof rt_console`	
  echo "rt_console stop!"   
fi

mode=${1:-start}

if [ "$mode" = "kill" ]
then
	# remove module
	lsmod | grep "$app_name" > /dev/null
	if [ $? == 0 ]
	then
	 rmmod "$app_name"
	fi
#	
	insmod /$app_flash	m_reset=1	
fi

if [ "$mode" = "stop" ]
then
	# remove module
	lsmod | grep "$app_name" > /dev/null
	if [ $? == 0 ]
	then
	 rmmod "$app_name"
	fi
	
	# umount nfs
	mount | grep nfs > /dev/null
	if [ $? == 0 ]
	then
	 umount /mnt/nfs
	fi
	if test -n "`pidof gprs_app`"; then
 		kill -INT `pidof gprs_app`	
	  echo "gprs_app stop!"   
	fi	
	if test -n "`pidof pppd`"; then
 		kill -INT `pidof pppd`	
	  echo "pppd stop!"   
	fi	
fi

if [ "$mode" = "start" ]
then
	# show all kernel log messages
	echo 8 >  /proc/sys/kernel/printk
	
	#
	echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
	echo 1 > /proc/sys/net/ipv4/conf/eth1/arp_ignore
	echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
	
	#time zone
	export TZ="CST-8"

	#env file
	echo ethact=`fw_printenv ethact` > $env_file
	echo ipaddr=`fw_printenv ipaddr` >> $env_file
	echo netmask=`fw_printenv netmask` >> $env_file
	echo gatewayip=`fw_printenv gatewayip` >> $env_file
	
	echo ver_u_boot=`fw_printenv version` >> $env_file
	echo ver_kernel=`cat /proc/kernel_version` >> $env_file
	echo ver_root_fs=`cat /etc/rootfs-release` >> $env_file
	echo auth=`fw_printenv auth 2>/dev/null` >> $env_file
	echo mt_port_on=`fw_printenv mt_port_on 2>/dev/null` >> $env_file
#	echo mt_port_on=1 >> $env_file
	echo pid=`fw_printenv pid 2>/dev/null` >> $env_file
	echo net1mac=`fw_printenv net1mac 2>/dev/null` >> $env_file
	echo net2mac=`fw_printenv net2mac 2>/dev/null` >> $env_file
	echo net3mac=`fw_printenv net3mac 2>/dev/null` >> $env_file
	echo goose_enable=1 >> $env_file
	
	# mount nfs
	if [ "$app_from_nfs" = "y" ]
	then
		mount | grep nfs > /dev/null
		if [ $? != 0 ]
		then
		echo mount -t nfs $nfs_server:/tftpboot/ltib/ /mnt/nfs -o nolock
		mount -t nfs $nfs_server:/tftpboot/ltib/ /mnt/nfs -o nolock
		fi
	fi
		
	# rmmod 
	lsmod | grep "$app_name" > /dev/null
	if [ $? == 0 ]
	then
	 rmmod "$app_name"
	fi
		
	# insmod
	if [ "$app_from_nfs" = "y" ]
	then
		echo Load $app_nfs from nfs:$nfs_server...
		
		# check crc
		crc_target -c $app_nfs
		if [ $? == 0 ]
		then
			echo CRC Check OK!
			insmod $app_nfs
		else
			echo CRC Check Error,Stop Load!
		fi
	else			
		#update file
# sunxi 20191113 注释掉以下脚本
#		if [ -e $app_flash_bak ]
#		then
#			crc_target -c $app_flash_bak
#			if [ $? == 0 ]
#			then
#				echo $app_flash_bak CRC Check OK!
#				echo update file...
#				rm $app_flash
#				mv $app_flash_bak $app_flash
#			else
#				echo CRC Check Error,rm $app_flash_bak!
#				rm $app_flash_bak
#			fi
#		fi

		ifconfig eth1 192.168.2.100
		if [ $app_disable == "1" ]
		then
			echo "app_disable == 1"
			exit
		fi
		echo Load $app_flash from flash...
		
		#check crc
		crc_target -c $app_flash
		if [ $? == 0 ]
		then
			echo CRC Check OK!
			call_server
			if [ $? == 0 ]
				then
				echo "The application was disabled by pc tool!"  
				exit 0
			fi
			
			# sunxi 20191113 备份ko文件
			if [ ! -e $app_flash_bak ]; then
				cp $app_flash $app_flash_bak
			fi
			
			insmod /$app_flash
			
			if test -z "$PIDS"; then
				chmod +x /app/gprs_app
				cp /app/gprs_app /var/
				chmod +x /var/gprs_app
				cd /var
				./gprs_app &
				echo "gprs_app start running"   
			fi

			pidof "ghc1000" > /dev/null
			if [ $? != 0  -a -e "/app/ghc1000" ]
			then
				 cd /app
				 chmod +x ./ghc1000
				 ./ghc1000 &
			fi
			
		else
			echo CRC Check Error,Stop Load!
# sunxi 20191113 恢复ko文件
			if [ -e $app_flash_bak ]; then
				crc_target -c $app_flash_bak
				if [ $? == 0 ]; then
					echo "恢复ko文件, 请不要断电......"
					cp $app_flash_bak $app_flash
					echo "恢复成功，重启系统......"
					sleep 1
					reboot -f
				fi
			fi
		fi
	fi
fi