#!/bin/sh
#
# $Id: make_info_file,v 1.37 2005/04/11 14:14:34 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: 
#
# ALICE
# Automatic Linux Installation and Configuration Environment
#
#############################################################################
#
# Copyright (c) 2000 SuSE Linux Solutions AG, Eschborn, Germany
# All rights reserved.
#
# This file is part of ALICE, which was sponsored by
# T-Online International AG
#
# Author: Fabian Herschel
#
#############################################################################
#
# make_info_file fqhn
#
#############################################################
#
# NOTES:
#      problem at this moment:
#      you can define a version for a class or a machine like 7.0.0
#      and also a class is read like SuSE_linux_7.0.0 but any variables,
#      which are set with an special extention like 
#      SYS_SERVER_INST_$VENDOR_$OS_$VERSION must NOT contain dots (.) in
#      their name. So in the tag names the dots MUST be replaced by
#      underscores (like 7_0_0).
#
#############################################################
# 
echo "make_info_file 0.3.1" >&2
dhcp_mode=0
if [ -z "$ALICE_HOME" ]
then
   echo "ALICE_HOME not set"
   exit 1
fi

function err_exit
{
   echo $1
   clean_up
   exit 1
}

function usage
{
  cat << EOF
usage: make_info_file fqhn
       make_info_file --help
   
   fqhn means full qualified host name
EOF
   
}

function last_of
{
    typeset -i last
    if [ $# -gt 0 ]
    then
       last=$(( $# -1 ))
       shift $last
       echo $1  
    fi
}

function clean_up
{
   cd /tmp
   cd -
}

function get_file
{
    source="$url_path/$1"
    dest=$2


    case $method in
        NFS )
               cp $source $dest
               ;;
        SCP )
               scp $url_server:$source $dest
               ;;
          * )  err_exit "Method $method not implemented yet"
               ;;
    esac
}
#
################################## MAIN #######################
#
#cd $ALICE_HOME
#
# have a look on the comamnd line
#
while [ $# -gt 0 ]
do
   case $1 in
        -\? | --help | -h ) usage
                           exit 1
                           ;;
        --dhcp )           export dhcp_class=$2
                           export GET_CONFIG_OPTION="--dhcp --debug"
                           export fqhn=dhcp
                           export dhcp_mode=1
                           shift
                           ;;
        [a-zA-Z]*        ) fqhn=$1
                           ;;
   esac
   shift
done

if [ "$fqhn" = "" ]
then
   echo "error: you must specify a hostname" >&2
   usage
   exit 1
fi

if [ -z "$alice_dir" ]; then
	alice_dir=$(cd $(dirname $0)/..; pwd)
	export alice_dir
fi
. $alice_dir/lib/alicerc 

GET_CONFIGURATION $GET_CONFIG_OPTION sys
export alice_host_classes="${SYS_OS_VENDOR}_${SYS_OS}_${SYS_OS_VERSION} ${SYS_LOCATION} ${SYS_ALICE_CLASSES}"
#
# class sys need to be read a second time, because ALICE_HOST_CLASSES  is set now so 
# the hosts classes are active now
#
GET_CONFIGURATION $GET_CONFIG_OPTION sys
GET_CONFIGURATION $GET_CONFIG_OPTION network
GET_CONFIGURATION $GET_CONFIG_OPTION yast
GET_CONFIGURATION $GET_CONFIG_OPTION routes
#GET_CONFIGURATION $GET_CONFIG_OPTION inst.server
#
##############################################################################
#
# The following standard output will be redireced to the info file
#
##############################################################################
#
(
echo "# Info file for host $fqhn"
echo "# generated $(date)"
echo "Language: $SYS_LANGUAGE"
echo "Display:  $SYS_DISPLAY"
echo "Keytable: $SYS_KEYTABLE"
SYS_BOOT_MODE=$(echo $SYS_BOOT_MODE | tr [:lower:] [:upper:])
echo "Bootmode: $SYS_BOOT_MODE"
#
# Configure the basic network settings for the installation
#
if [ -z "$NET_INST_DEV" ]  
then
   echo "NET_INST_DEV is not set " >&2
else
   echo "Netdevice: $NET_INST_DEV"
   if [ "$dhcp_mode" -ne 1 ]
   then
     echo "$NET_IP_CONFIG" | grep "$NET_INST_DEV" | \
    ( read net_interface net_addr net_mask net_broadcast
      echo "IP:        $net_addr"
      echo "Netmask:   $net_mask"
      echo "Broadcast: $net_broadcast"
    )
   fi
fi
#
# Configure the installation gateway, if defined.
# The default clause in $ROUTES has precedence to
# $DEF_GATEWAY
#
def_gateway=""
if [ -n "$ROUTES" ]
then
   def_gateway=$(echo "$ROUTES" | awk '$1 == "default" { print $ 0 }')
fi
if [ -z "$def_gateway" ]
then
  if [ -n "$DEF_GATEWAY" ]
  then
      def_gateway="$DEF_GATEWAY"
   fi
fi
#
# if we "found" a default gateway let us tell it to linuxrc
#
if [ -n "$def_gateway" ]
then
   echo "Gateway: $def_gateway"
fi

#
# The YaST settings for the auto mode
#
echo "$YAST_INFO"
#
# the software selection is either defined by an explicit list
# (SYS_SW_SELLIST) or a path to a selection file (SYS_SW_SELECTION)
#
if [ -n "$SYS_SW_SELLIST" ]
then
   #
   # we must generate the selection file later
   #
   echo "AUTO_INSTALL /auto.sel"
else
   #
   # SYS_SW_SELECTION should contain a path to a selection file
   #
   echo "AUTO_INSTALL $SYS_SW_SELECTION"
fi
#
# SYS_SW_ADDLIST should contain a list of standard packages, which should be
# installed additional to the packages listet in the selection file
#
if [ -n "$SYS_SW_ADDLIST" ]
then
   echo "ADD_INSTALL"  /add.sel
fi
#
# if the bootmode is net we need to know from where we are able to install
# this information could be set by the tags
# SYS_INST_SERVER, SYS_INST_DIR, SYS_INST_SERVER_$VENDOR_$SYSTEM_$VERSION and
# SYS_INST_DIR_$VENDOR_$SYSTEM_$VERSION
#
if [ "$SYS_BOOT_MODE" = "NET" ]
then
   # 
   # figure out the installation server SIS
   #
   SIS=$(get_inst_server)
   echo "Server:        $SIS"
   #
   # figure out the installation directory SID
   #
   SID=$(get_inst_directory)
   echo "Serverdir:        $SID"
fi
# 
#
#SYS_MODULES_AUTOPROBE
#
if [ "$SYS_MODULES_AUTOPROBE" = "yes" ]
then
   # autoprobe
   echo "autoprobe"
else
   # insmods
   if [ -n "$SYS_INSMOD_MODULES" ]
   then
      echo "$SYS_INSMOD_MODULES" | while read  i_mod
      do
         if [ -n "$i_mod" ]
         then
            echo "insmod $i_mod"   
         fi
      done
   fi
fi
#
#
# START CARDMGR IF ANY GIVEN PCMCIA MODULES
#
if [ -n "$SYS_PCMCIA_MODULES" ]
then
    echo "start_pcmcia"
fi
echo "AUTO_KERNEL $SYS_INST_KERNEL"
#
# The partition files
#
#
# Should some partitions be removed
if [ "$SYS_PARTITION_REMOVE" ]
then
	echo "$SYS_PARTITION_REMOVE" | awk -F= '{ print "AUTO_REMOVE_PART " $0 }' 
fi

#   get a list of var names matching SYS_PART_<adisk>
var_list_disks=$(set | awk -F= '$1 ~ /^SYS_PART_/ { print $1 }')
for VAR_X in $var_list_disks
do
   DISK_NAME=${VAR_X#SYS_PART_}
   eval CONTENTS=\"\$$VAR_X\"
   optional_path=$(echo "$CONTENTS" | awk -F= '$1 ~ /path\W*/ { print $2 }') 
   test -z "$optional_path"  && optional_path="/dev/$DISK_NAME"

#   echo "AUTO_FDISK_TABLE  /dev/$DISK_NAME \$I:/ALICE/info/$fqhn.$DISK_NAME.part.tab"
   echo "AUTO_FDISK_TABLE  $optional_path /${DISK_NAME}.ptt"
done

var_layout_disks=$(set | awk -F= '$1 ~ /^SYS_PARTITION_DISK_/ { print $1 }')
for VAR_X in $var_layout_disks
do
   DISK_NAME=${VAR_X#SYS_PARTITION_DISK_}
   eval CONTENTS=\"\$$VAR_X\"
   optional_path=$(echo "$CONTENTS" | awk -F= '$1 ~ /path\W*/ { print $2 }') 
   test -z "$optional_path"  && optional_path="/dev/$DISK_NAME"

#   echo "AUTO_FDISK_TABLE  /dev/$DISK_NAME \$I:/ALICE/info/$fqhn.$DISK_NAME.part.tab"
   echo "AUTO_KEEP_DISK $optional_path /${DISK_NAME}.plt"
done

#
# Should we run a partition script
if [ "$SYS_PARTITION_SCRIPT" ]
then
	echo "AUTO_INIT_CMD /auto_partition"	
fi

#
# The optional lvm pre call
#
if [ -n "$SYS_LVM" ]
then
   echo "AUTO_PREP_CMD (/usr/lib/YaST/alice_lvm_cmd -f /def.lvm --installmode --create_fs 2>/dev/null | grep \"^/\" )"
#   echo "########################"
#   echo $SYS_LVM
#   echo "########################"
fi

#
#
#
if [ -n "$SYS_CDROM_DEVICE" ]
then
   echo "CDROM_DEVICE $SYS_CDROM_DEVICE"
fi
#
#  For multi homed hosts...
#

if  [ -n "$NET_IP_CONFIG" ]
then
echo "$NET_IP_CONFIG" | grep -v "^#" | \
( typeset -i dev_no=0
  active_list=""
  while read device ipaddr ipmask ipbc
  do
      if [ "$ipaddr" = "dhcpclient" ]
      then
         echo "RC_CONFIG_0 IFCONFIG_${dev_no} dhcpclient"
		 if [ -z "$SYS_NAME" ]
		 then
             echo "RC_CONFIG_0 DHCLIENT_SET_HOSTNAME yes"
		 fi
      else
         echo "RC_CONFIG_0 IFCONFIG_${dev_no} ${ipaddr} broadcast ${ipbc} netmask ${ipmask} up"
         echo "RC_CONFIG_0 IPADDR_${dev_no} ${ipaddr}"
      fi
      echo "RC_CONFIG_0 NETDEV_${dev_no} ${device}"
      active_list="$active_list _${dev_no}"
      let dev_no=dev_no+1
  done
  echo "RC_CONFIG_0 NETCONFIG ${active_list}"
)
fi

if [ "$dhcp_mode" -ne 1 ]
then
   echo "RC_CONFIG_0   FQHOSTNAME   $SYS_NAME"
   echo "RC_CONFIG_0   SEARCHLIST  $DNS_SEARCHLIST"
   echo "RC_CONFIG_0   NAMESERVER  $DNS_NAMESERVER"
fi

echo "RC_CONFIG_0   MOUSE   $SYS_MOUSE_DEVICE"
echo "RC_CONFIG_0   TIMEZONE   $SYS_TIMEZONE"
echo "RC_CONFIG_0   MODEM   $SYS_MODEM_DEVICE"
echo "RC_CONFIG_0   SENDMAIL_TYPE   $SYS_MODEM_DEVICE"
echo "RC_CONFIG_0   START_GPM   $SYS_START_GPM"
echo "RC_CONFIG_0   START_INETD $SYS_START_INETD"
echo "RC_CONFIG_0   START_NAMED $SYS_START_NAMED"
echo "RC_CONFIG_0   START_ROUTED $SYS_START_ROUTED"
if [ -n "$SYS_START_HTTPD" ]; then echo "RC_CONFIG_0   START_HTTPD $SYS_START_HTTPD"; fi
if [ -n "$START_LPD" ]; then echo "RC_CONFIG_0   START_LPD $START_LPD"; fi
if [ -n "$SYS_START_NSCD" ]; then echo "RC_CONFIG_0	START_NSCD $SYS_START_NSCD"; fi
echo "RC_CONFIG_0   ROOT_LOGIN_REMOTE $SYS_ROOT_LOGIN_REMOTE"
echo "RC_CONFIG_0   ALICE_CFG_URL ${SYS_ALICE_CFG_URL}"


if [ "$SYS_HWC_GMT" = "yes" ]
then
   echo "RC_CONFIG_0    GMT  -u"
else
   echo "RC_CONFIG_0    GMT"
fi
#
# Auxilliarie sw packages
#
for i in $SYS_SW_AUXLIST
do
    echo "POST_INSTALL  \$I:/ADD-RPM/$i.rpm"
done
#
# ADDING SOME NEEDED TAGS FOR DHCP SETUP ###############
#
if [ $dhcp_mode -eq 1 ]; then
    echo "RC_CONFIG_0 ALICE_DHCP_CLASS  $dhcp_class"
    echo "RC_CONFIG_0 ALICE_CONFIG_MODE dhcp"
fi

#
#
# The alice logger facilities
#
echo "RC_CONFIG_0   FH_ADVANCED_CONFIG yes"
echo "RC_CONFIG_0   ALICE_LOGGER_FACILITY     $SYS_ALICE_LOGGER_FACILITY"
echo "RC_CONFIG_0   ALICE_LOGGER_DEST         $SYS_ALICE_LOGGER_DEST"
echo "RC_CONFIG_0   ALICE_LOGGER_ACTION_ALL   $SYS_ALICE_LOGGER_DEST"
echo "RC_CONFIG_0   ALICE_LOGGER_ACTION_ERR   $SYS_ALICE_LOGGER_DEST"
echo "RC_CONFIG_0   ALICE_LOGGER_ACTION_WARN  $SYS_ALICE_LOGGER_DEST"
#
##################### Some alice sttings for root passwd et al #############
#
echo "RC_CONFIG_0   ALICE_CONFIG   yes"
if [ -n "$SYS_SET_ROOT_PASSWORD" ]
then
    echo "RC_CONFIG_0   SET_ROOT_PASSWORD   $SET_ROOT_PASSWORD"
fi
if [ -n "$SYS_ROOT_START_PWD" ]
then
    echo "RC_CONFIG_0   ROOT_START_PWD   $ROOT_START_PWD"
fi

#
##################### CREATING AN OPTIONAL SET OF PARTITION FILES ##########
#
for VAR_X in $var_list_disks
do
   DISK_NAME=${VAR_X#SYS_PART_}
  (
   eval echo \"\$$VAR_X\"
  ) | grep -v "^path" > ${tmp_dir}/disk/${DISK_NAME}.ptt
done

# these are for the partion layout table (plt)
for VAR_X in $var_layout_disks
do
   DISK_NAME=${VAR_X#SYS_PARTITION_DISK_}
  (
   eval echo \"\$$VAR_X\"
  ) | grep -v "^path" > ${tmp_dir}/disk/${DISK_NAME}.plt
done

#
######################### CREATING AN OPTIONAL AUTO_INIT_CMD ######################
#

if [ "$SYS_PARTITION_SCRIPT" ]
then
	echo "$SYS_PARTITION_SCRIPT" > ${tmp_dir}/disk/auto_partition	
	chmod +x ${tmp_dir}/disk/auto_partition	
fi

#
######################### CREATING AN OPTIONAL LVM FILE ######################
#
if [ -n "$SYS_LVM" ]
then
  (
   echo "$SYS_LVM"
  )  > ${tmp_dir}/disk/def.lvm
fi
#
######################## CREATING A SYSLINUX.CFG FILE #######################
#
if [ -n "$SYS_LINUX_CFG" ]
then
  (
    echo "$SYS_LINUX_CFG"
  ) > ${tmp_dir}/disk/syslinux.cfg
fi
#
####################### CREATING OPTIONAL SELLIST ###########################
#
# SYS_SW_SELLIST should contain a list of standard packages, which should be
# installed additional to the packages listet in the selection file
#
if [ -n "$SYS_SW_SELLIST" ]
then
 (
   echo "# SuSE-Linux Configuration generated by ALICE (" $( date +%c ) ")"
   echo "Description: The packages"
   echo "Info: packages selected for"
   echo "automatical installation with ALICE"
   echo "Ofni:"
   echo "Toinstall:"
   echo "$SYS_SW_SELLIST"
   echo "Llatsniot:"
 ) > ${tmp_dir}/disk/auto.sel
fi

#
####################### CREATING OPTIONAL ADDLIST ###########################
#
# SYS_SW_ADDLIST should contain a list of standard packages, which should be
# installed additional to the packages listet in the selection file
#
if [ -n "$SYS_SW_ADDLIST" ]
then
 (
   echo "# SuSE-Linux Configuration generated by ALICE  (" $( date +%c ) ")"
   echo "Description: Additional packages"
   echo "Info: Additional packages selected for"
   echo "automatical installation with ALICE"
   echo "Ofni:"
   echo "Toinstall:"
   echo "$SYS_SW_ADDLIST"
   echo "Llatsniot:"
 ) > ${tmp_dir}/disk/add.sel
fi

#
###################### CREATING A MINI ALICE RC CONFIG #####################
#
(
   echo "# SuSE-Linux (alice)"
   echo "ALICE_CONFIG=yes"
   echo "SET_ROOT_PWD=${SYS_SET_ROOT_PWD}"
   echo "ROOT_START_PWD=${SYS_ROOT_START_PWD}"
   echo "ALICE_CFG_URL=${SYS_ALICE_CFG_URL}"
   echo "MY_FQHN=${fqhn}"
   echo "DEF_GW=$def_gateway"
) >  ${tmp_dir}/disk/alrc.cfg

) > ${tmp_dir}/disk/${fqhn}.info


exit 0
