# $Id: make_inst_disk7,v 1.6 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) 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_inst_disk7
#
#############################################################
#
# NOTES:
#
# Let us explain some of the image and mount point varaibles
#    The disk image:
#    the oraiginal boot image is loaded via a given loacation
#    the local copy of that boot image is stored  in the
#    file $new_boot_image
#    To add files to that copy the boot image is mounted.
#    This mount point is $new_boot_image_mnt
#
#    The normal boot image contains an aditional file system
#    initdisk. This filesystem contains a mini linux and also
#    a directory modules which holds the most common modules.
#    The variable initdisk_mnt refers to the mounted file
#    system.
#
#    The second disk is the module disk. This disk holds a
#    few packed minix filesystems. These filesystems holds
#    additional modules. The variable which refers to that
#    module disk file system is called module_disk_mnt
#
#    If you call he program with --original_image the initdisk
#    will *NOT* be changed. No modules (from the module disk)
#    will be added, no modules will be deleted. The Deletion
#    of modules is only needed to add add. modules, if no
#    space is left on the image. 
#
#    In the normal mode the program deletes all modules stored
#    in the initdisk and only adds all modules which are given
#    by a insmod <module> line in the machines info file.
#
#    If you want to use the new autoprobe function of the auto
#    installation, all modules *MUST* be available on the
#    boot image. And you *MUST* use the --original_image option
#    
#    If modules have to be added (or stripped) to (from) the
#    boot image, we need a new image which only contains 
#    a blank file system (so it can be packed better)
#
#    You can also use make_inst_disk to build just a simple
#    image file of the boot disk. Just use the flag --CD
#    or --only_create_disk_image
#
#
#############################################################
# 

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

# TODO: ggf so umstellen, dass verschiedene URLS verwendbar sind

    case $method in
        NFS )
               cp $source $dest
               ;;
        SCP )
               scp $url_server:$source $dest
               ;;
        FILE )
               cp $source $dest
               ;;
          * )  err_exit "Method $method not implemented yet"
               ;;
    esac
}

function build_new_initdisk
{
	#
	##########################################################################################
	#
	# try to get all needed modules (if info file has entries "insmod X")
    # in the new config method these modules are listed in the tag $SYS_INSMOD_MODULES
	#
	#    fist look, if the initdisk (on the bootdisk) has already that module
	#    than test the coresponding module-disk image. This means the looking
	#    in each packed fs in the module-disk image.
	#    we only copy needed (insmod entries) modules to the destination disk
	#    please also add dependent modules as insmod entries
	#
	# 
	test -d ${tmp_dir}/disk/the_modules || mkdir ${tmp_dir}/disk/the_modules
	gzip -d -c $new_boot_image_mnt/$INITDISK_GZ_NAME >${tmp_dir}/disk/initdisk
	mount -o loop -t minix ${tmp_dir}/disk/initdisk $initdisk_mnt
        #
        # save the info file on the initrd
        #
        cp $info_file $initdisk_mnt/info
        #
	echo "Copying the modules"
	mv $initdisk_mnt/modules/*.o ${tmp_dir}/disk/the_modules
        if [ -n "$SYS_INSMOD_MODULES" ]
        then
           THE_MODS=$SYS_INSMOD_MODULES
        else
           THE_MODS="$(awk '$1 == "insmod" { print $2 }' $info_file)"
        fi
    #
        if [ -n "$SYS_PCMCIA_MODULES" ]
        then
           if [ -n "$THE_MODS" ]
           then
              THE_MODS=$(echo "$THE_MODS"; echo "$SYS_PCMCIA_MODULES")
           else
              THE_MODS=$SYS_PCMCIA_MODULES
           fi
        fi
    #
	touch $initdisk_mnt/modules/OTHER
	test -f $initdisk_mnt/etc/need_modules_disk && rm $initdisk_mnt/etc/need_modules_disk
	echo "$THE_MODS" |
	while read the_module the_module_params
	do
	   module_found=0
	   printf "  module $the_module "
       for mod_disk in $modules_image; do
          mount -o loop -t msdos ${tmp_dir}/disk/$mod_disk $module_disk_mnt

	      if [ ! -f ${tmp_dir}/disk/the_modules/$the_module.o ]
	      then
	         for i in $module_disk_mnt/*.gz
	         do
		        file=$(basename $i)
		        file=${file%.gz}
		        gzip -d -c $i >${tmp_dir}/disk/${file}
		        mount -o loop -t minix ${tmp_dir}/disk/${file} $module_fs_mnt
		        if [ -f $module_fs_mnt/${the_module}.o ]
		        then 
		           echo "OK (found on module disk: $mod_disk)"
		           cp $module_fs_mnt/${the_module}.o $initdisk_mnt/modules
		           module_found=1
		        fi
		        umount $module_fs_mnt 
	         done
	      else
	         echo "OK (found on bootdiskimage $boot_image)"
	         cp ${tmp_dir}/disk/the_modules/$the_module.o $initdisk_mnt/modules
	         module_found=1
	      fi
	      umount $module_disk_mnt 
          if [ $module_found -eq 1 ]; then break; fi
       done
		if [ -n "$add_module_dir" ]
		then
			if [ -f $add_module_dir/$the_module.o ]
			then
				echo "OK (found on $add_module_dir)"
				cp $add_module_dir/$the_module.o $initdisk_mnt/modules
				module_found=1
			fi
		fi
	   if [ $module_found -eq 0 ]
	   then
	      echo "NOT OK (module not found)"
		  echo "Please check the SYS_INST_MODULES tag, when you do not need this module."
		  echo "Please check the standard boot and module disks, if they include this"
		  echo "module, if you need it. If you have this module available for the install"
          echo "kernel you can use the --add_module_dir <path> option to specify the path"
	      echo "of the directory containing the kernel module."
		  echo "EXIT!"
		  clean_up
		  exit 1
	   fi
	done                    
	#
    # delete some more stuff on the disk image
    #
    if  [ -n "$add_module_dir" ]
    then
            echo -e "\nRemoving console fonts and keyboard mappings"
            cd $initdisk_mnt/kbd
            for delete_it in consolefonts/* consoletrans/* keymaps/[abcefghijklmnopqrstvwxyz]*.map
            do
                rm $delete_it
            done
            cd -
            dd if=/dev/zero of=$initdisk_mnt/zero 2>/dev/null
            rm $initdisk_mnt/zero
    fi
	if [ -z "$SYS_INITRD_SIZE" ]
	then
		ddsize=2050  # (size of the init ram disk in KB )
	else
		ddsize=${SYS_INITRD_SIZE}
	fi
	if [ -z "$SYS_INITRD_INODES" ]
	then
		initrd_inodes=2000  # (number of the inodes)
	else
		initrd_inodes=${SYS_INITRD_INODES}
	fi
	#
	# creating the new initdisk file 
        # and copy it to the new boot_disk image
	#
	echo "Creating filesystem initdisk"
	dd if=/dev/zero of=${tmp_dir}/disk/new_initdisk bs=1024 count=$ddsize 2>/dev/null \
	   || err_exit "Failed to create ${tmp_dir}/disk/new_initdisk"
	mkfs -t minix -i ${initrd_inodes} ${tmp_dir}/disk/new_initdisk 1>/dev/null 2>/dev/null \
	   || err_exit "Failed to create filesystem on ${tmp_dir}/disk/new_initdisk"
	mount -o loop -t minix ${tmp_dir}/disk/new_initdisk $new_INITDISK_mnt \
	   || err_exit "Failed to mount filesystem initdisk"
	( cd $initdisk_mnt; cp -a . $new_INITDISK_mnt )
	#
	# Copy some additional files for the auto installation
	#
	if [ "$mk_info" = "yes" ]
	then
		#
		# copy the partition file
		#
		cp ${tmp_dir}/disk/*.ptt ${new_INITDISK_mnt} 2>/dev/null
		cp ${tmp_dir}/disk/*.plt ${new_INITDISK_mnt} 2>/dev/null
		#
		# copy the lvm definition file
		#
		LVM_DEF="${tmp_dir}/disk/def.lvm"
		test -f "$LVM_DEF"      && cp $LVM_DEF      $new_INITDISK_mnt
		#
		# copy the selection files
		#
		AUTO_INSTALL="${tmp_dir}/disk/auto.sel"
		test -f "$AUTO_INSTALL" && cp $AUTO_INSTALL $new_INITDISK_mnt
		ADD_INSTALL="${tmp_dir}/disk/add.sel"
		test -f "$ADD_INSTALL"  && cp $ADD_INSTALL  $new_INITDISK_mnt
		#
		# copy the alice rc config
		test -f "${tmp_dir}/disk/alrc.cfg" && cp "${tmp_dir}/disk/alrc.cfg" $new_INITDISK_mnt
		#
		# copy the auto_partition file
		cp ${tmp_dir}/disk/auto_partition ${new_INITDISK_mnt} 2>/dev/null
	fi
	#
	# BAUST
	#
	umount $initdisk_mnt      || err_exit "Failed to umount old initdisk"
	umount $new_INITDISK_mnt  || err_exit "Failed to umount new initdisk"
	if [ -f ${tmp_dir}/disk/new_initdisk.gz ]
	then
	   rm ${tmp_dir}/disk/new_initdisk.gz
	fi
	gzip --best ${tmp_dir}/disk/new_initdisk || err_exit "Failed to compress filesystem initdisk"
	case $MODE in
	# TODO: different cases :-)
#	cd floppy image netboot pxeboot
	    isolinux ) # we have to create a dir with 
				   # kernel, isolinux.bin, isolinux.cfg, initrd
				   	mkdir -p ${tmp_dir}/isolinux
					cp ${tmp_dir}/disk/new_initdisk.gz  ${tmp_dir}/isolinux/$INITDISK_GZ_NAME || err_exit "Failed to copy ${tmp_dir}/disk/new_initdisk.gz"
					cp $new_boot_image_mnt/linux        ${tmp_dir}/isolinux/linux || err_exit "Failed to copy $new_boot_image_mnt/linux"
					cp /usr/share/syslinux/isolinux.bin ${tmp_dir}/isolinux/isolinux.bin || err_exit "Failed to copy /usr/share/syslinux/isolinux.bin"
				    ;;	
		cd | floppy | image ) # we have to create a floppy image
			rm $new_boot_image_mnt/$INITDISK_GZ_NAME
			cp ${tmp_dir}/disk/new_initdisk.gz $new_boot_image_mnt/$INITDISK_GZ_NAME || err_exit "Failed to copy $INITDISK_GZ_NAME"
			;;
		netboot ) 
			cp ${tmp_dir}/disk/new_initdisk.gz $netboot/$INITDISK_GZ_NAME || err_exit "Failed to copy ${tmp_dir}/disk/new_initdisk.gz"
			cp $new_boot_image_mnt/linux	   $netboot/linux || err_exit "Failed to copy $new_boot_image_mnt/linux"
			;;	
		pxeboot )
            cp ${tmp_dir}/disk/new_initdisk.gz $pxeboot/$INITDISK_GZ_NAME || err_exit "Failed to copy ${tmp_dir}/disk/new_initdisk.gz"
            cp $new_boot_image_mnt/linux       $pxeboot/linux || err_exit "Failed to copy $new_boot_image_mnt/linux"
			;;
	esac
	# echo waiting for enter
	# read
	#
	#################### end of build new initdisk
	#
}

create_the_media()
{
	#
	################################## MAIN #######################
	#
	if [ -z "$alice_dir" ]; then
		alice_dir=$(cd $(dirname $0)/..; pwd)
		export alice_dir
	fi

	# definition of some constants:
	#
	#  the_new_dir="/floppy"
	#  the_master="AutoBootImg.Master"
	#boot_image="autodisk" 
	template_dir="./template"
	info_dir="./info"
	#MODE="floppy"
	debug=""
	netboot="$ALICE_HOME/netboot"
	pxeboot="$ALICE_HOME/pxeboot"
	pxe_or_netboot="$ALICE_HOME/netboot"
	#
	cd $alice_dir/lib
	. $alice_dir/lib/alicerc 
	cd $ALICE_HOME
	#
	case "$alice_config_mode" in
		 sys_style )  mk_info="yes"
					  GET_CONFIGURATION $debug sys 
					  GET_CONFIGURATION $debug network 
					  GET_CONFIGURATION $debug routes 
					  ;;
		 info_style )  mk_info="no";;
	esac
	#
	#
	#
	if [ -z "${tmp_dir}" ] || [ "${tmp_dir:0:5}" != "/tmp/" ]; then
		# TODO: tmp_dir sollte auch unabhaengig in make_info_file gesetzt werden
		tmp_dir="$ALICE_HOME/tmp"
	fi
	export tmp_dir
	#
	# some cleanup before start
	#
	rm -rf ${tmp_dir}/disk
	mkdir -p ${tmp_dir}/disk
	new_boot_image="${tmp_dir}/boot"
	#
	################################################################
	#
	# MAKE INFO FILE OR USE GIVEN ONE
	# SL 6x, 7x
	#
	#
	if [ "$mk_info" = "yes" ]
	then
	   if [ "$dhcp_mode" -ne 1 ]
	   then
		  $alice_dir/utils/make_info_file ${fqhn}
	   else
		  $alice_dir/utils/make_info_file --dhcp ${dhcp_class}
	   fi
	   info_file="${tmp_dir}/disk/${fqhn}.info"
	else
	   info_file="${info_dir}/disk/${fqhn}.info"
	fi
	#
	###############################################################
	#
	# Set some variables
	#
# TODO: isolinux mode (and others) should not fail, network should fail here
	NFS_SERVER=$(get_inst_server)
	NFS_DIR=$(get_inst_directory)
	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_mask=${NET_CFG[2]}
			net_broadcast=${NET_CFG[3]}
		fi
	fi
	#
	# set the url parameters
	#
	if [ -z "$url_server" ]
	then
	   url_server=$NFS_SERVER
	fi
	nfs_mnt=/tmp/mnt$$.dist
	if [ -z "$url_path" ]
	then
	   url_path=$nfs_mnt
	fi
	#
	# creating new master
	#
	echo "Creating new image from master"
	#
	##############
	#
	#
	#
	new_boot_image_mnt=/tmp/mnt$$.newimg 
	initdisk_mnt=/tmp/mnt$$.inidsk 
	module_disk_mnt=/tmp/mnt$$.moddsk
	module_fs_mnt=/tmp/mnt$$.modfs
	new_INITDISK_mnt=/tmp/mnt$$.newdsk 
	mkdir $new_boot_image_mnt
	mkdir $initdisk_mnt
	mkdir $module_disk_mnt
	mkdir $module_fs_mnt
	mkdir $new_INITDISK_mnt
	#
	# create a mout point and mount the dist from the i-server
	#
	mkdir -p $nfs_mnt
	if [ "$method" = "NFS" ]
	then
	   echo mount ${NFS_SERVER}:${NFS_DIR} $nfs_mnt
	   mount ${NFS_SERVER}:${NFS_DIR} $nfs_mnt
	fi

	#
	# set the image variables, if not set by comand line
	#
	if [ -z "$boot_image" ]
	then
	   boot_image=$default_boot_image
	fi
	if [ -z "$modules_image" ]
	then
	   modules_image=$default_modules_image
	fi
	#
	# get the image files: bootfloppy and module floppies
	#
	get_file ${disk_path}/${boot_image} ${new_boot_image} \
		 || err_exit "Getting disks failed"
	if [ "$original_image" != "yes" ]
	then
	   for mod_disk in $modules_image; do
	   get_file ${disk_path}/$mod_disk ${tmp_dir}/disk \
		 || err_exit "Getting disks failed"
	   done
	fi

	#
	# umount the dist mounted from the i-server
	#
	if [ "$method" = "NFS" ]
	then
		umount $nfs_mnt 
	fi

	echo "Mounting new image"
	mount -oloop -tmsdos $new_boot_image $new_boot_image_mnt || err_exit "mount of NewBootImg failed"
	#rm $the_new_dir/linux

	TMPSYSLX=`mktemp -q /tmp/ldlinux.sys.XXXXXX` ||
		err_exit "$0: Can't create temp file, exiting..."
	echo "Moving syslinux temporary from the disk to make space for new files"
	mv $new_boot_image_mnt/ldlinux.sys $TMPSYSLX

	#
	# creating directories
	#
	echo "Creating directory structure"
	if [ ! -d $new_boot_image_mnt/suse/setup/descr ]
	then
	   mkdir -p $new_boot_image_mnt/suse/setup/descr|| err_exit "make directory failed"
	fi

	#
	# copy local info file
	#
	echo "Copying info file"
	#
	# add the new ALICE_CONFIG variable entry
	#
	echo "RC_CONFIG_0 ALICE_CONFIG yes" >> $info_file
	cp $info_file $new_boot_image_mnt/$disk_info_path || err_exit "Copy of info file failed"
	if [ -z "$SYS_INST_KERNEL" ]
	then
	   echo "Please define the SYS_INST_KERNEL tag"
	   exit 1
	fi
	#  
	# message file
	# 
	# TODO: set the correct directory for netboot and pxeboot
	# TODO: fhbaust
	message_dest_file=$pxe_or_netboot/message
	case $MODE in
		isolinux )
				mkdir -p ${tmp_dir}/isolinux
				message_dest_file=${tmp_dir}/isolinux/message
				;;
		cd | image | floppy )
				message_dest_file=$new_boot_image_mnt/message	
				;;
		pxeboot | netboot )
				message_dest_file=$pxe_or_netboot/message
				;;
	esac
	#
	#
	#
	if [ -n "$SYS_BOOT_SCREEN" ]
	then
	   #
	   # EDIT THE TEMPLATE OF THE BOOT SCREEN
	   #
	   if [ $dhcp_mode -eq 0 ]
	   then
		  host_message=$(printf "%-59.59s"         "Host:        $SYS_NAME")
		  ipadr_message=$(printf "%-59.59s"        "IP:          $net_addr")
	   else
		  host_message=$(printf "%-59.59s"         "Class:       $dhcp_class")
		  ipadr_message=$(printf "%-59.59s"        "IP:          dhcp")
	   fi
	   bootmode_message=$(printf "%-59.59s"     "Boot:        $SYS_BOOT_MODE") 
	   kernel_message=$(printf "%-59.59s"       "Kernel:      $SYS_INST_KERNEL")

	   if [ "$SYS_BOOT_MODE" = "cdrom" ]
	   then
		   sourceserver_message=$(printf "%-59.59s" "Src:         CDROM or DVD") 
		   sourcedir_message=$(printf "%-59.59s"     "    ")
		else
		   sourceserver_message=$(printf "%-59.59s" "SrcHost:     $NFS_SERVER") 
		   sourcedir_message=$(printf "%-59.59s"     "SrcDir:      $NFS_DIR")
		fi
		  
	   if [ -n "$SYS_ALICE_CFG_URL" ]
	   then
		  cfgurl_message=$(printf "%-59.59s"     "CfgURL:      $SYS_ALICE_CFG_URL")
	   else
		  cfgurl_message=$(printf "%-59.59s"     "CfgURL:      \$I:/alice")
	   fi
	   echo "$SYS_BOOT_SCREEN" | sed -e "s/1--[^-]*--1/$host_message/" \
								 -e "s/2--[^-]*--2/$ipadr_message/" \
								 -e "s/3--[^-]*--3/$bootmode_message/" \
								 -e "s/4--[^-]*--4/$kernel_message/" \
								 -e "s/5--[^-]*--5/$sourceserver_message/" \
								 -e "s|6--[^-]*--6|$sourcedir_message|" \
								 -e "s|7--[^-]*--7|$cfgurl_message|" >${tmp_dir}/disk/message
		cp ${tmp_dir}/disk/message $message_dest_file || err_exit "Copy of file message (boot screen) failed."
	else
	   #
	   # CREATE A MINIMAL MESSAGE FILE, IF NO TEMPLATE IS DEFINED
	   #
	   echo "Creating message file"
	(
	   printf "\014\0171e\014"
	   FMT="%-79.79s\n"
	   printf $FMT "     Welcome to SuSE Linux"
	   printf $FMT "     SuSE Linux by SuSE GmbH Nuernberg"
	   printf $FMT "     ALICE by SuSE Linux Solutions AG"
	   printf $FMT " "
	   printf $FMT "     This is an automatic installation disk created by make_inst_disk."
	   printf $FMT "     Generation date: $(date)"
	 
	   printf $FMT "     Host:         $SYS_NAME "
	   printf $FMT "     IP-Address:   $net_addr "

	   printf $FMT "     SuSE Version: $SV"
	   printf $FMT "     Bootmode:     $SYS_BOOT_MODE"
	   printf $FMT "     Kernel:       $SYS_INST_KERNEL"

	   if [ "$SYS_BOOT_MODE" = "CD" ]
	   then
		  printf $FMT "     DIST-SERVER:  $NFS_SERVER"
		  printf $FMT "     DIST-DIR:     $NFS_DIR"
	   else
		  printf $FMT "     NFS-SERVER:   $NFS_SERVER"
		  printf $FMT "     NFS-DIR:      $NFS_DIR"
	   fi
	   printf $FMT " " 
	   printf $FMT " " 
	)> $message_dest_file
	fi
	#
	# optional syslinux.cfg for append parameters
	#
	case "$MODE" in 
		isolinux )
					sys_cfg_dest_file=${tmp_dir}/isolinux/isolinux.cfg
					mkdir -p ${tmp_dir}/isolinux
					;;
	    * )
					sys_cfg_dest_file=$new_boot_image_mnt/syslinux.cfg
					;;
	esac
	if [ "$mk_info" = "yes" ]
	then
	   sys_file="${tmp_dir}/disk/syslinux.cfg"
	else
	   sys_file=$(last_of $(get_config_files syslinux.cfg))
	fi
	if [ -f "$sys_file" ]
	then
	   echo "Copying syslinux configuration file"
	   cp $sys_file $sys_cfg_dest_file || err_exit "Copy of syslinux.cfg failed."
	fi  
	#
	# For yast1 do the following steps
	#	- copy an info file (if not generated)
	#	- copy the partition files (if not generated)
	#	- build the initdisk (if not supressed)
	#	- copy syslinux (ldlinux or whatever)
	#	- release the image
	#
	#
	# copy an info file, if not generated
	#
	if [ "$mk_info" != "yes" ]
	then
	   AUTO_INSTALL=$( basename $( awk '$1=="AUTO_INSTALL" { print $2 }' $info_file) )
	   if [ -f ${info_dir}/${fqhn}.$AUTO_INSTALL ]
	   then
		  echo "Copying selection file"
		  cp ${info_dir}/${fqhn}.$AUTO_INSTALL $new_boot_image_mnt/$AUTO_INSTALL
	   else
		  if [ -f ${template_dir}/$AUTO_INSTALL ]
		  then
			 echo "Copying selection template" 
			 cp ${template_dir}/$AUTO_INSTALL $new_boot_image_mnt/$AUTO_INSTALL 
		  fi
	   fi
	fi
	#
	# optional partition file(s)
	#
	if [ "$mk_info" != "yes" ]
	then
	   awk '$1 == "AUTO_FDISK_TABLE" { print $3 }' $info_file |
	   while read X
	   do
		  AFT=$(basename $X)
		  if [ -f ${info_dir}/${fqhn}.${AFT} ]
		  then
			 echo "Copying auto_fdisk_table $AFT"
			 cp ${info_dir}/${fqhn}.${AFT} ${new_boot_image_mnt}/${AFT}
		  else
			 if [ -f ${template_dir}/${AFT} ]
			 then
				echo "Copying auto_fdisk_table $AFT (template)"
				cp ${template_dir}/${AFT} ${new_boot_image_mnt}/${AFT}
			 fi
		  fi
	   done
	fi
	#
	# build the init-disk, if not supressed
	#
	if [ "$original_image" = "yes" ]
	then
	   echo "I do not touch the initdisk..."
	else
	   build_new_initdisk
	fi
	case $MODE in
		cd | image | floppy )
			echo "Moving syslinux back on the disk, hope it fits...."
			mv $TMPSYSLX $new_boot_image_mnt/ldlinux.sys ||
				err_exit "Moving of syslinux back on disk failed"
			sync; sync; sync
			echo "Releasing the new image"
			umount $new_boot_image_mnt || err_exit "Unmount $new_boot_image_mnt failed"
			;;
		pxeboot | netboot )
			;;
		isolinux )
			# this *should* not be needed
			umount $new_boot_image_mnt || err_exit "Unmount $new_boot_image_mnt failed"
	esac
	#
	# last not least place the gererated images
	#
	case $MODE in
		cd )
				cp ${new_boot_image} ${tmp_dir}/auto_boot_image   
				echo "Creating of new installation disk was successfull."
				echo "The image file is: tmp/auto_boot_image"
				;;
		floppy )
				clean_up
				sleep 3
				echo "Copy this image to a floppy"
				dd if=$THE_FLOPPY_DRIVE count=1 bs=1 of=/dev/null 2>/dev/null \
					|| ( echo Please insert disk and press ENTER; read ; sleep 3)
				dd if=${new_boot_image} of=$THE_FLOPPY_DRIVE bs=36k 2>/dev/null \
					|| err_exit "Creating disk failed. Please see log $tmp_dir/log$$"
				sleep 10
				if [ "$DISK_VERIFY" = "1" ]
				then
					echo "Verify disk image"
					dd of=${new_boot_image}.d if=$THE_FLOPPY_DRIVE2 bs=36k 2>/dev/null
					diff ${new_boot_image} ${new_boot_image}.d || err_exit "Creating disk failed. Please see log $tmp_dir/log$$"
					sleep 10
				fi
				echo "Creating of new installation disk was successfull."
				;;
		image )
				clean_up
				mv ${new_boot_image} bootdisk
				echo "Creating of new installation image was successfull."
				echo "The image file is: $(pwd)/bootdisk"
				;;
		netboot )
				#
				# etherboot start
				#
				echo "Copying objects for net boot (nbi)"
				umount $new_boot_image_mnt
				echo "Creating netboot image /tftpboot/linux.nbi.$net_addr"
				mknbi-linux -a "rw 2 linuxrc=auto" \
					  -r $netboot/initrd \
					  $netboot/linux \
					  /tftpboot/linux.nbi.$net_addr
				;;
		pxeboot )
				#
				# PXE start
				#
				echo "Copying objects for pxe boot"
				cp $pxeboot/initrd /tftpboot/initrd.$net_addr
				cp $pxeboot/linux /tftpboot/linux.$net_addr
				# TODO: pxelinux anstatt syslinux
				cp $pxeboot/syslinux.cfg /tftpboot/syslinux.cfg.$net_addr
				clean_up
				;;
	esac
	exit 0
}
