#!/bin/sh -
#
#  $Id: staticroutes,v 1.2 2002/06/11 14:51:37 dgregor Exp $
#
# Copyright (c) 2002 Daniel J. Gregor, Jr.
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
#    - Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#    - Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials provided
#      with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

PATH=$PATH:/usr/bin:/sbin:/usr/sbin

gatewayfile="/etc/gateways.conf"
routefile="/etc/staticroutes.conf"

gatewayfileread="no"
routefileopen="no"

rewritegateway() {
	if [ x"$gatewayfileread" = x"no" ]; then
		if [ ! -r $gatewayfile ]; then
			die "could not open gateways file \"$gatewayfile\""
		fi
	
		exec 3<$gatewayfile

		num=0
		gatewayregexp=""
		while read line <&3
		do
			num=`expr $num + 1`

		        set -- `echo "${line}" | sed 's/#.*//;s/^[       ]*$//'`

			if [ $# -eq 0 ]; then
				continue
			fi

			if [ $# -ne 2 ]; then
				warn "incorrect number of arguments on line $num: \"line\""
				continue
			fi

			gatewayname="$1"; shift
			gatewayip="$1"; shift

			gatewayregexp="$gatewayregexp -e s/$gatewayname/$gatewayip/"
		done

		gatewayfileread="yes"
	fi

	if [ x"$gatewayregexp" != x"" ]; then
		gateway="`echo $gateway | sed $gatewayregexp`"
	fi

	return 0
}

getroute() {
	if [ x"$routefileopen" = x"no" ]; then
		if [ ! -r $routefile ]; then
			die "could not open routes file \"$routefile\""
		fi
	
		exec 4<$routefile

		routefileopen="yes"
		linenum=0
	fi

	if [ x"$routefileopen" = x"end" ]; then
		routefileopen="no"
		return 1
	fi

	while true
	do
		read routeline <&4
	
		if [ $? -ne 0 ]; then
			routefileopen="end"
			exec 4<&- 
			return 1
		fi

		linenum=`expr $linenum + 1`

	        set -- `echo "${routeline}" | sed 's/#.*//;s/^[       ]*$//'`

		if [ $# -eq 0 ]; then
			continue
		fi

		if [ $# -ne 3 ]; then
			warn "incorrect number of arguments on line $linenum: \"routeline\""
			continue
		fi

		network="$1"; shift
		netmask="$1"; shift
		gateway="$1"; shift

		rewritegateway

		break
	done

	return 0
}

basename="`basename $0`"

die() {
        echo "${basename}: $*" >&2
        exit 1
}
warn() {
        echo "${basename}: $*" >&2
}

usage="Usage: $0 [-n] { start | stop } [network] [netmask]"

dryrun=0

while getopts vn opt
do
	case $opt in
		n)
			dryrun=1
		;;

		\?)
			die "unknown option\n$usage"
		;;
	esac
done
shift `expr $OPTIND - 1`

if [ $# -lt 1 ]; then
	die "too few arguments\n$usage"
fi

case "$1" in
	'start')
		command="add"
	;;

	'stop')
		command="delete"
	;;

	*)
		die "invalid command\n$usage"
	;;
esac

shift

if [ $# -gt 0 ]; then
	searchnetwork="$1"; shift
else 
	searchnetwork=""
fi

if [ $# -gt 0 ]; then
	searchnetmask="$1"; shift
else 
	searchnetmask=""
fi

if [ $# -gt 0 ]; then
	die "too many arguments\n$usage"
fi

while getroute
do
	if [ x"$searchnetwork" != x"" -a x"$searchnetwork" != x"$network" ]
	then
		continue
	fi

	if [ x"$searchnetmask" != x"" -a x"$searchnetmask" != x"$netmask" ]
	then
		continue
	fi

	if [ $dryrun -eq 0 ]; then
		route -n $command -net $network -netmask $netmask $gateway | \
			grep -v '[0-9]$' >&2
	else
		echo route -n $command -net $network -netmask $netmask $gateway
	fi
done
