#!/bin/sh
# vim:set sw=4 ts=4:
# $Id: make_inst_disk,v 1.89 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_inst_disk fqhn   # fqhn means full qualified host name
#
#############################################################
#
# 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
#
#############################################################
#
# Tests to be run:
# a) start with wrong hostname -> error no cfg
# b) start without hostname    -> error no hostname
# c) start with wrong method (--url) -> error wrong method
# d) start with corr hostname (Multiple tests possible)
#   d1) use bad module name    -> error module missing
#
#############################################################
#
echo "make_inst_disk 2.0.1"
dhcp_mode=0
yast="yast1"
THE_FLOPPY_DRIVE=/dev/fd0
THE_FLOPPY_DRIVE2=/dev/fd0h1440

function err_exit
{
   echo $1
   clean_up
   exit 1
}

function usage
{
  cat << EOF
usage: make_inst_disk [OPTIONS] fqhn
       make_inst_disk --help

   fqhn means full qualified host name
   imagefile is the original boot floppy image on the
   distribution server. the file MUST be placed in
   $I:/disks ($I: is the mountpoint of the distribution).

   -v, --verify
                      verify disk image
   --CD
                      create an image file in alice/tmp for a bootable CD
   --diskimage imagefile
                      use imagefile as template for the boot disk
   --debug
                      print, which config files are parsed
   --netboot
                      create objects for a netboot server. so the client
                      machine could perform a net boot.
   --pxeboot
					  create objects for a pxeboot
   --only_create_disk_image
                      create an image file of the boot floppy in
                      alice/bootdisk
   --original_image
                      do not strip the boot disk, do not add additional
                      modules, just use the original boot disk image and
                      add the needed auto isnstall files
   --url
                      this sould be an "URL" the root directory of the
                      distribution from which the boot/module disk should be
                      copied method:/[/server/]path/..

   --add_module_dir
					  if a module is not found on the bootdisk nor on the
					  module disks, the module will be taken from this directory
EOF

}
if [ -z "$ALICE_HOME" ]
then
	usage
	echo "ERROR: Environment variable \$ALICE_HOME is not set"
	exit 1
fi

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

function clean_up
{
   cd /tmp
   (
    umount $new_boot_image_mnt
    rmdir $new_boot_image_mnt
    umount $initdisk_mnt
    rmdir $initdisk_mnt
    umount $module_disk_mnt
    rmdir $module_disk_mnt
    umount $module_fs_mnt
    rmdir $module_fs_mnt
    umount $new_initdisk_mnt
    rmdir $new_initdisk_mnt
    umount $nfs_mnt
    rmdir $nfs_mnt
   ) >/dev/null 2>/dev/null
   cd -
}

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
}


#
################################## 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"
cd $ALICE_HOME

# check if templates exist
for i in classes/*_linux_*.sys.tcf ; do echo -n ; done
if test ! -r "templates/sys.tcf" -o ! -r $i ; then
	echo "templates/ or classes/*_linux_*.sys.tcf is missing!!"
	echo "Please copy the complete templates and classes"
	echo "for SuSE-Linux to your ALICE_HOME from /usr/lib/alice2/samples";
	sleep 5
fi

template_dir="./template"
info_dir="./info"
method=NFS
MODE="floppy"
debug=""
tftpboot_url="/srv/tftp"
netboot="$ALICE_HOME/netboot"
pxeboot="$ALICE_HOME/pxeboot"
pxe_or_netboot="$ALICE_HOME/netboot"
#
# have a look on the comamnd line
#
while [ $# -gt 0 ]
do
   case $1 in
        -? | --? | --help | -h )
                           usage
                           exit 1
                           ;;
        -CD | -cd | --CD | --cd        )
                           MODE="cd"
                           ;;
		--isolinux ) # maybe the name of this option will be
					     # changed, if I find a better name
						 MODE="isolinux"
						 ;;
        --only_create_disk_image )
                           MODE="image"
                           ;;
        --netboot )
                           MODE="netboot"
							pxe_or_netboot="$netboot"
							mkdir -p $netboot
                           ;;
		--pxeboot )
						    MODE="pxeboot"
							pxe_or_netboot="$pxeboot"
							 mkdir -p $pxeboot
							;;
        -URL | -url | --URL | --url    )
                           URL=$2
                           # this sould be an "URL" the root directory of the distribution
                           # from which the boot/module disk should be copied
                           # method:/[/server/]path/..
                           method=${URL%:*}
                           method=$(echo $method | tr '[:lower:]' '[:upper:]')
                           URL=${URL#*:}
                           case $method in
                                SCP ) # //server/ follows
                                               URL=${URL#//}
                                               url_server=${URL%%/*}
                                               url_path="/${URL#*/}"
                                               method=SCP
                                               ;;
                                FILE ) # /path follows
                                               URL=${URL#/}
                                               url_path="/${URL#*/}"
                                               method=FILE
                                               ;;
									* ) # // error
											echo "Method $method not handeld here."
											exit 1
											;;
                           esac
                           shift
                           ;;
        --diskimage | --bootimage )
                           boot_image=$2
                           shift
                           ;;
        --modulesimage   ) modules_image=$2
                           shift
                           ;;
        --original_image ) original_image="yes"
                           ;;
        --dhcp )          #
                          # dhcp client installation
                          # we do not know the machines name
                          # special routines are needed
                          #
                          export dhcp_mode=1
                          export dhcp_class=$2
                          shift
                          export fqhn=dhcp
                          ;;
        --debug )         #
                          # let GET_CONFIGURATION print, which files are serached and found
                          #
                          export debug="--debug"
                          ;;
        -v | --verify    ) # verify disk image
                           DISK_VERIFY=1
                           ;;
        --mkinfo         )
                           mk_info="yes"
                           ;;
		--add_module_dir )
							add_module_dir=$2
							shift
							;;
        [a-zA-Z]*        ) fqhn=$1;
                           ;;
		--only_pxe_cfg )
							export only_pxe_cfg=1
						;;
		--tftpboot_url )  # tftpboot_url=
				export tftpboot_url=$2
				shift
				;;
        *                )  
                           echo "Unknown command line option: $1"
                           usage
                           exit 1
                           ;;
   esac
   shift
done
#
# check for host parameter
#
if [ -z "$fqhn" ]
then
	usage
	exit 1
fi
#
# source library
#
cd $alice_dir/lib
. $alice_dir/lib/alicerc 
#
# load configuration
#
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
#
# check if templates and host-specific are existing
#
cd $ALICE_HOME
if [ ! -r "templates/sys.tcf" ]; then
	echo "Can not open \$ALICE_HOME/templates/sys.tcf"
	echo "Please copy the complete templates, needed classes"
	echo "for SuSE-Linux to your ALICE_HOME from /usr/lib/alice/samples";
	clean_up
	exit 1
fi
if [ ! -r "info/${fqhn}.sys.tcf" ]; then
	echo "Can not open \$ALICE_HOME/info/${fqhn}.sys.tcf"
	echo "Can not figure out what to do for host ${fqhn}."
	clean_up
	exit 1
fi

DISTRIB_VERSION="$SYS_OS_VENDOR-$SYS_OS-$SYS_ARCH-$SYS_OS_VERSION"
echo DISTRIB_VERSION is ">>"$DISTRIB_VERSION"<<"

to_be_called=make_inst_disk8
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 ;-)
				export sys_os_short=$(echo "$SYS_OS_VERSION" | awk '/^SL/ {print $1} !/^SL/ { gsub("\\.",""); print "SL" $1}')
				case "$SYS_OS_VERSION" in
					 6.[0123] | 6.[0123].* )
						export disk_path="/disks/"
						export INITDISK_GZ_NAME=initdisk.gz
						export default_boot_image="bootdisk"
						export default_modules_image="modules"
						export disk_info_path=suse/setup/descr/info
						export yast=yast1
						export to_be_called=make_inst_disk7
						;;
					 6.4 | 6.4.* )
						export disk_path="/disks/"
						export INITDISK_GZ_NAME=initrd
						export default_boot_image="bootdisk"
						export default_modules_image="modules"
						export disk_info_path=info
						export yast=yast1
						export to_be_called=make_inst_disk7
						;;
					 SLES7 | 7.[0-9] | 7.[0-9].* )
						export disk_path="/disks/"
						export INITDISK_GZ_NAME=initrd
						export default_boot_image="bootdisk"
						export default_modules_image="modules modules2"
						export disk_info_path=info
						export yast=yast1
						export to_be_called=make_inst_disk7
						;;
					 SLES-[89]* | SLES[89]* | [98].[012] | [98].[012].* )
						#
						# for SLES8 and SL 8.x we do not generate a new
						# initrd, because the hardware detectionnormaly works
						# fine and we have all normaly needed modules on the initrd
						# maybe we must readd this funktion later so we can add
						# some externel kernel modules to fix ugly hardware behaviour
						#
						export disk_path="/disks/"
						export initrd_path="/boot/loader/initrd"
						export kernel_path="/boot/loader/linux"
						export yast="yast2"
						export to_be_called=make_inst_disk8
                        ;;
					* )
						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
