#! /bin/sh

test -f /usr/bin/ifplugd || exit 0

start()
{
	echo -n "Starting network detect daemon: ifplugd"
	iface_count=`/bin/ls /sys/class/net/ |grep eth |wc -l`
	for iface_line in $(seq 1 $iface_count)
	do
		iface_name=`/bin/ls /sys/class/net/ |grep eth |grep -v "\." |sed -n "$iface_line"p`
		#-a :off auto up when link down
		/usr/bin/ifplugd -I -a -d 0 -i "$iface_name" -r /usr/bin/net_notify.php >/dev/null 2>&1
	done
	echo " done."
}

stop()
{
	echo -n "Stoppng network detect daemon: ifplugd"
	iface_count=`/bin/ls /sys/class/net/ |grep eth |grep -v "\." |wc -l`
	for iface_line in $(seq 1 $iface_count)
	do
		iface_name=`/bin/ls /sys/class/net/ |grep eth |grep -v "\." |sed -n "$iface_line"p`
		/usr/bin/ifplugd -k -i "$iface_name"  >/dev/null 2>&1
	done
	echo " done."
}

case "$1" in
  start)	
	start
    ;;
  stop)
	stop
    ;;
  restart)
	stop
    sleep 1
	start
    ;;
  *)
    echo "Usage: /etc/init.d/ifplugd {start|stop|restart}"
    exit 1
esac

exit 0
