#! /bin/sh
#
# /etc/init.d/init_system: do some appropriate init for system
#

init_data_service()
{
	NFS_STAT=`grep nfs_service /wetc/system.conf | awk '{print $3}' | sed 's/"//g'`
	if [ "$NFS_STAT" != "disable" ]; then
		/etc/init.d/nfs-common start
		/etc/init.d/nfs-kernel-server start
	fi
	
	WDAV_STAT=`grep webdav_service /wetc/system.conf | awk '{print $3}' | sed 's/"//g'`
	if [ "$WDAV_STAT" != "disable" ]; then
		/etc/init.d/httpd start
	fi
	
	FTP_STAT=`grep ftp_service /wetc/system.conf | awk '{print $3}' | sed 's/"//g'`
	if [ "$FTP_STAT" != "disable" ]; then
		/etc/init.d/proftpd start
	fi
	
	SAMBA_STAT=`grep samba_service /wetc/system.conf | awk '{print $3}' | sed 's/"//g'`
	if [ "$SAMBA_STAT" != "disable" ]; then
		/etc/init.d/samba start
		/etc/init.d/winbind start
	fi
	
	AFP_STAT=`grep afp_service /wetc/system.conf | awk '{print $3}' | sed 's/"//g'`
	if [ "$AFP_STAT" != "disable" ]; then
		/etc/init.d/netatalk start
	fi

	IPFILTER_STAT=`grep ipfilter_service /wetc/system.conf | awk '{print $3}' | sed 's/"//g'`
	if [ "$IPFILTER_STAT" == "enable" ]; then
		/etc/init.d/ipfilter start
	fi
	
	RSYNCD_STAT=`grep rsyncd_service /wetc/system.conf | awk '{print $3}' | sed 's/"//g'`
	if [ "$RSYNCD_STAT" == "enable" ]; then
		/etc/init.d/rsyncd start
	fi
}

init_auto_shutdown()
{
	STAT=`grep auto_shutdown /wetc/system.conf | awk '{print $3}' | sed 's/"//g'`
	if [ "$STAT" = "enable" ]; then
		[ -f /proc/ems/tsensor/cfg_shutdown ] && echo enable > /proc/ems/tsensor/cfg_shutdown
		[ -f /proc/ems/tsensor/cfg_shutdown ] && echo enable > /proc/ems/vsensor/cfg_shutdown
	fi
}

beep()
{
	# checking device
	[ -f /proc/ems/alarm/tone ] || return

	BOOTUP_BEEP="1"
	TONE=`cat /proc/ems/alarm/tone`

	if [ "$TONE" = "0" ]; then
		echo -n "Issue buzzer beep......"
		echo "boot_beep" > /proc/ems/alarm/tone
		sleep $BOOTUP_BEEP
		echo "boot_mute" > /proc/ems/alarm/tone
	fi
	echo "done"
}

/usr/bin/update_hosts.sh
init_data_service
if [ -f /var/lock/init_service_passwd ]; then
	# 0x1 : admin, 0x2 : guest, 0x3 : admin & guest
	val=`/bin/cat /var/lock/init_service_passwd`
	/bin/rm /var/lock/init_service_passwd

	if [ "$(($val&0x1))" -ne "0" ]; then
		PASSWD=`/usr/bin/awk '/password =/{print $3}' /etc/oem.conf`
		[ "$PASSWD" == "" ] && PASSWD="1234"

		#echo "Set samba password for default users";
		(echo "$PASSWD"; echo "$PASSWD") | smbpasswd -as admin > /dev/null 2>&1
		#echo "Set webdav password for default users";
		/usr/bin/webdav_passwd.php admin $PASSWD > /dev/null 2>&1
		#echo "Set iSCSI CHAP password for default users";
		/usr/bin/iscsi_passwd.php -a admin $PASSWD > /dev/null 2>&1
	fi

	if [ "$(($val&0x2))" -ne "0" ]; then
		OP_PASSWD=`/usr/bin/awk '/op_passwd =/{print $3}' /etc/oem.conf`
		[ "$OP_PASSWD" == "" ] && OP_PASSWD="1234"

		#echo "Set samba password for default users";
		(echo "$OP_PASSWD"; echo "$OP_PASSWD") | smbpasswd -as guest > /dev/null 2>&1
		#echo "Set webdav password for default users";
		/usr/bin/webdav_passwd.php guest $OP_PASSWD > /dev/null 2>&1
		#echo "Set iSCSI CHAP password for default users";
		/usr/bin/iscsi_passwd.php -a guest $OP_PASSWD > /dev/null 2>&1
	fi
fi
init_auto_shutdown
/usr/bin/start_av.php
/usr/bin/dashboard_logd.php
#start dedup check
[ -f /usr/bin/dedup_check.sh ] && /usr/bin/dedup_check.sh

if [ -f /usr/bin/ipmitool ]; then
	BMC_VERSION=$(ipmitool -I open bmc info | grep "Firmware Revision" |awk '{print $4}')
	echo -e "BMC Version = $BMC_VERSION" >> /etc/bios
fi
# Change blinking state to ON state for power LED
product=`/usr/bin/awk -F= '/Product Name/{print $2}' /etc/bios | /bin/sed 's/ //g'`
if [ "$product" = "GS-6UASV3" -o "$product" = "GA-6UASV3" ]; then
	/usr/bin/iomem w32-port 0x518 0x0 > /dev/null 2>&1
	/bin/echo 28 0 > /proc/ga_gpio
fi
beep

[ -x /usr/bin/olapinotifywait ] && /usr/bin/olapinotifywait -d -o /var/log/olapinotify.log -q -q -r /mnt/pool

# Show management IP
IP=`ifconfig eth0 | awk '$2 ~ /addr:/{print $2}' | awk -F: '{print $2}'`
if [ "$IP" == "" ]; then
	IP=`ifconfig bond0 | awk '$2 ~ /addr:/{print $2}' | awk -F: '{print $2}'`
fi
echo "IP: $IP"
/usr/sbin/fire-evt INFO EVTID_SYS_BOOTUP
#system boot up ok, turn on controller status led

if [ -f /proc/lcm/syshbt/timeout ]; then
	/bin/echo 1 > /proc/lcm/syshbt/timeout
	/bin/echo enable > /proc/lcm/syshbt/control
fi

if [ -f /wetc/autoexec.sh ]; then
	/wetc/autoexec.sh
fi

