#!/bin/sh
# vim:set sw=4 ts=4:
# $Id: make_boot_cd,v 1.12 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) 2004 SuSE Linux AG, Eschborn, Germany
# All rights reserved.
#
# This file is part of ALICE, which was sponsored by
# T-Online International AG
#
# Author: Fabian Herschel
#
#############################################################################
#
# make_boot_cd fqhn, fqhn, ...   # fqhn means full qualified host name
#
#############################################################
#
# DONE:
#   to be able to have one boot CD to install several machines
#   we must also be able to store. more than one kernel, initrd
#   and so on. An we need to create a isolinux.cfg with multiple
#   entries.
#
#   One problem might be, that isolinux.cfg only can handle approx
#   16 entries...
#
#   For multiple machines we should place the kernel and initrd
#   either in different dierctories (sles8, sles8sp3, ...) or with
#   special name extentions (linuxSLES8, linuxSLES8sp3, ...).
#
#   Because isolinux.bin seams to have some problems with lable
#   names longer than 16 chars, we have to cut the machine names
#
# TODO:
#   - A format string to get the lable names from the system names?
#   - An option where to load the CD template (isolinux.bin) at this
#     moment this stuff is loaded from the first install location
#     (InstRoot of the first system).
#
echo "make_boot_cd 0.3.0"

function err_exit
{
   echo $1
   clean_up
   exit 1
}

function usage
{
  cat << EOF
usage: make_boot_cd [OPTIONS] fqhn, ... 
       make_boot_cd --help
   
   fqhn means full qualified host name

	--create_control_files
	--cd_template path
	--iso_file    path	
   
EOF
   
}

#
################################## MAIN #######################
#
if [ -z "$ALICE_HOME" ]
then
	usage
	echo "ERROR: Environment variable \$ALICE_HOME is not set"
	exit 1
fi
#
#
#
if [ -z "$alice_dir" ]; then
	alice_dir=$(cd $(dirname $0)/..; pwd)
	export alice_dir
fi
#
# have a look on the comamnd line
#
export create_xml_file=0
export FQHNs=""
while [ $# -gt 0 ]
do
   case $1 in
     -? | --? | --help | -h ) 
                           usage
                           exit 1
                           ;;
	--create_control_files )
		create_xml_file=1
		;;
	--cd_template )
			export cd_template=$2
			shift
			;;
	--iso_file )
			export iso_file=$2
			shift
			;;
	--debug )
			debug="--debug"
			;;
	-* )
			echo "do not know option $1" >&2
			exit 1
			;;
	* )
			export FQHNs="$FQHNs $1"
			;;
   esac
   shift
done
#
bootcd_dir=${ALICE_HOME}/tmp/bootcd
bootcd_loader_dir=${bootcd_dir}/boot/loader
rm -rf $bootcd_loader_dir
#
# create a CD-entry for any given Host
#
echo "creating boot CD for $FQHNs" 
for fqhn in $FQHNs
do
    cd $alice_dir/lib
    . $alice_dir/lib/alicerc 
    cd $ALICE_HOME
	GET_CONFIGURATION $debug sys 
	GET_CONFIGURATION $debug network 
	GET_CONFIGURATION $debug routes 

	DISTRIB_VERSION="$SYS_OS_VENDOR-$SYS_OS-$SYS_ARCH-$SYS_OS_VERSION"
	echo DISTRIB_VERSION is ">>"$DISTRIB_VERSION"<<"
	export sys_os_short=$(echo "$SYS_OS_VERSION" | 
		awk ' /^SL/ {print $1} 
			 !/^SL/ { gsub("\\.",""); print "SL" $1}
		')

	to_be_called=make_boot_cd8
	case "$SYS_OS_VENDOR" in
		suse | SuSE | SUSE )
			# THE SUSE THING ;-)
			case "$SYS_OS" in
				linux | [Ll][Ii][Nn][Uu][Xx] )
					# THE LINUX IS THE ONLY ONE ;-)
					case "$SYS_OS_VERSION" in
						 SLES-[89]* | SLES[89]* | [89].[012] | [89].[012].* )
							export initrd_path="/boot/loader/initrd"
							export kernel_path="/boot/loader/linux"
							export yast="yast2"
							export to_be_called=make_boot_cd8
                        ;;
						* )
							echo "At this point of time os version \"$SYS_OS_VERSION\" is not supported."
							exit 2
						;;
					esac
					;;
				* ) # other os than linux
					echo "At this point of time os \"$SYS_OS\" is not supported, please define 'linux' as SYS_OS"
					exit 2
					;;
			esac
			;;
		* )	echo "At this point of time os vendor $SYS_OS_VENDOR is not supported, please define 'SuSE' as SYS_OS_VENDOR"
			exit 2
			;;
	esac


	echo "call $to_be_called"
	. $alice_dir/utils/$to_be_called
	create_the_media
done

create_iso_image
