#!/bin/sh

###############################################################################
#
# router - management script to handle route table
# 
# Primary purpose is to :
# 1) add routes from the routes file to the route table.  This is call by 
#       by network startup script. 
# 2) update the default route to the route table, as called by the API.
#
#  route table = /etc/sysconfig/network/routes
#
# Copyright (c) 2006, all rights reserved
# License- GPL
# Author-  Mike Goins
# Company- Adtec Digital, Inc.
# Date-    2006-05-11
# Last Modified- 2006-10-11 by GMC
# Changed default MASK to 0.0.0.0 to fix gateway init issue
################################################################################

shopt -s -o nounset

declare -rx ROUTECONF="/etc/sysconfig/network/routes"
 
declare NUM
declare TAG=$'\040\040\040[ROUTE]'              # Logging tag
declare ROUTE="/sbin/route"
declare SED="/bin/sed"

startup () {
    echo "Adding Startup routes: "
    # TODO  add netmask handling?
    while read DEST GWAY MASK IFACE ; do
        test "$DEST" = "-" && DEST=""
        test "$GWAY" = "-" && GWAY=""
        test "$MASK" = "-" && MASK="0.0.0.0"
        test "$IFACE" = "-" && IFACE=""
        if [ "$GWAY" != "0.0.0.0" ]; then 
           local CMD="$ROUTE add -net $DEST netmask $MASK gw $GWAY $IFACE"
           logger -p user.info -t "$TAG" "$CMD"
           $CMD 
        fi        
    done < $ROUTECONF
    echo "Routes complete:"
#    $ROUTE -n
}

getdefault () {
  declare -x GW=`route -n | grep "^0.0.0.0" | head -1 | cut -c 17-32`  # gets the default route
  GW=`expr "$GW" : '[[:space:]]*\(.*\)[[:space:]]*$'`        # strip whitespace
  if [ -z "$GW" ]; then GW="0.0.0.0"; fi                       # if no default, then use 0.0.0.0
  echo $GW
}

setdefault () {
  local GW=$(getdefault)
  route del default gw $GW &>/dev/null
  route add default gw $1 &>/dev/null 
  if [ $? -ne 0 -a "$1" != "0.0.0.0" ] ; then   # did not add, restore the old GW
       $ROUTE add default gw $GW &>/dev/null
	   logger -p user.error -t "$TAG" "ERROR: Failed to set default route"
       exit 2
  else 
       # make the persistent change
	   $SED -i "s/\(^default \)[\.0-9]*\(.*$\)/\1${1}\2/" $ROUTECONF 
       #vi -e -n -c "%s/\(^default \)[\.0-9]*\(.*$\)/\1${1}\2/g | w! | q" $ROUTECONF &>/dev/null
  fi
}

setother () {
   if [ $# -ne 4 ]; then
     logger -p user.error -t "$TAG" "ERROR: Wrong number of args to set route"      
	 exit 1
   fi
   local CMD="$ROUTE add -net $1 netmask $3 gw $2 dev $4"
   logger -p user.info -t "$TAG" "$CMD"
   $CMD
   if [ $? -eq 0 ]; then
     declare -i OTHER=$(grep -vc "^default" $ROUTECONF)
	 if [ $OTHER -eq 0 ]; then
	    echo "$1 $2 $3 $4" >> $ROUTECONF
     else
        $SED -i '/default/!s/.*/'"$1 $2 $3 $4"'/' $ROUTECONF
	 fi
   else
     logger -p user.error -t "$TAG" "ERROR: Failed to set route"
     exit 1
   fi
}

deleteother (){
   if [ $# -ne 4 ]; then
     logger -p user.error -t "$TAG" "ERROR: Wrong number of args to delete route"      
	 exit 1
   fi
   local CMD="$ROUTE del -net $1 netmask $3 gw $2 dev $4"
   logger -p user.error -t "$TAG" "$CMD"
   $CMD
   $SED -i '/^default/!d' $ROUTECONF
}

getother (){
   declare OTHER=$(grep -v "^default" $ROUTECONF)
   if [ -n "$OTHER" ]; then
     echo $OTHER
   fi
}

usage (){
    echo "router startup|default|other|delete [gateway|network gateway netmask interface]"
	echo "Add default gateway:  router default 192.168.10.1"
	echo "Show default gateway:  router default"
	echo "Add other route:      router other 192.168.8.0 192.168.8.1 255.255.255.0 eth0"
	echo "Show other route:     router other"
	echo "Delete other route:   route delete 192.168.8.0 192.168.8.1 255.255.255.0 eth0"
}

if [ $# -eq 0 ]; then 
   usage
elif [ $# -eq 1 ]; then 
    if [ "$1" == "startup" ]; then
        startup
    elif [ "$1" == "default" ]; then
	    getdefault
    elif [ "$1" == "other" ]; then
	    getother
	elif [ "$1" == "all" ]; then
	    cat $ROUTECONF
    else
	    usage
	fi
else 
    if [ "$1" == "default" ]; then
        setdefault "$2"      
    elif [ "$1" == "other" ]; then
	    shift 
	    setother $@ 
    elif [ "$1" == "delete" ]; then
		shift 
		deleteother $@
    fi
fi

exit 0
