#!/bin/sh
# ifplugd      Brings up/down network automatically
#
# chkconfig: 2345 11 89
# description: Brings networks interfaces up and down automatically when \
#              the cable is removed / inserted
#
# processname: /usr/sbin/ifplugd
# config: /etc/ifplugd/ifplugd.conf
# Version: 2003020100

# For VinePlus 2.5/2.6
# Satoshi IWAMOTO <satoshi.iwamoto@nifty.ne.jp.
# 2003.06.05

# Source function library.
if [ -f /etc/init.d/functions ]; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 0
fi

# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# This is our service name
BASENAME=`basename $0`
if [ -L $0 ]; then
  BASENAME=`find $0 -name $BASENAME -printf %l`
  BASENAME=`basename $BASENAME`
fi

CFG=/etc/ifplugd/ifplugd.conf
IFPLUGD=/usr/sbin/ifplugd

[ -x $IFPLUGD ] || exit 0

if [ $(id -u) != "0" ] && [ "$1" = "start" -o "$1" = "stop" ] ; then
  echo "You must be root to start, stop or restart ifplugd."
  exit 1
fi

[ -f $CFG ] && . $CFG
[ -z "$INTERFACES" ] && INTERFACES="eth0"

[ "$INTERFACES" = "auto" ] && INTERFACES=$(cat /proc/net/dev | awk '{ print $1 }' | grep ^eth | cut -d: -f1)

RETVAL=0

start() {
  echo -n "Starting $BASENAME: "
  for IF in $INTERFACES ; do
    $IFPLUGD -i $IF $ARGS > /dev/null 2>&1
  done
  /sbin/pidof $BASENAME >/dev/null 2>&1; RETVAL="$?"
  action "" [ $RETVAL -eq 0 ]
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$BASENAME
  return $RETVAL
}

stop() {
  echo -n "Shutting down $BASENAME: "
  killproc $BASENAME
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$BASENAME
  return $RETVAL
}

restart() {
  stop
  sleep 3
  start
}

rhstatus() {
  status $BASENAME
}

condrestart() {
  [ -e /var/lock/subsys/$BASENAME ] && restart || :
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    rhstatus
    ;;
  restart|reload)
    restart
    ;;
  condrestart)
    condrestart
    ;;
  *)
    echo "Usage: $BASENAME {start|stop|status|restart|reload|condrestart}"
    exit 1
esac

exit $RETVAL
