# $Id: make_boot_cd8,v 1.13 2005/04/11 14:14:34 fabian Exp $
# vim:set sw=4 ts=4:
#
#############################################################################
#
# 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) 2002,2003 SuSE Linux AG, Eschborn, Germany
# All rights reserved.
#
# This file is part of ALICE
#
# Author: Fabian Herschel
#
#############################################################################
#
# NOTES:
#
#############################################################################
# 

function err_exit
{
   echo $1
   echo SKIP clean_up
   exit 1
}

function get_file
{
# echo "### DBG: get_file $* ###"
    source="$url_path/$1"
    dest=$2
	#
    # TODO: 
	#    - ggf so umstellen, dass verschiedene URLS verwendbar sind
	#    - place this funtion in a lib
	#    - more protocols (FTP, TFTP, HTTPS, ...)
	#
    case $method in
		HTTP )
#				echo "### DBG: GET http://$url_server/$source ###" 
				GET "http://$url_server/$source" > $dest
				;;
        NFS )
               cp $source $dest
               ;;
        SCP )
               scp $url_server:$source $dest
               ;;
        FILE )
               cp $add_path$source $dest
               ;;
          * )  err_exit "Method $method not implemented yet"
               ;;
    esac
}

create_the_media()
{
	#
	#########################################################
	#
	if [ -z "$alice_dir" ]; then
		alice_dir=$(cd $(dirname $0)/..; pwd)
		export alice_dir
	fi
	cd $alice_dir/lib
	. $alice_dir/lib/alicerc 
	cd -
	#
	###############################################################
	#
	# Set some variables
	#
	NFS_SERVER=$(get_inst_server)
	NFS_DIR=$(get_inst_directory)
	nfs_mnt=/tmp/mnt$$.dist
	#
	#
	if [[ -z "${cd_template}" ]]
	then
		method=$(echo "$SYS_INST_PROTOCOL" | tr '[:lower:]' '[:upper:]')
		add_path=""
	else
		method="FILE"
		add_path="$cd_template"
	fi
	#
	# TODO: HANDLING OF FTP, HTTPS, TFTP(!), ???
	#
	case "$method" in
		HTTP )
			#
			#
			#
			url_server=$NFS_SERVER
			url_path=$NFS_DIR
			;;
		NFS )
			#
			# create a mout point and mount the dist from the i-server
			#
			url_path=$nfs_mnt
			mkdir -p $nfs_mnt
			if [ "$method" = "NFS" ]
			then
			   echo mount ${NFS_SERVER}:${NFS_DIR} $nfs_mnt
			   mount ${NFS_SERVER}:${NFS_DIR} $nfs_mnt
			fi
			;;
	esac
	#
	#
	#
	if [ -z "$NET_INST_DEV" ]
	then
		echo "NET_INST_DEV is not set " >&2
	else
		if [ "$dhcp_mode" -ne 1 ]
		then
			# read the install interface settings
			tmp_var=$( echo "$NET_IP_CONFIG" | grep "$NET_INST_DEV" )
			declare -a NET_CFG
			NET_CFG=($tmp_var)
			net_interface=${NET_CFG[0]}
			net_addr=${NET_CFG[1]}
			net_addr_hex=$(echo $net_addr | awk -F\. '{ printf "%02X%02X%02X%02X\n",$1,$2,$3,$4}')
			net_mask=${NET_CFG[2]}
			net_broadcast=${NET_CFG[3]}
			echo "NET_INST_DEV <" $NET_INST_DEV ">"
			echo "net_addr     <" $net_addr     ">"
			echo "net_addr_hex <" $net_addr_hex ">"
		 fi
	fi
	if [ -n "$DEF_GATEWAY" ]
	then
		gateway=" gateway=$DEF_GATEWAY"
	fi
	#
	#################################################################
	# 
	# Store the XML File on the CD
	#
	# OPTIONALY:
	#    create the XML file and place it on the boot CD
	#
	echo "### DBG: create_xml_file $create_xml_file ###"
	if [ $create_xml_file -eq 1 ]
	then
		xml_dir="yast.cfg"
		mkdir -p ${bootcd_dir}/$xml_dir
		echo "Calling alice2autoyast.pl"
		#
		# TODO:
		#   set the variable xml_dir to a path on the CD
		#   test the access of the XML file on the CD
		#cd $alice_dir
		$alice_dir/utils/alice2autoyast.pl --host $SYS_NAME \
			--alice_home=$ALICE_HOME \
			--xml_file=${bootcd_dir}/$xml_dir/${net_addr_hex}.xml \
			|| err_exit "can't create ${bootcd_dir}/$xml_dir/${net_addr_hex}.xml"
		echo "Control File for $SYS_NAME is placed under $xml_dir/${net_addr_hex}.xml"
		cdrom_device=${SYS_CDROM_DEVICE##*/}
		autoyast=" autoyast=device://${cdrom_device}/${xml_dir}/${net_addr_hex}.xml"
	else
		if [ -n "$SYS_AUTOYAST_URL" ]
		then
			autoyast=" autoyast=$SYS_AUTOYAST_URL"
		else
			autoyast=" autoyast=$SYS_INST_PROTOCOL://$NFS_SERVER$NFS_DIR/yast.cfg/$net_addr.xml"
		fi
	fi
	#
	#################################################################
	#
	# Get isolinux.cfg, kernel and initrd, if not already done
	#
	mkdir -p $bootcd_loader_dir/${sys_os_short}
	if [ ! -e ${bootcd_loader_dir}/isolinux.cfg ]
	then
		get_file /boot/loader/isolinux.cfg ${bootcd_loader_dir}/isolinux.cfg || err_exit "Getting isolinux.cfg failed"
	fi
	if [ ! -e ${bootcd_loader_dir}/isolinux.bin ]
	then
		get_file /boot/loader/isolinux.bin ${bootcd_loader_dir}/isolinux.bin
	fi
	if [ ! -e ${bootcd_loader_dir}/bootlogo ]
	then
		get_file /boot/loader/bootlogo	${bootcd_loader_dir}/bootlogo
	fi
	if [ ! -e ${bootcd_loader_dir}/message ]
	then
		get_file /boot/loader/message   ${bootcd_loader_dir}/message
	fi
	if [ ! -e ${bootcd_loader_dir}/memtest ]
	then
		get_file /boot/loader/memtest   ${bootcd_loader_dir}/memtest
	fi

	the_initrd=i${sys_os_short}
	the_kernel=k${sys_os_short}

	if [ ! -e ${bootcd_loader_dir}/${the_initrd} ]
	then
		get_file $initrd_path ${bootcd_loader_dir}/${the_initrd} || err_exit "Getting initrd failed"
	fi
	if [ ! -e ${bootcd_loader_dir}/${the_kernel} ]
	then
		get_file $kernel_path ${bootcd_loader_dir}/${the_kernel}  || err_exit "Getting linux  failed"
	fi
	#
	# assample the kernel append line (TODO: some of this stuff could
	#     also be placed inside a CD/info
	#     have to test if we need to place info inside the initrd)
	#
    label_line="label ${SYS_NAME:0:11} ..."
	kernel_line="kernel ${the_kernel}"
	append_line="append initrd=${the_initrd}"
	append_line="$append_line install=$SYS_INST_PROTOCOL://$NFS_SERVER$NFS_DIR"
	append_line="$append_line ip=$net_addr netmask=$net_mask $gateway"
        append_line="$append_line $autoyast"
	#
	#################################################################
	#
	#
	# now, after we know what to place in the isolinux.cfg, let us do the work
	#
	if [ -f ${bootcd_loader_dir}/isolinux.cfg.in ]
	then
		awk '
			$1 == "@@APPEND@@" { print "	append " append_line }
			$1 != "@@APPEND@@" { print $0 }
		' append_line="${append_line}" \
		${bootcd_loader_dir}/isolinux.cfg.in \
		> ${bootcd_loader_dir}/isolinux.cfg

	elif [ -f ${bootcd_loader_dir}/isolinux.cfg ]
	then
		(
			echo
			echo $label_line
			echo $kernel_line
			echo $append_line
			echo
		) >> ${bootcd_loader_dir}/isolinux.cfg
	else
		echo "ERROR: Neither \`boot/loader/isolinux.cfg.in nor \`boot/loader/isolinux.cfg found -- Exiting in error."
	fi
	if [ "$method" = "NFS" ]
	then
		echo umount $nfs_mnt
		umount $nfs_mnt
		rmdir $nfs_mnt
	fi
}

create_iso_image()
{
	#
	# create the iso image tree
	#
	cd ${ALICE_HOME}/tmp
	cd_dir=bootcd
	iso_linux_bin=boot/loader/isolinux.bin
	if [ -z "$iso_file" ]
	then
		iso_file=${ALICE_HOME}/tmp/bootcd.iso
	fi
	#
	# create sort file, so catalog and bin are placed to the
	# beginning of the CD (needed for some BIOS versions)
	#
	SORTFILE=/tmp/$$.SORT
	touch $SORTFILE
	(
	        echo "$cd_dir/boot.cat 3"
            find $cd_dir/boot/loader -printf "%p 1\n"
            # last priority wins
            echo "$cd_dir/$iso_linux_bin 2"
	) > $SORTFILE
cat $SORTFILE
	#
	#
	#
	mkiso_cmd="mkisofs -o $iso_file\
                -sort $SORTFILE \
                -b $iso_linux_bin -c boot.cat \
                -no-emul-boot -boot-info-table -boot-load-size 4 \
                -r $cd_dir"
	echo "$mkiso_cmd"
	$mkiso_cmd
	cd -
	rm -f $SORTFILE
}
