#!/bin/sh
# vim:set sw=4 ts=4:
#
# $Id: make_routes,v 1.8 2005/04/11 14:14:39 fabian Exp $
#
#############################################################################
#
# ALICE
# Automatic Linux Installation and Configuration Environment
#
# Copyright (c) 2000-2002 SuSE Linux Solutions AG, Eschborn, Germany
#               2002-2004 SuSE Linux AG, Eschborn, Germany
#               2005           SUSE GmbH, Nuernberg, Germany
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#############################################################################
#
# Author: Fabian Herschel
#
#############################################################################
#
# make_routes
# configures the ip routing
#
# Sections: routes
# Tags:     DEF_GATEWAY, ROUTES
#
############################################################################
# 
# Notes:
#    The default route could be configured with two methods:
#    using the <DEF_GATEWAY>-Tag: example: <DEF_GATEWAY>an_ip_adress
#                                          </DEF_GATEWAY>
#    or using the <ROUTES>-Tag: example:   <ROUTES>...default an_ip_adress...
#                                          </ROUTES>
#
############################################################################
#
#
test -n "$alice_dir" || alice_dir="/usr/lib/alice2/"
export alice_dir
usage()
{
   cat <<EOF
usage: $0 -h | --help | -?
       $0 -fqhn hostname

EOF
}

while [ $# -gt 0 ]
do
   case $1 in
       -h | --help | -? ) usage
                          exit 1
                          ;;
       -fqhn ) export fqhn=$2; shift
            ;;

   esac
   shift
done   

echo "load lib $alice_dir/lib/alicerc"
. $alice_dir/lib/alicerc

WELCOME
# TODO: Check for new routing table under sysconfig/network
if [ -e "/etc/sysconfig/network/routes" ]
then
   std_route_conf="/etc/sysconfig/network/routes"
else
   std_route_conf=/etc/route.conf
fi
#
############################################################################
#
GET_CONFIGURATION --require routes $CFG_DEBUG
if [ -n "$ROUTES" -o -n "$DEF_GATEWAY" ]
then
   #
   # there are three types of routes
   #  - the device route for each network interface
   #  - dedicated static routes
   #  - the default route
   #
   # <destination> 0.0.0.0 <netmask> <device>
   #  -----------                     ------
   # <destination> <ip-gw> <netmask>
   #  -----------   ----- 
   # default  <ip-gw>
   # -------
   #
   # underlined you see the "key" fields of the routes. There must not
   # be two routes with the same key fields
   #
   # the already configured routes have the lowest priority, the
   # route defined in the DEF_GATEWAY has middle prio and the routes
   # defined with ROUTES have the highest priority. In the first
   # step we write all route definitions into one file and then we
   # "unify" these routes.
   #
   test -f ${std_route_conf} && cat ${std_route_conf} > ${std_route_conf}.alice
   if [ -n "$DEF_GATEWAY" ]
   then
      echo "default $DEF_GATEWAY" >> ${std_route_conf}.alice
   fi
   if [ -n "$ROUTES" ]
   then
      echo "$ROUTES" >> ${std_route_conf}.alice
   fi
   #
   # now let us unify these routes. the device routes will be
   # the first routes, the static routes follows and the default
   # route comes at the end of the file
   # TODO: Decide wether we need (or need not) to use the old routing table
   #       it would be easier just to provide the routing table got from alice
   #
   awk 'BEGIN {}
        $1 == "default"  && NF==2  {  def_gw=$2          # store default gateway, last entry wins
                                 }
        $2 == "0.0.0.0"  && NF==4  {                     # its a device route
                                    Key=$1 ":" $4      # destination and device as key

                                    DevRoutes[Key]=$3  # store netmask, last entry wins
                       }
        NF==3 {                                         # its a deticated static route
               Key=$1 ":" $2                           # destination and gateway as key
               StaticRoutes[Key]=$3                    # store netmask, last entry wins 
             }
        

        END  {
                 #
                 # print device routes first
                 #
                  for ( a_key in DevRoutes ) {
                      split(a_key,Words,":") 
                      printf "%s 0.0.0.0 %s %s\n", Words[1], DevRoutes[a_key], Words[2]
                  } 
                  #
                  # now print static routes
                  #
                  for ( a_key in StaticRoutes ) {
                      split(a_key,Words,":")
                      printf "%s %s %s\n", Words[1], Words[2], StaticRoutes[a_key]
                  }
                  #
                  # last print the default route
                  #
                  printf "default %s\n", def_gw

             }
       ' ${std_route_conf}.alice > ${std_route_conf}.alice.2
       mv ${std_route_conf}.alice.2 ${std_route_conf}.alice
    #----------------------------------------------
    #
    # install the new routing file 
    #
    CHANGED ${std_route_conf} || {
	  BACKUP --error ${std_route_conf}  
          INSTALL ${std_route_conf}
    }
fi
#
############################################################################
# Thats all
GOOD_BYE 
