#!/bin/sh

. /etc/sysconfig/rc.conf
. /etc/sysconfig/network.conf
. /bin/network-functions
. /etc/image_features

PIDFILE=/var/run/secondlanipeconfupdate.pid
SERVICE=secondlanipeconfupdate
PROCESS_NAME=secondlanipeconfupdate
CONF_FILE=/etc/secondlanipeconfupdate.conf

do_restart ()
{
	echo -n "Generating IPE Configuration: "
	if [ "$FEATURE_IPLINES_AUTOCONFIG" = "1" ] ; then
	    /etc/init.d/ipphonedirectory > /dev/null 2>&1
	    if [ "$START_PBX_ONLY_MODE" = "true" -a "$START_DHCLIENT" = "true" ] ; then
		echo "skipped"
	    else
		/bin/ipeconfgen generate normalize > /dev/null 2>&1 && echo "done" || echo "failed"	    
	    fi
	else
	    echo "feature disabled"
	fi

	/etc/init.d/dhcpd restart > /dev/null 2>&1;		

	/etc/init.d/epnetwork restart_lan1 > /dev/null 2>&1;

	if [ "$FEATURE_IP_LINES" = "1" ] ; then
		echo -n " Killing phone booter:                    "	
		$SETSID //bin/killall phrebooter	
		
		echo -n " Starting phone Rebooter:                 "
		$SETSID /bin/phrebooter -s -r > /dev/null 2>&1 &
		PID=$!
		$KILL_BIN -0 $PID > /dev/null 2>&1
		if [ $? = 0 ] ; then
			echo "done"
		else
			echo "failed"
		fi
	fi

	return
}

do_stop ()
{
	echo -n "Generating IPE Configuration: "
	if [ "$FEATURE_IPLINES_AUTOCONFIG" = "1" ] ; then
	    /etc/init.d/ipphonedirectory > /dev/null 2>&1
	    if [ "$START_PBX_ONLY_MODE" = "true" -a "$START_DHCLIENT" = "true" ] ; then
		echo "skipped"
	    else
		/bin/ipeconfgen generate normalize > /dev/null 2>&1 && echo "done" || echo "failed"	    
	    fi
	else
	    echo "feature disabled"
	fi

	/etc/init.d/dhcpd stop > /dev/null 2>&1;		

	/etc/init.d/epnetwork restart_lan1 > /dev/null 2>&1;

	if [ "$FEATURE_IP_LINES" = "1" ] ; then
		echo -n " Killing phone booter:                    "	
		$SETSID //bin/killall phrebooter	
		
		echo -n " Starting phone Rebooter:                 "
		$SETSID /bin/phrebooter -s -r > /dev/null 2>&1 &
		PID=$!
		$KILL_BIN -0 $PID > /dev/null 2>&1
		if [ $? = 0 ] ; then
			echo "done"
		else
			echo "failed"
		fi
	fi

	return
}

case "$1" in
	restart)
		do_restart
		;;
	stop)
		do_stop
		;;
	*)
		echo "Usage: $0 {restart|stop}"
		exit 1
esac

exit $?
