#!/bin/sh
########################################################################
# Begin $network_devices/services/ipv4-alias
#
# Description : Add an aliased IPV4 address to a device
#
# Author      : Andrew Beverley - andy@andybev.com
#
# Version     : 00.00
#
# Notes       :
#
########################################################################

. /etc/sysconfig/rc 
. ${rc_functions} 
. ${IFCONFIG}

# Check for IP and ALIAS variables
if [ -n "${ALIAS}" -a "${IP}" ]; then
	args="label ${1}:${ALIAS}"
elif [ -n "${ALIAS}" ]; then
	boot_mesg "IP variable missing from ${IFCONFIG}, cannot continue." ${FAILURE}
	echo_failure
	exit 1
else
	boot_mesg "ALIAS variable missing from ${IFCONFIG}, cannot continue." ${FAILURE}
	echo_failure
	exit 1
fi

if [ -z "${PREFIX}" -a -z "${PEER}" ]; then
	boot_mesg -n "PREFIX variable missing from ${IFCONFIG}," ${WARNING}
	boot_mesg " assuming 24."
	echo_warning
	PREFIX=24
	args="${args} ${IP}/${PREFIX}"
elif [ -n "${PREFIX}" -a -n "${PEER}" ]; then
	boot_mesg "PREFIX and PEER both specified in ${IFCONFIG}, cannot continue." ${FAILURE}
	echo_failure
	exit 1
elif [ -n "${PREFIX}" ]; then
	args="${args} ${IP}/${PREFIX}"
elif [ -n "${PEER}" ]; then
	args="${args} ${IP} peer ${PEER}"
fi

if [ -n "${BROADCAST}" ]; then
	args="${args} broadcast ${BROADCAST}"
fi

case "${2}" in
	up)
		boot_mesg "Adding IPv4 address ${IP} to alias ${ALIAS} of the ${1} interface..."
		ip addr add ${args} dev ${1}
		evaluate_retval
	;;
	
	down)
		boot_mesg "Removing IPv4 address ${IP} from alias ${ALIAS} of the ${1} interface..."
		ip addr del ${args} dev ${1}
		evaluate_retval
	;;
	
	*)
		echo "Usage: ${0} [interface] {up|down}"
		exit 1
	;;
esac

# End $network_devices/services/ipv4-alias
