#!/bin/sh
# $Revision: 1.5 $

#
# NAME
#       aliases-ip - configure network IP aliases
#
# SYNOPSIS
#       /etc/init.d/aliases-ip ( start | stop ) [ debug ]
#
# DESCRIPTION
#       This script automates the configuration of IP address aliases for
#       network interfaces.
#
#       The file /etc/config/ipaliases.options contains the list of hosts or
#       IP addresses to be used as IP address aliases. The format of
#       this file is:
#
#               interface host1 [netmask addr] [broadcast addr]
#               interface host2 [netmask addr] [broadcast addr]
#               ...
#               interface host3 [netmask addr] [broadcast addr]
#
#       hosti is either a valid hostname in /etc/hosts or is a "dot"
#       notation IP address as in 192.22.33.44.  The interface should be
#       the interface name as reported by netstat -i which will support
#       the alias (i.e. ec0, ec3, et cetera).
#
#       ipaliases automatic start up at boot time can be enabled or disabled
#       by the commands:
#
#               chkconfig ipaliases on          # enable ipaliases
#               chkconfig ipaliases off         # disable ipaliases
#
# FILES
#       /etc/hosts              # host table file
#       /etc/config/ipaliases.options
#                               # table of IP addresses to configure.
#       /etc/config/ipaliases   # chkconfig control file for ipaliases startup
#       /etc/config/network     # chkconfig control file for network startup
#
# SEE ALSO
#       chkconfig(1M), ifconfig(1M), netstat(1), routed(1M)
#
# COPYRIGHT NOTICE
#       Copyright (C) 1995, Silicon Graphics.  All Rights Reserved.
#

#
# Configuration variables
#
IPALIASES_TABLE="/etc/config/ipaliases.options"

#
# Convenience macros
#
IS_ON="/sbin/chkconfig"
IFCONFIG="/usr/etc/ifconfig"

#
# If "debug" is passed as second argument, enable output of noisy commands
# to stdout/stderr. Otherwise redirect extraneous output to /dev/null.
#
if test "$2" = "debug" || $IS_ON verbose ; then
    DBGOUT="/dev/fd/1"
    DBGERR="/dev/fd/2"
else
    DBGOUT="/dev/null"
    DBGERR="/dev/null"
fi

#
# "main" body.  Handle "start|stop".
#
case $1 in
    "start")
    if $IS_ON network && $IS_ON ipaliases ; then

        echo "IP aliases:" > $DBGOUT;

        #
        # Get hosts from ipaliases.tab.  Parse out # comments which begin
        # in first column.
        #
        while read line
        do
                ipaliases=`echo $line | sed -e 's/[     ]*#.*$//' -e '/^$/d' `
                if test -z "$ipaliases" ; then
                        continue;
                fi
		read interface host restofline <<-EOF
		$ipaliases
		EOF
                $IFCONFIG $interface alias $host $restofline
                echo "Adding alias $host to interface $interface" > $DBGOUT;
        done < $IPALIASES_TABLE
    fi
    ;;

    "stop")
        #
        # Get hosts from ipaliases.tab.  Parse out # comments which begin
        # in first column.
        #
        while read line
        do
                ipaliases=`echo $line | sed -e 's/[     ]*#.*$//' -e '/^$/d' `
                if test -z "$ipaliases" ; then
                        continue;
                fi
		read interface host restofline <<-EOF
		$ipaliases
		EOF
                $IFCONFIG $interface delete $host
                echo "Deleting alias $host to interface $interface" > $DBGOUT;
        done < $IPALIASES_TABLE
    ;;
esac
