#!/bin/sh
#--------------------------------------------------------------------
# nic	 -- Start/stop NSS Image Collecting services.
#--------------------------------------------------------------------

PATH="/sbin:/usr/sbin:/bin:/usr/bin"
export PATH

BIN_PATH="/opt/nss/bin/nicd"
NIC_CONF_FILE="/opt/nss/etc/nic.conf"

if [ -f /var/log/hdd_type.conf ]; then
	DEV_HDD=`cat /var/log/hdd_type.conf`
fi


[ -f $BIN_PATH ] || exit 0

RETVAL=0

# See how we were called.
case "$1" in
	start)
		echo -n "Starting NIC Daemon: "
		
		$BIN_PATH
		RETVAL=$?

		if [ $RETVAL -eq 0 ]; then
			echo "[  OK  ]"
		else
			echo "[FAILED]"
		fi
	
#		sed -e 's/RECORDING=stop/RECORDING=run/g' < /opt/nss/etc/nic.conf > /tmp/conf.tmp
#		cp /tmp/conf.tmp /opt/nss/etc/nic.conf
#		rm -f /tmp/conf.tmp

		;;

	stop)
		echo -n "Stopping NIC Daemon: "

		kill `/bin/cat /var/run/nicd.pid`
        
#--------------------------------------------------------------------
		for a in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
		do
#			echo -n "$a"
			sleep 2
			if [ ! -e /var/run/nicd.pid ]; then
				break
			fi
		done
#		sleep 2
#--------------------------------------------------------------------

		if [ ! -e /var/run/nicd.pid ]; then
			RETVAL=0
			echo "[  OK  ]"
		else
			RETVAL=-1
			echo "[FAILED]"
		fi

		/usr/sbin/led_off 0
		/usr/sbin/led_off 1

		;;

	restart)
		$0 stop
		sleep 3
		$0 start
		RETVAL=$?

		;;

	fstart)
		echo -n "Force Starting NIC Daemon: "
	
		sed -e 's/RECORDING=stop/RECORDING=run/g' < /opt/nss/etc/nic.conf > /tmp/conf.tmp
		cp /tmp/conf.tmp /opt/nss/etc/nic.conf
		rm -f /tmp/conf.tmp

		$BIN_PATH
		RETVAL=$?

		if [ $RETVAL -eq 0 ]; then
			echo "[  OK  ]"
		else
			echo "[FAILED]"
		fi

		;;

	fstop)
		echo -n "Force Stopping NIC Daemon: "

		sed -e 's/RECORDING=run/RECORDING=stop/g' < /opt/nss/etc/nic.conf > /tmp/conf.tmp
		cp /tmp/conf.tmp /opt/nss/etc/nic.conf
		rm -f /tmp/conf.tmp

		kill `/bin/cat /var/run/nicd.pid`
		RETVAL=$?

#--------------------------------------------------------------------
		for a in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
		do
#			echo -n "$a"
			sleep 2
			if [ ! -e /var/run/nicd.pid ]; then
				break
			fi
		done
#		sleep 2
#--------------------------------------------------------------------

		if [ $RETVAL -eq 0 ]; then
			echo "[  OK  ]"
		else
			echo "[FAILED]"
		fi

		/usr/sbin/led_off 0
		/usr/sbin/led_off 1

		;;

	frestart)
		$0 fstop
		sleep 3
		$0 fstart
		RETVAL=$?
		;;

	*)
		echo "Usage: nic {start|stop|restart|fstart|fstop|frestart}"
		exit 1
esac

exit $REVAL

