#! /bin/sh
# start sansoft and load CFG

/usr/bin/prepare_cfg.sh

[ -f /sansoft/nasapi.ko ] || exit 0
echo "Starting NASAPI:"
/sbin/insmod /sansoft/nasapi.ko
echo "Start NASAPI done."

echo "Insert FUSE module:"
/sbin/insmod /lib/modules/fuse.ko
echo "Insert FUSE done....."

[ -f /sansoft/ipc.ko ] && {
echo "Starting IPC:"
/sbin/insmod /sansoft/ipc.ko
echo "Insert ipc done....."
}

echo "Starting EMS:"
if [ -f /usr/bin/rays_bios_util.php ]; then	#start get rays bios information
	serial_number=`/usr/bin/rays_bios_util.php -m -s`
	ems_params="ga_serial_num=\"$serial_number\""
	monitor_psu_cnt=`/usr/bin/awk -F= '/CONFIG_MONITOR_PSU_CNT/{print $2}' /etc/features.conf | /bin/sed 's/ //g'`
	[ -n "$monitor_psu_cnt" ] && ems_params="$ems_params ga_u1_psu_cnt=$monitor_psu_cnt"
	/sbin/insmod /sansoft/ems.ko $ems_params
else
	/sbin/insmod /sansoft/ems.ko
fi
echo "Insert EMS done....."

if [ -f /sansoft/hac.ko ] ; then
	echo "Starting HAC:"
	/sbin/insmod /sansoft/hac.ko
	echo "Insert HAC done."
fi

echo "Starting DTE:"
/sbin/insmod /sansoft/dte.ko
echo "Insert DTE done....."

echo "Starting SOE:"
/sbin/insmod /sansoft/soe.ko
echo "Insert SOE done....."

echo "Mounting for log..."
MP=/var/log
[ -d $MP ] || mkdir -p $MP
mount -t tmpfs -o size=32M tmpfs $MP

[ -f /etc/test.conf ] && exit 0

if [ -f /etc/model_name.conf ]; then
	if [ -x /usr/bin/get_bpl_id.php ]; then
		# Norma platform
		if [ -f /wetc/model_name.conf ]; then
			new_product_name=`/usr/bin/awk -F= '/product-name/{print $2}' /wetc/model_name.conf`
			new_product_id=`/usr/bin/awk -F= '/product-id/{print $2}' /wetc/model_name.conf`
			new_lcd_caption=`/usr/bin/awk -F= '/lcd-caption/{print $2}' /wetc/model_name.conf`
		else
			if [ "`lspci | grep -e 10fb -e 1528`" != "" ]; then
				product_name_term='product-name-dxe'
				product_id_term='product-id-dxe'
				lcd_caption_term='lcd-caption-dxe'
			else
				product_name_term='product-name-de'
				product_id_term='product-id-de'
				lcd_caption_term='lcd-caption-de'
			fi
		
			bpl_id=`/usr/bin/get_bpl_id.php`
			new_lcd_caption=`/usr/bin/awk -F= "/${lcd_caption_term}_D_${bpl_id}"/'{print $2}' /etc/model_name.conf`
			new_product_name=`/usr/bin/awk -F= "/${product_name_term}_D_${bpl_id}"/'{print $2}' /etc/model_name.conf`
			new_product_id=`/usr/bin/awk -F= "/${product_id_term}_${bpl_id}"/'{print $2}' /etc/model_name.conf`
			new_lcd_caption_S=`/usr/bin/awk -F= "/${lcd_caption_term}_S_${bpl_id}"/'{print $2}' /etc/model_name.conf`
			new_product_name_S=`/usr/bin/awk -F= "/${product_name_term}_S_${bpl_id}"/'{print $2}' /etc/model_name.conf`
			
			echo "product-name=$new_product_name" >> /wetc/model_name.conf
			echo "product-id=$new_product_id" >> /wetc/model_name.conf
			echo "lcd-caption=$new_lcd_caption" >> /wetc/model_name.conf
		fi
	else
		# Rays platform
		if [ -f /wetc/model_name.conf ]; then
			new_product_name=`/usr/bin/awk -F= '/product-name/{print $2}' /wetc/model_name.conf`
			new_product_id=`/usr/bin/awk -F= '/product-id/{print $2}' /wetc/model_name.conf`
		else
			if [ "`lspci | grep -e 168e`" != "" ]; then
				# broadcom 10G network adapter
				new_product_name=`/usr/bin/awk -F= '/product-name-dxe/{print $2}' /etc/model_name.conf`
				new_product_id=`/usr/bin/awk -F= '/product-id-dxe/{print $2}' /etc/model_name.conf`
			else
				new_product_name=`/usr/bin/awk -F= '/product-name-de/{print $2}' /etc/model_name.conf`
				new_product_id=`/usr/bin/awk -F= '/product-id-de/{print $2}' /etc/model_name.conf`
			fi
			echo "product-name=$new_product_name" >> /wetc/model_name.conf
			echo "product-id=$new_product_id" >> /wetc/model_name.conf
		fi
	fi

	if [ -n "$new_lcd_caption" ]; then
		# Update lcd-caption in oem.conf
		old_lcd_caption_line=`grep 'lcd-caption =' /etc/oem.conf`
		new_lcd_caption_line="lcd-caption = $new_lcd_caption"
		sed -i "s/$old_lcd_caption_line/$new_lcd_caption_line/" /etc/oem.conf
	fi
	if [ -n "$new_product_name" ]; then
		# Update product name in oem.conf
		old_product_line=`grep 'product-name =' /etc/oem.conf`
		new_product_line="product-name = $new_product_name"
		sed -i "s/$old_product_line/$new_product_line/" /etc/oem.conf
	fi
	if [ -n "$new_product_id" ]; then
		# Update PRODUCT_ID in oem.conf
		old_product_id_line=`grep 'PRODUCT_ID =' /etc/oem.conf`
		new_product_id_line="PRODUCT_ID = $new_product_id"
		sed -i "s/$old_product_id_line/$new_product_id_line/" /etc/oem.conf

		# BUG 10376, Patch this field for early version of poweruser config.
		old_feature_product_id_line=`grep 'PRODUCT_ID =' /etc/features.conf`
		if [ -n "$old_feature_product_id_line" ]; then
			sed -i "s/$old_feature_product_id_line/$new_product_id_line/" /etc/features.conf
		fi
	fi
	if [ -x /usr/bin/get_bpl_id.php ]; then
		if [ -d /www/img/${bpl_id} ]; then
			files="logo.gif welcome.jpg";
			for file in $files; do
				[ -f /www/img/${bpl_id}/$file ] && cp -f /www/img/${bpl_id}/$file /www/img/
			done
		fi
	fi
fi
/usr/bin/process_oem.sh
/usr/bin/gen_default.php
/usr/bin/gen_mib.php
if [ ! -f /wetc/passwd ]; then
	/usr/bin/load_cfg_file passwd shadow group
	/usr/bin/use_cfg_passwd
	/usr/bin/use_cfg_op_passwd

	# Content of init_service_passwd represent which account is going to reset the default passwd for services.
	# 0x1 : admin, 0x2 : guest, 0x3 : admin & guest
	/bin/echo '0x3' > /var/lock/init_service_passwd
else
	/bin/grep -q "^guest:x:38:101:" /etc/passwd
	ret=$?
	if [ $ret -eq 1 ]; then
		# FEATURE 9804, create guest account.
		/bin/adduser -h /var/tmp -H -G 'User_Group' -D 'guest' -u '38'
		ret=$?
		if [ $ret -eq 0 ]; then
			/usr/bin/use_cfg_op_passwd
			/bin/echo '0x2' > /var/lock/init_service_passwd
		fi
	fi
fi
/usr/bin/load_cfg_file clock hostname system.conf mail.conf resolv.conf snmp.conf log.conf port.conf qsyslog.conf netmsgd.conf mgtupsd.conf avscan.conf
TZ=`/usr/bin/awk -F= '/ZONE/{print $2}' /wetc/clock | sed 's/"//g'`
/bin/cp /usr/share/zoneinfo/$TZ /wetc/localtime
/usr/bin/set_reset_passwd.sh
/usr/bin/sync-clock.sh &
[ -f /usr/bin/expander.php ] && /usr/bin/expander.php   #start expander daemon
[ -f /usr/bin/crond_check.php ] && /usr/bin/crond_check.php   #start check crond duplicate task
[ -f /wetc/storage.conf ] && home_pool=`cat /wetc/storage.conf | grep home_pool |sed 's/"//g' | awk '{printf $3}'`
if [ "$home_pool" != "" ]; then
	/bin/kill -SIGALRM `/bin/cat /var/run/qlogd.pid` >/dev/null 2>&1
fi
/bin/dmesg | grep EVT_LEVEL_ >/tmp/dmesg_evt.log
exit 0
