#!/bin/bash
#
# by ymc-dabe
# Bring an interface up an set its primary IP
#


### Include ymclibnettools
. /usr/lib/lib-fliwi/ymc-networktools.bash || exit 1

ymcCommand=$4
USAGE="usage: $0 ip netmask interface {start|stop|status}";
ipToUse=$1
netmaskToUse=$2
interfaceToUse=$3

send_arp="/usr/lib/heartbeat/send_arp"

usage()
{
    echo $USAGE >&2
}

if [ $# != 4 ]; then
    usage
    exit 1
fi


if_status()
{
    if [ $(/sbin/ifconfig $interfaceToUse | grep -F "$ipToUse" | wc -l) -gt 0 ]; then
      echo "running"
    else
      echo "stopped"
    fi
}

if_start()
{
  /sbin/ifup $interfaceToUse
  /sbin/ifconfig $interfaceToUse $ipToUse netmask $netmaskToUse up
  if [ -x "$send_arp" ]; then
    $send_arp -i 1010 -r 5 $interfaceToUse $ipToUse auto $(ymc_get_field_from_ifconfig $interfaceToUse "Bcast:") $netmaskToUse &
  fi
}

if_stop()
{
  /sbin/ifconfig $interfaceToUse 0.0.0.0 up
  /sbin/ifdown $interfaceToUse
}



case $ymcCommand in
  start)        if_start;;
  stop)         if_stop;;
  status)       if_status;;
  *)            usage
                exit 1
                ;;
esac

