#!/bin/sh
# vim:set sw=4 ts=4:
#
# $Id: make_all,v 1.59 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_all
# Master script - starts them all :)
#
# Sections: sys
# Tags:     ALICE_PRE_SCRIPT, ALICE_POST_SCRIPT, SYS_APPLICATION_SCRIPTS
# Tags:     CONFIGMODE, SYS_UPDATE_SCRIPTS
#
test -n "$alice_dir" || alice_dir="/usr/lib/alice2/"
export alice_dir
. $alice_dir/lib/alicerc1
WELCOME

usage()
{
   echo "$_self -h | --help | -?"
   echo "$_self -fqhn"
}


# run SuSEconfig for those people who are in hurry and pressed ENTER,
# because they don't want SuSEconfig on Console 9 to be finished :-)
(
	typeset _self
	_self="SuSEconfig"
	WELCOME
	/sbin/SuSEconfig --module hostname
	GOOD_BYE
)

if [ -r /etc/sysconfig/alice ]   ## alice2 stuff for SLES8
then
	. /etc/rc.status
	rc_reset
	. /etc/sysconfig/alice
else
	if [ -r /etc/rc.config ]     ## alice1 stuff for SLES7
	then
		. /etc/rc.config
	fi
fi
return=$rc_done  


# uncomment the following line, if you want make_all to show which
# files are searched...
# export CFG_DEBUG="--debug"


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

. $alice_dir/lib/alicerc2 2>&1

aquire_config_files

. $alice_dir/lib/alicerc 2>&1 
export PATH="${alice_bin_dir}:$PATH"
GET_CONFIGURATION --optional sys $CFG_DEBUG   # yes we need the data, but I want to suprtess the mutiple error messages
if [ $? -ne 0 ]
then
	FATAL NOTFOUND "ALICE config data. Check SYS_ALICE_CFG_URL"
fi
############################################################################
# And now we begin
#
# FH: do NOT source make files, because of the side effects
# (exit in make_goup exists make_all if make_group is sourced)
#. ${alice_bin_dir}/make_groups	# Create groups
#

############################################################################
# 
# MODULE_LIST: each cmd to be called must have a separate line!
#              the line is truncated at the first position of a hash (#)!!!
#              So something like 'echo "# this is a comment"' will be
#              truncated to 'echo '. 
#

#
# preinstallation-scripts
#
if [ -n "$ALICE_PRE_SCRIPT" ]; then
   MODULE_LIST="\
     #
     # the pre scripts
     #
     $ALICE_PRE_SCRIPT
     #"
fi

#
# the default modules
#
MODULE_LIST="\
$MODULE_LIST
   module_accounts
#
# begin of the system param makes
#
   make_services
   make_syslog
   make_inetd
   make_cron
   make_etc_hosts
   make_dns_client
   make_routes
   make_ssh
"

#
# applications
#
if [ -n "$SYS_APPLICATION_SCRIPTS" ]; then
   MODULE_LIST="\
     $MODULE_LIST
     #
     # the applications
     #
     $SYS_APPLICATION_SCRIPTS
     #"
fi

#
# If we are in updatemode only the specified modules should be run, no others
#
if [ -n "${CONFIGMODE}" ]; then
	MODUL_LIST="\
	#
	# modules which should be run in the Update Configuration mode
	#
	${SYS_UPDATE_SCRIPTS}
	"
fi
#
# add optional special scripts...
#
if [ -n "$ALICE_POST_SCRIPT" ]; then
   MODULE_LIST="\
     $MODULE_LIST
     #
     # and an optional alice post script
     #
     ALICE_POST_SCRIPT
     #"
fi

#
# end of the MODULE_LIST
#
touch /var/log/alice_state_warn.$$
touch /var/log/alice_state_error.$$
touch /var/log/alice_state_fatal.$$
echo "$MODULE_LIST" | while read CMDLINE
do
  # first truncate from the first hash (#) till the end of the line
  # then split the line into the command and the parameters part
  CMDLINE=${CMDLINE%%#*} 
  CMD=$(echo $CMDLINE | ( read A B; echo $A ))
  PARAMS=$(echo $CMDLINE | ( read A B; echo $B ))
  if [ -n "$CMD" ]
  then
      if [ "$CMD" == "ALICE_POST_SCRIPT" ]; then
	    alice_post_script=/tmp/alice_post_script.$$
	  	echo "$ALICE_POST_SCRIPT" > $alice_post_script
		chmod +x $alice_post_script
		$alice_post_script 2>&1
		last_rc=$?
		rm $alice_post_script
	  else
	    $CMD $PARAMS 2>&1 
	    last_rc=$?
	  fi
	  case $last_rc in
	      0 )  # everything looks well, we have nothing to do :))
		   ;;
	      1)   # the command gave at least one warning
                   printf "%s:" $CMD >> /var/log/alice_state_warn.$$
		   ;;
	      2)   # the command reported at least one error
                   printf "%s:" $CMD >> /var/log/alice_state_error.$$
		   ;;
	      3)   # the comand gave up due to a fatal error
                   printf "%s:" $CMD >> /var/log/alice_state_fatal.$$
		   ;;
	      4)   # the comand reported a FATAL CONFIGURATION STOP 
		   FATAL UNKNWN
		   ;;
	     200)  # need a reboot NOW (not implemented yet)
		   ;;
	     201)  # need a reboot after ALICE
           #
           # for some reason variables are not kept beyond the
           # scope of the "while ... do done" statement
           # -> store it in tmp file
                   NEED_REBOOT=yes
          echo -n "${NEED_REBOOT}" > /tmp/NEED_REBOOT.$$

		   ;;
	     202)  # need a network restart 
		   nohup ${alice_bin_dir}/restart_network
		   ;;
	  esac
  fi
done | tee -a $alice_log_file

#
# retrieve value of NEED_REBOOT from tmp file
#
if [ -f /tmp/NEED_REBOOT.$$ ]
then
   NEED_REBOOT=$(cat /tmp/NEED_REBOOT.$$)
   rm -f /tmp/NEED_REBOOT.$$
else 
   NEED_REBOOT=""
fi

cleanup_config_files


#
############################################################################
# Thats all
FATALS=$(cat /var/log/alice_state_fatal.$$)
ERRORS=$(cat /var/log/alice_state_error.$$)
WARNINGS=$(cat /var/log/alice_state_warn.$$)
if [ -n "$FATALS" ]
then
   MESSAGE CONFIGFAIL "$FATALS" | tee -a $alice_log_file
   return=$rc_failed 
   alice_rc=2
fi
if [ -n "$ERRORS" ]
then
   MESSAGE CONFIGERR   "$ERRORS" | tee -a $alice_log_file
   return=$rc_failed 
   alice_rc=2
fi        
if [ -n "$WARNINGS" ]
then
   MESSAGE CONFIGWARN "$WARNINGS" | tee -a $alice_log_file
   alice_rc=1
fi        
if [ "${NEED_REBOOT}" = "yes" ]
then
   MESSAGE NEEDREBOOT
   #
   # wait until floppy is removed
   # 
   echo "Reboot will be done, if no disk is in the floppy-drive"
   while dd if=/dev/fd0 count=1 bs=1 of=/dev/null 2>/dev/null ; do echo -n "."; sleep 10; done;
	echo "Reboot will be done, if no CD is in the CDROM-drive"
	if [ -n "${SYS_CDROM_DEVICE}" -a -e "${SYS_CDROM_DEVICE}" ]
	then
		while dd if="${SYS_CDROM_DEVICE}" count=1 bs=1 of=/dev/null 2>/dev/null ;
	   do echo -n "."; sleep 10; done;
	fi
   shutdown -r now
fi
#echo -e "$return" 
echo MESSAGE BYE >> $alice_log_file 
GOOD_BYE 
