#!/bin/sh
OEM_CONF_ROOT="/etc/config/oem"

WWW_ROOT="/var/www"
WWW_CSS="${WWW_ROOT}/css"
WWW_IMG="${WWW_ROOT}/images"

case "$1" in
	start)
		#create symbol link to /WebUI/CAcertSrv.pem, this file is needed for the smtp ssl.
		ln -s /etc/default/CAcertSrv.pem /WebUI/CAcertSrv.pem
		#check brand version is ACTi or NB, then symbol link to path correctly.
        ln -s /WebUI $WWW_ROOT
	  ln -fs ThirdPartySoftwareLicence_AC.html /WebUI/licenses/ThirdPartySoftwareLicence.html
		cp /etc/default/firmware_version.conf /etc/default/firmware_version_orig.conf
		if [ -d $OEM_CONF_ROOT ]; then
			. $OEM_CONF_ROOT/oem.conf
			## found the oem configuration file. change the firmware to NB
			if [ "$FW_VER_PREFIX" = "" -a "$FW_VER_POSTFIX" = "" ]; then
				sed -ie "s/-AC/-NB/" /etc/default/firmware_version.conf
			else
				sed -ie "s/A1D-500-/$FW_VER_PREFIX/" /etc/default/firmware_version.conf
				sed -ie "s/-AC/$FW_VER_POSTFIX/" /etc/default/firmware_version.conf	
			fi
			cp -af ${OEM_CONF_ROOT}/update.css ${WWW_CSS}
			cp -af ${OEM_CONF_ROOT}/*.png ${WWW_IMG}
			## Special handler
			if [ "$COMPANY_NAME" != "" ]; then
				## Analysis for LevelOne
				if [ "$COMPANY_NAME" == "Level1" ]; then
					COMPANY_NAME='LevelOne'
				fi
			fi
			## Patch Customer's ActiveX to runtime ActiveX
			if [ -e "/WebUI/control/NB/nvA1Media_${COMPANY_NAME}.diff" ]; then
				echo "OEM: patch file NB/nvA1Media_${COMPANY_NAME}.diff"
				bspatch /WebUI/control/nvA1Media.cab /WebUI/control/NB/nvA1Media.cab /WebUI/control/NB/nvA1Media_$COMPANY_NAME.diff
				ln -fs ThirdPartySoftwareLicence_${COMPANY_NAME}.html /WebUI/licenses/ThirdPartySoftwareLicence.html
			else
				echo "OEM: patch file NB/nvA1Media_NB.diff"
				bspatch /WebUI/control/nvA1Media.cab /WebUI/control/NB/nvA1Media.cab /WebUI/control/NB/nvA1Media_NB.diff
				ln -fs ThirdPartySoftwareLicence_NB.html /WebUI/licenses/ThirdPartySoftwareLicence.html
			fi
			rm /WebUI/control/nvA1Media.cab
			ln -s NB/nvA1Media.cab /WebUI/control/nvA1Media.cab
			## TODO: update the oem.conf to /etc/default/default.conf
			if [ -s ${OEM_CONF_ROOT}/oem_devcap ]; then
				acti_logger `grep DEVCAP_REV_LOG ${OEM_CONF_ROOT}/oem_devcap`
				acti_logger `grep DEVCAP_UUID ${OEM_CONF_ROOT}/oem_devcap`
			fi
		fi
		## this is AC brand, assume the firmware version is AC already. do nothing
		;;

	save)
		if [ -f $2 ]; then
			## there is a oem tar ball
			if [ -d ${OEM_CONF_ROOT} ]; then
				rm -rf ${OEM_CONF_ROOT}
			fi
			mkdir -p ${OEM_CONF_ROOT}
			if [ ! -d /tmp/oem ]; then
				mkdir -p /tmp/oem
			else
				rm -f /tmp/oem/*
			fi
			cd /tmp/oem
			tar zxf $2
			if [ -s /tmp/oem/oem.conf ]; then
				chmod 755 *
				cp /tmp/oem/oem.conf ${OEM_CONF_ROOT}
				dos2unix ${OEM_CONF_ROOT}/oem.conf
				if [ -s /tmp/oem/oem_devcap ]; then
					cp /tmp/oem/oem_devcap ${OEM_CONF_ROOT}
					dos2unix ${OEM_CONF_ROOT}/oem_devcap
				fi
				if [ -s /tmp/oem/update.css ]; then
					cp /tmp/oem/update.css ${OEM_CONF_ROOT}
					cp /tmp/oem/*.png ${OEM_CONF_ROOT}
					sync
				fi
				##Update customize serail_num, model_name, model_desc if indication
				#OEM_SERIAL_NUM
				if [ ! -z "$3" ]; then
				sed -ie "s/OEM_SERIAL_NUM=.*/OEM_SERIAL_NUM='$3'/" ${OEM_CONF_ROOT}/oem.conf
				fi
				#OEM_MODEL_NAME
				if [ ! -z "$4" ]; then
				sed -ie "s/OEM_MODEL_NAME=.*/OEM_MODEL_NAME='$4'/" ${OEM_CONF_ROOT}/oem.conf
				fi
				#OEM_MODEL_DESC
				if [ ! -z "$5" ]; then
				sed -ie "s/OEM_MODEL_DESC=.*/OEM_MODEL_DESC='$5'/" ${OEM_CONF_ROOT}/oem.conf
				fi
				## TODO! this is 80503736 CGI URL, need to do factory default before reboot.
				exit 0;
			fi
		fi
		touch /var/run/Fail
		;;

	update)
		if [ ! -e ${OEM_CONF_ROOT}/oem.conf ]; then
			exit 0;
		fi
		[ -f ${OEM_CONF_ROOT}/oem.conf ] && . ${OEM_CONF_ROOT}/oem.conf
		echo "SYS_HOSTNAME='$SYS_HOSTNAME'" > ${OEM_CONF_ROOT}/oem.conf
		echo "COMPANY_NAME='$COMPANY_NAME'" >> ${OEM_CONF_ROOT}/oem.conf
		echo "COMPANY_URL='$COMPANY_URL'" >> ${OEM_CONF_ROOT}/oem.conf
		echo "OEM_SERIAL_NUM='$2'" >> ${OEM_CONF_ROOT}/oem.conf
		echo "OEM_MODEL_NAME='$3'" >> ${OEM_CONF_ROOT}/oem.conf
		echo "OEM_MODEL_DESC='$4'" >> ${OEM_CONF_ROOT}/oem.conf
 		;;

	clear)
		if [ -d $OEM_CONF_ROOT ]; then
			rm -rf $OEM_CONF_ROOT
		fi
		;;	

    *)
		echo "Usage: "$0" [start|save|clear]"
		exit 1
		;;
esac
exit 0

