#!/bin/bash
# vim:set sw=4 ts=4:
#
# $Id: make_inst_cd,v 1.39 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,2001 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_cd
#
#############################################################
# NOTES:
#
#    TODO: make a copy of the MD5SUMS file in each directory
#    TODO: make a copy of packages named in SYS_SW_AUXLIST
#    TODO: At the moment only packages (if you use a selection file)
#          between the tags Toinstall: and Llatsniot: are copied.
#          But there are also packages between tags like:
#          Toinstall.german: and Llatsniot.german:
#          So we need something like a Language variable which decides this.
#          Maybe SYS_LANG is NOT the correct one, so you could an english
#          system with german SW
#    TODO: call build_mini_dist.pl instead of copying the files itself
#          build_mini_dist.pl hat a bit more stable and direct interface
#          to find and copy the correct distribution files. build_mini_dist.pl
#          ready the source dists description files instead of searching the
#          files like *.rpm ans so on.
#
#    only the selection file AUTO_INSTALL is used
#    all others (ADD_INSTALL) are ignored!!
#
#    $D: in paths (used in info files) is NOT supported yet
#
#    The script has to be started in the main directory of
#    ALICE (i.e. /root/ALICE).
#
#    The script must be started as user root
#
#    The informations about the nfs distribution server
#    must be placed in the info file. But the Bootmode
#    MUST be "CD" (not "Net") otherwise the machine will
#    not use the CD for installation
#
#    ALICE should be placed on the distribution server
#    in the main directory of the distribution!!
#
#    This script is for i386 fist the arch is coded
#    by arch=i386. I think this could be changed easy
#
#############################################################
#
cp_and_check()
{
  $DEBUG
  typeset mode="" rc=0
  if [ $1 = "-optional" ]
  then
     mode=opt
     shift 1
  fi
  if [ "$GLOBAL_CP_SIMULATE" != "sim" ]
  then
     cp "$@" 1>>$log_file 2>&1 ; rc=$?
  fi
  if [ $rc -ne 0 ]
  then
     if [ "$mode" != "opt" ]
     then
         echo "Fatal error: failed command:"
         echo "cp \"$@\""
         exit 1
     else
         return 1
     fi
  fi
}
test -n "$DEBUG" && DEBUG="set -x"
ALICE_RELEASE=""
GLOBAL_CD_SIMULATE=""
$DEBUG  
echo "make_inst_cd 0.7.0"

if [ -z "$alice_dir" ]; then
	alice_dir=$(cd $(dirname $0)/..; pwd)
	export alice_dir
fi
#
#
#
function err_exit
{
   echo $1
   exit 1
}       
function usage
{
   cat <<EOF
usage: make_inst_cd hostname options
       make_inst_cd --dhcp alice_dhcp_class options
       make_inst_cd --help

 options:
           [--dist_location mount_point] \\
           [--dev burn_dev_location]  \\
           [--cvsroot=cvs_path] [--release=cvstag] [--alice_home=local_path]\\
           [--speed recording_speed] [--only_create_iso_image]\\
           [--iso_image_file local_path] [--tmp_file_space local_path]


 burn_dev_location    device location of the scsi cd-r(w) in the cdrecord 
                      style: busno,scsiid,lunid"
                      THIS PARAMETER CAN ALSO BE ST BY THE ALICE TAG SYS_CDRW_DEVICE
 cvs_path             a local path to the local CVS servers "root" or a 
                      remote path in style host:/local_path
 hostname             full qualified host name (including the domaine name)
 local_path           a path to a localy rechable directory or file (this includes
                      NFS support but not scp-Kopies)
 mount_point          path to a localy mounted distribution as source for the
                      cd image
 speed                recording speed

 IF YOU DO NOT SPECIFY THE burn_dev_location THE CD WILL NOT BE BURNED
 IN THIS CASE AN ISO IMAGE WILL BE CREATED FOR A LATER CREATION OF CDS
 YOU CAN ALSO FORCE THIS BY USING THE SWITCH --only_create_iso_image
   
EOF
}      
#
# Some defaults
#
cd_recorder_speed=4
BURN=yes
export debug_file="/tmp/mk_inst_cd.$$.log"
echo "INFO: The log file is $debug_file"
#
# get and test the parameters
#
mk_disk_flags="-CD"
MODE=floppysimul
while [ $# -gt 0 ]
do
   case $1 in
         -? | --? | --help | -h ) usage
                           exit 1
                           ;;
         --alice_home=* )
                        ALICE_HOME=${1#--alice_home=}
                        ;;
         --alicehome=* )
                        ALICE_HOME=${1#--alicehome=}
                        ;;
         --alice_home | --alicehome )
                        ALICE_HOME=$2; shift
                        ;;
         --cp_simulate )
                        GLOBAL_CP_SIMULATE="sim"
                        ;;
         --cvs_root=* )
                        CVSROOT=${1#--cvs_root=}
                        ;;
         --cvsroot=* )
                        CVSROOT=${1#--cvsroot=}
                        ;;
         --cvs_root | --cvsroot )
                        CVSROOT=$2; shift
                        ;;
         --dev | -dev ) cd_recorder_location=$2
                        shift
                        ;;
         --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
                        mk_disk_flags="$mk_disk_flags --dhcp $dhcp_class"
                        ;;         
         --dist_location | -dmp ) DIST_MOUNT_POINT=$2
                        mk_disk_flags="$mk_disk_flags --url file:/${DIST_MOUNT_POINT}"
                        shift
                        ;;
         --iso_image_file )
                        iso_DEST=$2
                        shift
                        ;;
         --mkinfo         )
                           mk_info="yes"
                           mk_disk_flags="$mk_disk_flags --mkinfo"
                           ;;
         --only_create_iso_image )
                        BURN=no
                        ;;
         --release=* )
                        ALICE_RELEASE=${1#--release=}
                        ;;
         --release )    ALICE_RELEASE=$2; shift
                        ;;
         --repository=* )
                        ALICE_REPOSITORY=${1#--repository}
                        ;;
         --repository )
                        ALICE_REPOSITORY=$2; shift
                        ;;
         --speed | -speed ) cd_recorder_speed=$2
                        shift
                        ;;
         --tmp_file_space )
                        dest_dir=$2
                        shift
                        ;;
	     --add_module_dir )
						mk_disk_flags="$mk_disk_flags --add_module_dir $2"
						shift
						;;
		 --isolinux )
					    MODE=isolinux
						mk_disk_flags="$mk_disk_flags --isolinux"
						;;
		 --noboot )
						MODE=noboot
						# just need an other MODE than isolinux or floppysimul
						;;
        [a-zA-Z]*        ) fqhn=$1;
                        ;;
		*		)       # // error
						echo "Do not know parameter $1"
						usage
						exit 1
						;;
   esac
   shift
done      
if [ "$fqhn" = "" ]
then
   echo "Host name not given"
   usage
   exit 1
fi
#
# test some needed environment variables
#
if [ -z "$ALICE_HOME" ]
then
   echo "ALICE_HOME is not set."
   exit 1
fi
cd $ALICE_HOME

#
# load the alice libary and variables
. $alice_dir/lib/alicerc 2>>${debug_file} 1>>${debug_file}

case "$alice_config_mode" in
     sys_style )  mk_info="yes"
                  GET_CONFIGURATION sys 1>>${debug_file} 2>>${debug_file}
                  ;;
     info_style ) mk_info="no"
                  ;;
esac

#
# Create directories for the creation of the CD
#
if [ -z "$tmp_dir" ]
then
   tmp_dir="${ALICE_HOME}/tmp"
fi
if [ -z "$ALICE_CD_TMP" ]
then
   ALICE_CD_TMP="${tmp_dir}/cd"
fi
mkdir -p $ALICE_CD_TMP/files $ALICE_CD_TMP/cdImage 2>>${debug_file}
if [ ! \( -d $ALICE_CD_TMP/files -o -L $ALICE_CD_TMP/files \) ]
then
   echo "Fatal error: Can not find $ALICE_CD_TMP/files"
   exit 1
fi
if [ ! \( -d $ALICE_CD_TMP/cdImage -o -L $ALICE_CD_TMP/cdImage \) ]
then
   echo "Fatal error: Can not find $ALICE_CD_TMP/cdImage"
   exit 1
fi

#
# get or use the repoitory (the alice configuration files)
#
if [ -n "$CVSROOT" -a -n "$ALICE_RELEASE" ]
then
   #
   # load the alice cvs repository
   #
   echo "INFO: get new configuration files from the CVS..."
   $alice_dir/utils/get_alice_config --alice_home="$ALICE_HOME" --cvsroot="$CVSROOT" --release="$ALICE_RELEASE" \
       1>>${debug_file} 2>>${debug_file}
else
   #
   # use the local repository 
   # 
   echo "INFO: using the local alice repository in $ALICE_HOME"
fi

#
# try to get some config variables of the burning machine :)
#
if [ -z "$cd_recorder_location" -a "$BURN" = "yes" ]
then
   cd_recorder_location=$(
      fqhn=$(hostname -f)
      GET_CONFIGURATION sys 1>>${debug_file} 2>>${debug_file}
      GET_CONFIGURATION sys 1>>${debug_file} 2>>${debug_file}
      test -n "$SYS_CDRW_DEVICE" && echo "$SYS_CDRW_DEVICE"
   )
   if [ -z "$cd_recorder_location" ]
   then
	   echo "Try to figure out the scsi rw device using the local machine tags (dev=$cd_recorder_location)"
   else
	   BURN=no
	   echo "Warning: Missing the cd_recorder_location (i.e. -dev 0,4,0)"
       echo "The iso image will be created. You can use cdrecord to write a CD."
   fi
fi
#
# get some infos from the info file of the host
# and mount the distribution directory from the
# nfs installation server
#
log_file=$debug_file
info_dir=$ALICE_HOME/info
info_file="${info_dir}/${fqhn}.info"
#
# create the correct boot disk 
#
echo "Creating boot area (starting make_inst_disk)"
#${alice_dir}/utils/make_inst_disk $mk_disk_flags $fqhn   1>>$log_file 2>&1
if [ $dhcp_mode -ne 1 ]
then
   ${alice_dir}/utils/make_inst_disk $mk_disk_flags $fqhn 1>>${debug_file} 2>>${debug_file}
else
   ${alice_dir}/utils/make_inst_disk $mk_disk_flags 1>>${debug_file} 2>>${debug_file}
fi
if [ $? -ne 0 ]
then
   echo "ERROR: make_inst_disk failed"
   echo "Please have a look into the log file ${debug_file}"
   exit 1
fi
#
# if -dmp is set the distribution should already be mounted,
#   in the other case the distribution is mounted from the nfsserver/nfsdirectory
#   the nfsserver/nfsdirectory are defined in the machines info file
#
if [ -n "$DIST_MOUNT_POINT" ]
then
   source_dist=$DIST_MOUNT_POINT
   #
   # get the correct selection file. In info files $I: is a palceholder
   # for the installation media, so we have to replace that by the directory
   # where we have mounted the distribution
   # $D replaces the boot disk. We do not support this here!
   #
   ALICE_BASE=$(dirname $ALICE_HOME)
   # 
   # 
   # 
   case $alice_config_mode in
        sys_style ) #
                    # tag <SYS_SW_SELECTION> contains a path to the selection file                     
                    # tag <SYS_SW_SELLIST>  contains a list of packages
                    sel_file=$(echo ${SYS_SW_SELECTION} | sed -e "s,\$I:,$DIST_MOUNT_POINT,")
##echo "1:  " $sel_file "-- hit enter"
##read
                    ;;

        info_style) #
                    sel_file=$(awk '$1 == "AUTO_INSTALL" { print $2 }' $info_file ) 
                    sel_file=$(echo $sel_file | sed -e "s,\$I:,$ALICE_BASE,")
                    ;;
   esac
   #
   # echo $sel_file
else
   nfs_mnt=/tmp/mnt$$.dist
   mkdir $nfs_mnt
   test ! -d $nfs_mnt && ( echo "Fatal error: Can not create dir $nfs_mnt"; exit 1)
   #
   #
   case  $alice_config_mode in
        sys_style ) #
                    # tag <SYS_INST_SERVER> contains the default inst server
                    # tag <SYS_INST_SEVER_vendor_os_version> contains a special
                    #     inst server (prio higher than <SYS_INST_SERVER>)
                    # tag <SYS_INST_DIR> contains the default inst dir
                    # tag <SYS_INST_DIR_vendor_os_version> contains a special
                    #     inst dir (prio higher than <SYS_INST_DIR> 
                    #
                    # first use the standard tags
                    #    note that in that standard string four markers are allowed:
                    #      %vendor
                    #      %os
                    #      %arch   (only intel is supported so far, so this is left empty)
                    #      %version
                    #
                    test -n "$SYS_INST_SERVER" && NFS_SERVER=$(expand_alice_var "$SYS_INST_SERVER")
                    test -n "$SYS_INST_DIR" && NFS_DIR=$(expand_alice_var "$SYS_INST_DIR")
                    #
                    # and now try the version specific tags
                    # 
                    S_VAR_NAME=$(echo "SYS_INST_SERVER_${SYS_OS_VENDOR}_${SYS_OS}_${SYS_OS_VERSION}" | tr "." "_")
                    eval SYS_INST_SERVER1=\$${S_VAR_NAME}
                    test -n "$SYS_INST_SERVER1" && NFS_SERVER=$SYS_INST_SERVER1
                    #
                    S_VAR_NAME=$(echo "SYS_INST_DIR_${SYS_OS_VENDOR}_${SYS_OS}_${SYS_OS_VERSION}" | tr "." "_")
                    eval SYS_INST_DIR1=\$${S_VAR_NAME}
                    test -n "$SYS_INST_DIR1" && NFS_DIR=$SYS_INST_DIR1
					#
					# get the name of the optional selection file...
					#
					if [ -n ${SYS_SW_SELECTION} ]
					then
                       sel_file=$(echo ${SYS_SW_SELECTION} | sed -e "s,\$I:,${nfs_mnt},")
##echo "2: " $sel_file "-- hit enter"
##read
					fi
					#
                    #
                    ;;
        info_style) # 
                    NFS_SERVER=$(awk '$1 == "Server:" { print $2 }' $info_file )
                    NFS_DIR=$(awk '$1 == "Serverdir:" { print $2 }' $info_file )
                    #
                    # get the correct selection file. In info files $I: is a palceholder
                    # for the installation media, so we have to replace that by the directory
                    # where we have mounted the distribution
                    # $D replaces the boot disk. We do not support this here!
                    #
                    sel_file=$(awk '$1 == "AUTO_INSTALL" { print $2 }' $info_file ) 
                    sel_file=$(echo $sel_file | sed -e "s,\$I:,$nfs_mnt,")
                    # echo $sel_file
                    ;;
   esac
   #
   # mount the distribution
   #
   mount ${NFS_SERVER}:${NFS_DIR} $nfs_mnt 1>>$log_file 2>&1 
   if [ $? -ne 0 ]; then
       echo "Fatal error: can not mount ${NFS_SERVER}:${NFS_DIR} to $nfs_mnt"; exit 1 
   fi
   source_dist="$nfs_mnt"
fi

auto_disk="${tmp_dir}/auto_boot_image"
#
# dest_dir must NOT be a realtive path!!
#
if [ -z "$dest_dir" ]
then
   dest_dir="$ALICE_CD_TMP/Cdtmp"
fi
if [ -n "$SYS_ARCH" ]
then
   arch=$SYS_ARCH
else
   arch="i386"
fi
#
# set the default params for the iso image
#
if [ -z "$iso_DEST" ]
then
   iso_DEST="$ALICE_CD_TMP/cdImage/SuSEAutoBootCD"
fi
iso_PARAMS=" -R -T "



get_install_packages()
{
case $alice_config_mode in
	sys_style )  #
		if [ -n "$SYS_SW_SELLIST" ] 
		then
			echo "$SYS_SW_SELLIST"
		else
			if [ -n "$SYS_SW_SELECTION" ]
			then
				cat $sel_file | awk '
					$1 == "Toinstall:" { flag=1 }
					$1 ~  /[^:]$/      { if (flag == 1 ) { print $0 } }
					$1 == "Llatsniot:" { flag=0 }'
			fi
		fi
		test -n "$SYS_SW_ADDLIST" && echo "$SYS_SW_ADDLIST"
		;;
	info_style ) #
		cat $sel_file | awk '
			$1 == "Toinstall:" { flag=1 }
			$1 ~  /[^:]$/      { if (flag == 1 ) { print $0 } }
			$1 == "Llatsniot:" { flag=0 }' 
		;;
esac
}

#
# Cleaning up, a prior copy
# testing the variable is maybe a bit paranoid, but a / in 
# the variable could be very fatal ;-)
#
if [ -z "$dest_dir" ]
then
   err_exit "FATAL internal error variable dest_dir has no value"
else
   if [ -d "$dest_dir" -o -L "$dest_dir" ]
   then
      cd_id_files=$(ls /$dest_dir/.S.u.S.E-dis* 2>>${debug_file} )
      if [ -n "$cd_id_files" ]
      then
         cd $dest_dir
         rm .S.u.S.E-dis*
         rm -rf ADD-*
         rm -rf disks
         rm -rf setup
         rm -rf suse
         rm -rf full-names
      else
         echo "FATAL: the directory $dest_dir is not empty and seems not to be"
         echo "       a correct CD temp directory used before."
         echo "       Please remove this directory or choose an other path using"
         echo "       --tmp_file_space and start again"
         exit 1
      fi
   else 
      if [ -e "$dest_dir" ]
      then
         echo "FATAL: $dest_dir is not a directory or link. Please remove that or use an other ALICE_CD_TMP."
         exit 1
      else 
         mkdir $dest_dir
      fi
   fi
fi
#
#
# BUILD MIN STRUKTURE AND COPY BOOT IMAGE
#
case "$MODE" in
	floppysimul )
		echo "Copy boot image"
		test -d $dest_dir ||  mkdir $dest_dir
		test -d $dest_dir/disks || mkdir -p $dest_dir/disks
		cp_and_check ${auto_disk} $dest_dir/disks/alice 
		iso_PARAMS="$iso_PARAMS -b disks/alice -c disks/alice.cat"
		;;
	isolinux )
		echo "Copy isolinux directory"
		cp_and_check -a ${tmp_dir}/isolinux $dest_dir
		iso_PARAMS="$iso_PARAMS -b isolinux/isolinux.bin -c isolinux/isolinux.cat -no-emul-boot -boot-load-size 4 -boot-info-table"
		;;
esac
test -d $dest_dir/suse/images || mkdir -p $dest_dir/suse/images 
#
# BUILD ADD STRUKTURE
#
echo "Copy installation directories"
old_pwd1=$(pwd)
cd $source_dist
cp_and_check -d --parent .S.u.S.E-dis* $dest_dir;             printf "."
# cp_and_check -a ALICE         $dest_dir;              printf "."  
cp_and_check -optional -a ADD-*       $dest_dir;              printf "."  
cp_and_check -a disks         $dest_dir;              printf "."  
cp_and_check -a setup         $dest_dir;              printf "."  
cp_and_check -a suse/images   $dest_dir/suse;         printf "."  
cp_and_check -a suse/inst-sys $dest_dir/suse;         printf "."  
mkdir -p $dest_dir/suse/setup
old_pwd2=$(pwd)
cd suse/setup
for i in $(ls)
do
  ## if [ "$i" != "du" ]
  ## then
  ##    cp_and_check -a $i $dest_dir/suse/setup/$i;     printf "."  
  ## fi
  case $i in
    . | .. | du ) # ignore that :))
                  ;;
    *           ) cp_and_check -a $i $dest_dir/suse/setup/$i;     printf "."
                  ;;
  esac
done
cd $old_pwd2
#
# COPY PACKAGE FILES
#
printf "\nCopy package files\n"
cd $source_dist 1>>$log_file 2>&1
#LIST_FULL=$( find i386 noarch src )
LIST_FULL=$( cd full-names; find i386 noarch )
for pack in $(get_install_packages)
do
    # echo "$pack:"
    # TODO: Check, if the ls pattern ${pack}-*.${arch}.rpm is ok. An example where it
    #       could went wrong: the pack name is perl and the file name is perl-bubble-1.2.3-1.i386.rpm
    #       so our pattern matches but this was NOT intended.
#
# CORRECT MATCH WOULD BE
#
#  PACKAGE-NAME        -VERSION-RELEASE.ARCH.RPM
#  NAME1-NAME2-...NAMEx-XXXXXXX-XXXXXXX.ARCH.RPM  so matches, only with XXXX variant
#
	FRPMs=full-names/$(echo "$LIST_FULL" | egrep "^.*/${pack}(-[a-zA-Z0-9.]+){2,2}$")
    test -z "$FRPM" && FRPM=$(ls full-names/noarch/${pack}-*.noarch.rpm 2>>${debug_file})
    RPM=$(ls suse/*[0-9]/${pack}.rpm 2>>${debug_file})
#   SPM=$(ls suse/*[0-9]/${pack}.spm 2>>${debug_file})
    CNT=$(ls suse/contents/${pack} 2>>${debug_file})
    #
    # older dists had an du-file for each rpm in suse/setup/du/<pkgname>.fil
    # the newer ones have a singe du-file suse/setup/du/du.dir
    # there is alo an md5sum file in this directory suse/setup/du
    # TODO: check, if du.dir is copied correctly
    #
    FIL=$(ls suse/setup/du/${pack}.fil 2>>${debug_file})
    cp_and_check -optional -d --parent $FRPMs $dest_dir || printf "\nWARNING: Package $pack missing\n"
    cp_and_check -optional -d --parent $RPM  $dest_dir
#    cp_and_check -optional -d --parent $SPM  $dest_dir
    cp_and_check -optional -d --parent $CNT  $dest_dir
    cp_and_check -optional -d --parent $FIL  $dest_dir;                 printf "."
done
cp_and_check -optional -dP suse/setup/du/du.dir $dest_dir;
cd $ALICE_HOME
mkdir -p $dest_dir/alice
cp_and_check -a templates $dest_dir/alice;      printf "."
cp_and_check -a classes   $dest_dir/alice;      printf "."
cp_and_check -a info      $dest_dir/alice;      printf "."
cp_and_check -optional -a addons $dest_dir;     printf "." 
cd $old_pwd1
#
# umount the dist, if mounted before
#

if [ -z "$DIST_MOUNT_POINT" ]
then
   #
   # unmount the distribution
   #
   umount $nfs_mnt
   rmdir $nfs_mnt    
fi
#
#############################################################################
#
# ISO IMAGE CREATION
#
#############################################################################
#
typeset -i failed=0
if [ -f "$iso_DEST" ]
then
   rm $iso_DEST
fi
printf "\nCreating the iso filesystem now, please be *very* patient\n"
mkisofs -p "Fabian Herschel" \
        -P "SuSE Linux AG" \
        -A "ALICEInstallationCD" \
        -V "alice" \
        $iso_PARAMS \
        -o $iso_DEST $dest_dir 1>>$log_file 2>&1 
if [ $? -ne 0 ]
then
   echo "return code of mkisofs is not 0"
   failed=1
fi
#
#############################################################################
#
# ISO IMAGE TESTS
#
#############################################################################
#
if [ ! -f "$iso_DEST" ]
then
   echo "the iso image has not been created"
   failed=1
else
   mkdir /tmp/isoTEST_$$
   mount -o loop $iso_DEST /tmp/isoTEST_$$
   if [ $? -ne 0 ]
   then
      echo "ERROR: unable to mount the created iso image"
      failed=1
   else
      if [ ! -d /tmp/isoTEST_$$/suse ]
      then
         echo "ERROR: sub directory suse is missing on the iso image"
         failed=yes
      else
         failed=0
      fi
      umount /tmp/isoTEST_$$ 
   fi
   rmdir /tmp/isoTEST_$$
fi

if [ $failed -eq 1 ]
then
   echo "Fatal error: mkisofs failed"
   echo "Please check the log file $log_file"
   exit 1
fi
#
#############################################################################
#
# CD CREATION
#
#############################################################################
#
if [ "$BURN" = "yes" ]
then
   echo "*********************************************************************"
   echo "Now we are ready to "burn" the CD. Please check the log file"
   echo "$log_file"
   echo "then press RETURN to burn cd"
   read X
   echo "*********************************************************************"
   echo "cdrecord is creating the CD now..."

   echo "Image is: " $iso_DEST
   cdrecord dev=$cd_recorder_location speed=$cd_recorder_speed $iso_DEST 1>>$log_file 2>&1
   if [ $? -ne 0 ]; then
       echo "FATAL error: cdrecord failed"
       echo "Please check the log file $log_file"
       exit 1
   fi
   echo "*********************************************************************"
   echo 'Everything seems to be OK now :)'
   echo "We are ready to use the CD now.  Please check the log file"
   echo "$log_file"                          
   echo "before using it!"
   echo "*********************************************************************"
else
   echo "INFO: The CD will NOT be created, you can use cdrecord to write the iso"
   echo "image to the CD."
   echo "The image file is: $iso_DEST"
   exit 0
fi
#
#############################################################################
#
