#!/bin/sh

reboot_flag=`cat /tmp/reboot_flag`
download_complete_flag=`cat /tmp/download_complete_flag`
second_time_pre_upgrade=`cat /tmp/second_time_pre_upgrade`

# stop modules
/etc/init.d/monitor stop
/etc/init.d/sys_monitor stop
/etc/init.d/nginx stop
/etc/init.d/dnsmasq stop
/etc/init.d/improxy stop
/etc/init.d/cmxddns stop
/etc/init.d/ippool stop
/etc/init.d/tmngtd stop
/etc/init.d/cron stop
/etc/init.d/loggerd stop
/etc/init.d/queueventd stop
/etc/init.d/pptpd stop
/etc/init.d/xl2tpd stop
/etc/init.d/online stop
[ -e /etc/init.d/ifstat-mini ] && {
	/etc/init.d/ifstat-mini stop
}
[ -e /etc/init.d/ipstat ] && {
	/etc/init.d/ipstat stop
}
# undo stop gatewayd
/etc/init.d/dnsproxy stop
/etc/init.d/tddpd stop
/etc/init.d/sysntpd stop
/etc/init.d/drop_caches stop
[ -e /etc/init.d/dpi ] && {
	/etc/init.d/dpi stop
	rm -rf /tmp/database/database
}
[ -e /etc/init.d/ips ] && {
	/etc/init.d/ips stop
}
[ -e /etc/init.d/url_filter ] && {
	/etc/init.d/url_filter stop
	rm -rf /tmp/url_database/url_dataset_fts4.db
}

# download_complete_flag=0时，关闭uhttpd、omada_server、netifd会下载不了固件
if [ ${download_complete_flag} -eq 1 ] && [ ${second_time_pre_upgrade} -eq 0 ];
then
	/etc/init.d/uhttpd stop
	[ -e "/etc/init.d/zzomada_server" ] && {
		/etc/init.d/zzomada_server stop
	}
	[ -e "/etc/init.d/ecs_frame" ] && {
		/etc/init.d/ecs_frame stop
	}
	killall netifd

	# down eth interface
	for ifname in `ifconfig -a | grep '^[a-z]' | awk '{print $1}'`
	do
		ifconfig $ifname down
	done
fi

# kill tasks
killall online_check
killall load_balance
killall balance_loop
killall klogd
killall queueventd
# reboot_flag=1时，杀死lua会中断升级
if [ ${reboot_flag} -eq 0 ];
then
	killall lua
fi
killall vnete

# umount userconfig jffs2 to avoid read/write when upgrading.
umount /tmp/userconfig
if [ -d /tmp/agileconfig ];
then
	umount /tmp/agileconfig
	rm -rf /tmp/agileconfig
fi

if [ ${download_complete_flag} -eq 1 ];
then
	# close hnat
	/etc/init.d/hwnat stop
	rm -rf /tmp/hnat_start_complete

	# free memory in kernel.
	anete iptables -t mangle -I PREROUTING 1 -j DROP
	/etc/init.d/sfe stop
	vnete conntrack -F
	conntrack -F

	for ifname in `ifconfig -a | grep 'ra' | awk '{print $1}'`
	do
		if ( echo $ifname | grep -q 'ra' ); then
			ifconfig $ifname down
		fi
	done
fi

echo 3 > /proc/sys/vm/drop_caches
echo 0 > /tmp/pre_upgrade_complete
if [ ${download_complete_flag} -eq 1 ]; then
	# change mtdroot dev(31:4) to mtdram dev(31:14)
	[ -e /lib/modules/$(uname -r)/mtdram.ko ] && {
		# create mtdram block device
		tsize=$(cat /proc/mtd | grep '"rootfs"' | awk '{print("0x"$2)}')
		esize=$(cat /proc/mtd | grep '"rootfs"' | awk '{print("0x"$3)}')
		insmod mtdram.ko total_size=$(($tsize>>10)) erase_size=$(($esize>>10))
		echo insmod mtdram.ko total_size=$(($tsize>>10)) erase_size=$(($esize>>10)) > /dev/console

		# copy binary data of mtdroot dev to mtdram dev
		rootid=$(cat /proc/mtd | grep '"rootfs"' | sed 's/mtd\(.*\):.*/\1/g')
		ramid=$(cat /proc/mtd | grep '"mtdram"' | sed 's/mtd\(.*\):.*/\1/g')
		rootblkid=$(ls /dev/mtdblock${rootid} -l | awk '{print$5}' | sed 's/,//g')
		ramblkid=$(ls /dev/mtdblock${ramid} -l | awk '{print$5}' | sed 's/,//g')
		dd if=/dev/mtdblock${rootid} of=/dev/mtdblock${ramid} bs=$(printf '%d' $esize)

		# mount a tmp dir of squashfs from mtdram dev
		mkdir -p /tmp/.mtdram
		mount -t squashfs /dev/mtdblock${ramid} /tmp/.mtdram

		#ubi block
		rootdev=$(cat /proc/cmdline | sed "s/^.*root=\([^[:space:]]*\).*$/\1/g")
		[ "${rootdev:0:13}" == "/dev/ubiblock" ] && {
			rootid=$(ls ${rootdev} -l | awk '{print$6}')
			rootblkid=$(ls ${rootdev} -l | awk '{print$5}' | sed 's/,//g')
		}
		# bind tmp dir of squashfs to rootfs
		echo "squashfs,rootdev=${rootblkid}:${rootid},ramdev=${ramblkid}:${ramid}" > /sys/mtdram/mtdram

		count=0
		cycles=10
		while [ $count -lt $cycles ];
		do
			status=`busybox cat /sys/mtdram/mtdram`
			if [ -n "$status" ];
			then
				break
			fi
			echo "squashfs,rootdev=${rootblkid}:${rootid},ramdev=${ramblkid}:${ramid}" > /sys/mtdram/mtdram
			let count++
			echo "change mtdroot to mtdram failed $count" 2>&1 1>/dev/console
			sleep 2
		done

		if [ $count -ge $cycles ];
		then
			echo "change mtdroot to mtdram failed > $cycles, will reboot!" 2>&1 1>/dev/console
			/sbin/reboot -f
		fi
		
		echo 1 > /tmp/pre_upgrade_complete
	}

fi


# echo debug info
busybox ps > /dev/console
cat /proc/meminfo > /dev/console
