#!/bin/sh

if [ -n "$DEBUG" ]; then
  echo "$0: Beginning $1"
fi

if [ "$1" = "change" ]; then
  if [ "$IPADDR" = "$OLD_IPADDR" -a \
       "$NETMASK" = "$OLD_NETMASK" \
     ]; then

    if [ -n "$VERBOSE" ]; then
      echo "$0: IP address and netmask unchanged; skipping."
    fi

    exit 0
  fi
fi


case "$1" in
  up|change)

    if [ -z "${DEVICE}" -o -z "${IPADDR}" ]; then
      echo "$0: DEVICE (${DEVICE}) or IPADDR (${IPADDR}) not set!" 1>&2
      exit 1
    fi

    ifconfig_opts=""
		
    if [ -n "${NETMASK}" ]; then
      ifconfig_opts="$ifconfig_opts netmask ${NETMASK}"
    fi

    if [ -n "$VERBOSE" ]; then
      echo "$0: Running ifconfig" "${DEVICE}" "${IPADDR}" $ifconfig_opts up
    fi

    /sbin/ifconfig "${DEVICE}" "${IPADDR}" $ifconfig_opts up

    ;;

  down)

    if [ -n "$VERBOSE" ]; then
      echo "$0: Running ifconfig" "${DEVICE}" down
    fi

    /sbin/ifconfig "${DEVICE}" down
    ;;

  *)
    echo "Usage: $0 up|down|change" 1>&2
    exit 1
    ;;
esac

if [ -n "$DEBUG" ]; then
  echo "$0: Ending $1"
fi

exit 0
