#!/bin/sh
# vim:set sw=4 ts=4:
#
# $Id: make_modules,v 1.19 2005/04/11 14:14:39 fabian Exp $
#
#############################################################################
#
# ALICE
# Automatic Linux Installation and Configuration Environment
#
# Copyright (c) 2000 SuSE Linux Solutions AG, Eschborn, 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_modules
# configures the /etc/modules.conf (or /etc/conf.modules)
#
# Sections: modules
# Tags:     MODULES_CONF
#
#############################################################################
#
# NOTES:
#         configures the file /etc/modules.conf or /etc/conf.modules
#         needs a alice config file conf.modules
#         the lines, which are defined in the alice file will be replaced
#         in /etc/conf.modules
#         please remember: there are three places where an alice config 
#         file could be placed:
#           ALICE/info/{hostname}.{config_file}
#           ALICE/classes/{class}.{config_file}
#           ALICE/templates/{config_file}
#         only one alias and one option line is supported for each
#         identifier. so if you have multiple options-lines for a
#         device (i.e. eth0) only the first one is used!!
#
#############################################################################
#
# TODO: Check, if we can handle the new syntax extentions of modules.conf
# TODO: Check, if we are able to create the modprobe.conf file too
#
usage()
{
   echo "$_self -h | --help | -?"
   echo "$_self -fqhn"
}

test -n "$alice_dir" && alice_dir="/usr/lib/alice2/"
export alice_dir
#
# setting some "globals"
#
# ! Decide wether to use conf.modules or modules.conf (newer):
#    the old config file for modutils was /etc/conf.modules
#    the newer is /etc/modules.conf. This routine figures out
#    wether to use /etc/modules.conf or /etc/conf.modules
#    If a /etc/modules.conf exists, the config file is /etc/modules.conf
#    If no /etc/modules.conf but a /etc/conf.modules exists the
#       config file is /etc/conf.modules
#    If neither /etc/modules.conf nor /etc/conf.modules exists, the
#       config file is /etc/modules.conf
#    So the must be a /etc/conf.modules and there must not be a 
#       /etc/modules.conf to get /etc/conf.modules as config file.
#
std_conf_file="/etc/modules.conf"
if [ ! -f $std_conf_file -a -f /etc/conf.modules ]
then
   std_conf_file="/etc/conf.modules"
fi
#
# what says the command line?
#
while [ $# -gt 0 ]
do
   case $1 in
       -h | --help | -? ) usage
                          exit 1
                          ;;
       -fqhn ) export fqhn=$2; shift
            ;;
   esac
   shift
done
#
# load the ALICE environment
#
. $alice_dir/lib/alicerc
WELCOME
#
############################################################################
#
# And now we begin
#
GET_CONFIGURATION modules $CFG_DEBUG
# config_file=$(get_config_file conf.modules) 
if [ -z "$MODULES_CONF" ]
then
   GOOD_BYE
fi
#
# Lets merge the conf.module files
#   if the line is NOT a comment line (first char is a #)
#   we use the keyword (i.e. alias or options) and the 
#   identifier (i.e. eth0) to unify device specific lines
#   if multiple option lines are required, we have to change that
#   here...
# 
(
   cat << EOF
# /etc/modules.conf (or /etc/conf.modules)
# generated by ALICE make_modules
# $(date)
#
EOF
   echo "$MODULES_CONF" 
   cat $std_conf_file
) >  /tmp/conf.modules.alice.$$
awk '/^[^#]/ {  key=$1 "_" $2
                if ( printed[key] != "y" ) {
                   print $0
                   printed[key] = "y"
                }
             }
     /^#/    { print $0 }
     ' /tmp/conf.modules.alice.$$ > /tmp/conf.modules.alice.$$.new
#
# Copy the resulting conf.module to the destination
#    and saveing a recovery copy
#
BACKUP --require ${std_conf_file}
mv /tmp/conf.modules.alice.$$.new ${std_conf_file} || FATAL NOINST ${std_conf_file}
#
# Calling depmod to activate changes to the kerneld
#
depmod 2>/dev/null || WARNING CMDFAILED depmod
#
# clean up
#
rm /tmp/conf.modules.alice.$$
#
############################################################################
# Thats all
GOOD_BYE 
