#!/bin/sh
echo "Doing post-install ..." > /dev/console

#
# NOTE: to handle package built by different tool chain issue
#       by andy 2012/01/30
#
# start
MONTAVISTA_LAST_REVISION="34713"
CUR_REVISION="`cat /zyxel/mnt/info/revision`"
PROGRESS_LOG="/tmp/fwupgrade/fwlog"
RM_PKG_LIB_FLAG="/tmp/remove_package_libraries"


# check firmware revision
# NOTE: 
#	Because the revision information in NSA210 is stored in Flash, which will updated after writing firmware to flash,
#	we use "reset flag" generated in pre-install to determine if package libraries need to be removed here instead of
#	using "firmware revision"
#echo "*** current revision: ${CUR_REVISION}"				> /dev/console
#echo "*** montavista last revision: ${MONTAVISTA_LAST_REVISION}"	> /dev/console
#if [ ${CUR_REVISION} -le ${MONTAVISTA_LAST_REVISION} ]; then
if [ -f ${RM_PKG_LIB_FLAG} ]; then
	HTTPD_EXIST="`ps | grep httpd`"
	if [ "${HTTPD_EXIST}" != "" ]; then
		echo "100" > ${PROGRESS_LOG}
		sleep 1
	fi

	echo "*** clearing all libraries in \"/usr/local/zy-pkgs/lib\""	> /dev/console
	rm -rf /usr/local/zy-pkgs/lib/*					> /dev/console 2>&1
fi

cat /proc/mounts > /dev/console
configok=`cat /proc/mounts|grep /etc/zyxel|grep /dev/mtdblock3`
if [ "${configok}" == "" ]; then
	echo "fix ITS#53371: after NSA221 reboot config is disappear" > /dev/console
	echo "flash_eraseall /dev/mtd3" > /dev/console
	flash_eraseall /dev/mtd3
	echo "mkdir -p -m 777 /mnt/zyxel" > /dev/console
	mkdir -p -m 777 /mnt/zyxel
	echo "mount -t jffs2 /dev/mtdblock3 /mnt/zyxel" > /dev/console
	mount -t jffs2 /dev/mtdblock3 /mnt/zyxel
	echo "Check if there is /etc/zyxel and copy to flash"
	if [ -d /etc/zyxel ]; then
		echo "cd /etc/zyxel/" > /dev/console
		cd /etc/zyxel/
		echo "cp -a `ls|grep -Ev \"^zy-pkgs$\"` /mnt/zyxel/" > /dev/console
		cp -a `ls|grep -Ev "^zy-pkgs$"` /mnt/zyxel/
		echo "mkdir -m 777 /mnt/zyxel/zy-pkgs" > /dev/console
		mkdir -m 777 /mnt/zyxel/zy-pkgs
		echo "cd /" > /dev/console
		cd /
	else
		echo "No /etc/zyxel"
	fi
fi
cat /proc/mounts > /dev/console
echo "sync" > /dev/console
sync
echo "umount /mnt/zyxel/" > /dev/console
umount /mnt/zyxel/

echo " ... post-install finishes!" > /dev/console
exit 0
