#!/bin/sh
# $Id: ifplugd.sh,v 1.5 2006/05/27 15:36:48 eha Exp eha $
# -----------------------------------------------------------------------------
# Slackware script for customized setup of network interfaces.
# Parameters:
# $1  : interface name
# $2  : <pre|post|stop>
#
# If the script ends with 'RETURN=1', then rc.inet1 will omit further
# processing of the ip_up / if_down function for this interface.
# You can use that for instance if you want total control over your interface
# configuration; put all your functionality in the pre() function and let that
# end with a "RETURN=1".
# 
# This is an example script for using ifplugd to manage the interface,
# depending on link status.
# A package for ifplugd is available at:
#   http://www.slackware.com/~alien/slackbuilds/ifplugd/
#
# Eric Hameleers <alien@slackware.com>
# -----------------------------------------------------------------------------

INTERFACE=$1
ACTION=$2
RETURN=${RETURN:-0}

pre() {
  # Start ifplugd before Slackware has a chance to setup the interface.
  # This way, ifplugd will determine when the time is right to do so
  # (the interface cable might be unplugged on boot)
  # if [ ! -e /var/run/ifplugd.${INTERFACE}.pid ]; then # does not catch the 1st instance?
  if ! ps ax |grep "ifplugd -i ${INTERFACE}" |grep -v grep 1>/dev/null; then 
    echo "Running ifplugd to monitor $INTERFACE"
    /usr/sbin/ifplugd -i $INTERFACE
    touch /var/run/ifplugd.${INTERFACE}.pid
    RETURN=1
  fi
  # Do not 'RETURN=1' here so that an active ifplugd can call
  # '/etc/rc.d/rc.inet1 ${INTERFACE}_start' when link is detected.
}

post() {
  return
}

stop() {
  # If I do this, I end up with no ifplugd after I disconnect the cable :-/
  #/usr/sbin/ifplugd -k -i $INTERFACE
  return
}

case "$ACTION" in
'pre')
  pre
  ;;
'post')
  post
  ;;
'stop')
  stop
  ;;
*)
  echo "usage $0 <INTERFACE> <pre|post|stop>"
esac

# This script must NEVER call 'exit' !
# It can set the variable $RETURN so that the calling script rc.inet1 will stop
# further processing of the parent function (if_up or if_down).
# To make the script return you should set RETURN=1 in one of the
# "pre|post|stop" functions, where you deem it appropriate.


