#!/bin/bash
#
# mksles9root.sh - a script to build a SLES9 install tree
# ----------------------------------------------------------------------------
# THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
# WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY
# OR FITNESS FOR A PARTICULAR PURPOSE.
# NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR
# DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
# HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
# ----------------------------------------------------------------------------
#

#+--------------------------------------------------------------------------+
function usage
# Give help
#+--------------------------------------------------------------------------+
 {
  scriptName=`basename $0`
  echo ""
  echo "$scriptName: Create a SLES9 install tree "
  echo ""
  echo "Usage: $scriptName [option] <arch>"
  echo "where [option] can be:"
  echo "    -m, --mounts "
  echo "        Mount ISO images loopback, don't copy"
  echo "where <arch> must be one of:"
  echo "    s390"
  echo "        For 31-bit SLES9 install tree"
  echo "    s390x"
  echo "        For 64-bit SLES9"
  echo ""
  echo "The 4 (6 with source) ISO images for vanilla SLES9 must exist in the current directory"
  echo "If 4 SP4 ISO images exist, they will be built into the tree sles9[x]sp4root/"
  echo "else if 3 SP3 ISO images exist, they will be built into the tree sles9[x]sp3root/"
  echo "else if 3 SP2 ISO images exist, they will be built into the tree sles9[x]sp2root/"
  echo "else if 2 SP1 ISO images exist, they will be built into the tree sles9[x]sp1root/"
  echo "else the vanilla SLES9 tree will be sles9[x]root/"
  exit
 }

#+--------------------------------------------------------------------------+
function check_for_discs
# Check if the first 4 SLES9 GA ISO images exist - if any are missing then exit
# Check for the various Service Pack ISO images
# return:
#     0: Only the 4 SLES9 GA ISO images are found
#     1: The 2 SP1 ISO images are found
#     2: The 3 SP2 ISO images are found
#     3: The 3 SP3 ISO images are found
#     4: The 4 SP3 ISO images are found
#+--------------------------------------------------------------------------+
 {
  for i in $cd1 $cd2 $cd3 $cd4; do # check for the 4 SLES9 GA ISO images
    if [ ! -f $i ]; then
      echo "The file $i is not found"
      echo "Error: cannot proceed without the following 4 SLES9 GA ISO images:"
      echo "$cd1"
      echo "$cd2"
      echo "$cd3"
      echo "$cd4"
      echo ""
      usage
    fi
  done
  retVal="0"
  if [[ -f $sp4cd1 && -f $sp4cd2 && -f $sp4cd3 && -f $sp4cd4 ]]; then
    echo "SP4 ISO images found ..."
    retVal=4
  elif [[ -f $sp3cd1 && -f $sp3cd2 && -f $sp3cd3 ]]; then
    echo "SP3 ISO images found ..."
    retVal=3
  elif [[ -f $sp2cd1 && -f $sp2cd2 && -f $sp2cd3 ]]; then
    echo "SP2 ISO images found ..."
    retVal=2
  elif [[ -f $sp1cd1 && -f $sp1cd2 ]]; then # SP1 ISOs found
    echo "SP1 ISO images found ..."
    retVal=1
  fi
  return $retVal
 }

#+--------------------------------------------------------------------------+
function mount_copy
# Mount an ISO image over the directory tmpCD/ and copy its contents
#   arg 1: the ISO image to mount
#   arg 2: the temporary directory over which to mount
#   arg 3: the directory to copy to
#+--------------------------------------------------------------------------+
 {
  ISO_image=$1
  temp_dir=$2
  target_dir=$3
  echo "  Mounting and copying $ISO_image ..."
  mount -o loop,ro $ISO_image $temp_dir
  if [ $? != 0 ]; then # unable to mount
    echo "Error: unable to mount $ISO_image over $temp_dir - exiting"
    rmdir $temp_dir
    exit
  fi
  cd $temp_dir
  rsync -HlogptrS * ../$target_dir
  cd ..
  umount $temp_dir
 }

#+--------------------------------------------------------------------------+
function copy_CDs
# Mount and copy appropriate CDs
#   arg 1: service pack level (0 = vanilla)
#+--------------------------------------------------------------------------+
 {
  echo "Copying SLES9 ISO images ..."
  temp_dir=tmpCD
  if [ ! -d $temp_dir ]; then mkdir $temp_dir; fi
  mount_copy $cd1 $temp_dir $rdir/$sles/CD1/
  mount_copy $cd2 $temp_dir $rdir/$core/CD1/
  mount_copy $cd3 $temp_dir $rdir/$core/CD2/
  mount_copy $cd4 $temp_dir $rdir/$core/CD3/
  if [ -f $cd5 ]; then  # avoids error messages if source CDs are not available
    mount_copy $cd5 $temp_dir $rdir/$core/CD4/
  fi
  if [ -f $cd6 ]; then  # avoids error messages if source CDs are not available
    mount_copy $cd6 $temp_dir $rdir/$core/CD5/
  fi
  if [ $1 = 1 ]; then # then SP1 ISO images exist
    echo "Copying SLES9 SP1 ISO images ..."
    mount_copy $sp1cd1 $temp_dir $rdir/$sp1/CD1
    mount_copy $sp1cd2 $temp_dir $rdir/$sp1/CD2
  elif [ $1 = 2 ]; then # then SP2 ISO images exist
    echo "Copying SLES9 SP2 ISO images ..."
    mount_copy $sp2cd1 $temp_dir $rdir/$sp2/CD1
    mount_copy $sp2cd2 $temp_dir $rdir/$sp2/CD2
    mount_copy $sp2cd3 $temp_dir $rdir/$sp2/CD3
  elif [ $1 = 3 ]; then # then SP3 ISO images exist
    echo "Copying SLES9 SP3 ISO images ..."
    mount_copy $sp3cd1 $temp_dir $rdir/$sp3/CD1
    mount_copy $sp3cd2 $temp_dir $rdir/$sp3/CD2
    mount_copy $sp3cd3 $temp_dir $rdir/$sp3/CD3
  elif [ $1 = 4 ]; then # then SP4 ISO images exist
    echo "Copying SLES9 SP4 ISO images ..."
    mount_copy $sp4cd1 $temp_dir $rdir/$sp4/CD1
    mount_copy $sp4cd2 $temp_dir $rdir/$sp4/CD2
    mount_copy $sp4cd3 $temp_dir $rdir/$sp4/CD3
    mount_copy $sp4cd4 $temp_dir $rdir/$sp4/CD4
  fi
  echo "Removing temporary mount point ..."
  rmdir $temp_dir
 }

#+--------------------------------------------------------------------------+
function mk_directory_structure
# Make the directory structure
#   arg 1: service pack level (0 = vanilla)
# The directory structure is as follows:
# /sles9root/
#     boot -> sles9/CD1/boot
#     content -> sles9/CD1/content
#     control.xml -> sles9/CD1/control.xml
#     media.1 -> sles9/CD1/media.1
#     core9/
#         CD1/
#         CD2/
#         CD3/
#         CD4/
#         CD5/
#     sles9/
#         CD1/
#     sp1-9/       // if the 2 SP1 ISO images exist
#         CD1/
#         CD2/
#     sp2-9/       // if the 3 SP2 ISO images exist
#         CD1/
#         CD2/
#         CD3/
#     sp3-9/       // if the 3 SP3 ISO images exist
#         CD1/
#         CD2/
#         CD3/
#     sp4-9/       // if the 4 SP4 ISO images exist
#         CD1/
#         CD2/
#         CD3/
#         CD4/
#     yast/
#         instorder
#         order
#+--------------------------------------------------------------------------+
 {
  echo "Making the directory structure ..."
  if [ -d $rdir ]; then # the subdirectory already exists - don't overwrite it
    echo "Error: root directory $rdir exists - must be removed first (rm -fr $rdir)"
    exit
  fi
  if [ ! -d $rdir/$sles/CD1 ]; then mkdir -p $rdir/$sles/CD1; fi
  if [ ! -d $rdir/$core/CD1 ]; then mkdir -p $rdir/$core/CD1; fi
  if [ ! -d $rdir/$core/CD2 ]; then mkdir -p $rdir/$core/CD2; fi
  if [ ! -d $rdir/$core/CD3 ]; then mkdir -p $rdir/$core/CD3; fi
  if [ ! -d $rdir/$core/CD4 ]; then mkdir -p $rdir/$core/CD4; fi
  if [ ! -d $rdir/$core/CD5 ]; then mkdir -p $rdir/$core/CD5; fi
  if [ ! -d $rdir/yast ]; then mkdir -p $rdir/yast; fi
  if [ $1 = 1 ]; then # SP1 ISO images exist
    if [ ! -d $rdir/$sp1/CD1 ]; then mkdir -p $rdir/$sp1/CD1; fi
    if [ ! -d $rdir/$sp1/CD2 ]; then mkdir -p $rdir/$sp1/CD2; fi
  elif [ $1 = 2 ]; then # SP2 ISO images exist
    if [ ! -d $rdir/$sp2/CD1 ]; then mkdir -p $rdir/$sp2/CD1; fi
    if [ ! -d $rdir/$sp2/CD2 ]; then mkdir -p $rdir/$sp2/CD2; fi
    if [ ! -d $rdir/$sp2/CD3 ]; then mkdir -p $rdir/$sp2/CD3; fi
  elif [ $1 = 3 ]; then # SP3 ISO images exist
    if [ ! -d $rdir/$sp3/CD1 ]; then mkdir -p $rdir/$sp3/CD1; fi
    if [ ! -d $rdir/$sp3/CD2 ]; then mkdir -p $rdir/$sp3/CD2; fi
    if [ ! -d $rdir/$sp3/CD3 ]; then mkdir -p $rdir/$sp3/CD3; fi
  elif [ $1 = 4 ]; then # SP4 ISO images exist
    if [ ! -d $rdir/$sp4/CD1 ]; then mkdir -p $rdir/$sp4/CD1; fi
    if [ ! -d $rdir/$sp4/CD2 ]; then mkdir -p $rdir/$sp4/CD2; fi
    if [ ! -d $rdir/$sp4/CD3 ]; then mkdir -p $rdir/$sp4/CD3; fi
    if [ ! -d $rdir/$sp4/CD4 ]; then mkdir -p $rdir/$sp4/CD4; fi
  fi
 }

#+--------------------------------------------------------------------------+
function mk_symbolic_links
# Make symbolic links as specified in the SLES9 install manual
#   arg 1: service pack level (0 = vanilla)
#+--------------------------------------------------------------------------+
 {
  echo "Making symbolic links ..."
  cd $rdir
  ln -fs $sles/CD1/content content
  ln -fs $sles/CD1/control.xml control.xml
  ln -fs $sles/CD1/media.1 media.1
  if [ $arch = "s390x" ]; then # symlink to fix yast installer bug
    ln -fs $core/CD1/suse
  fi
  if [ $1 = 1 ]; then # the SP1 ISO images exist
    ln -fs $sles/CD1/boot boot
    ln -fs $sp1/CD1/driverupdate
    ln -fs $sp1/CD1/linux
  elif [ $1 = 2 ]; then # the SP2 ISO images exist
    ln -fs $sles/CD1/boot boot
    ln -fs $sp2/CD1/driverupdate
    ln -fs $sp2/CD1/linux
  elif [ $1 = 3 ]; then # the SP3 ISO images exist
    ln -fs $sles/CD1/boot boot
    ln -fs $sp3/CD1/driverupdate
    ln -fs $sp3/CD1/linux
  elif [ $1 = 4 ]; then # the SP4 ISO images exist
    mkdir boot
    cp $sles/CD1/boot/root boot/root 
    cp $sp4/CD1/boot/* boot
    ln -fs $sp4/CD1/driverupdate
    ln -fs $sp4/CD1/linux
  fi
  cd ..
 }

#+--------------------------------------------------------------------------+
function clean_mounts
# Clean up loopback mount points
#   arg 1: service pack level (0 = vanilla)
#+--------------------------------------------------------------------------+
 {
  echo "Cleaning up mount points ..."
  umount $rdir/$sles/CD1/
  umount $rdir/$core/CD1/
  umount $rdir/$core/CD2/
  umount $rdir/$core/CD3/
  umount $rdir/$core/CD4/
  umount $rdir/$core/CD5/
# check for Service Packs
  if [ $1 = 1 ]; then # then SP1 ISO images exist
    umount $rdir/$sp1/CD1/
    umount $rdir/$sp1/CD2/
  elif [ $1 = 2 ]; then # then SP2 ISO images exist
    umount $rdir/$sp2/CD1/
    umount $rdir/$sp2/CD2/
    umount $rdir/$sp2/CD3/
  elif [ $1 = 3 ]; then # then SP3 ISO images exist
    umount $rdir/$sp3/CD1/
    umount $rdir/$sp3/CD2/
    umount $rdir/$sp3/CD3/
  elif [ $1 = 4 ]; then # then SP4 ISO images exist
    umount $rdir/$sp4/CD1/
    umount $rdir/$sp3/CD2/
    umount $rdir/$sp3/CD3/
    umount $rdir/$sp4/CD4/
  fi
  echo "Cleaning up remains of $rdir ..."
  rm -fr $rdir
  echo "Cleaned up - exiting"
  echo "You may need more loopback devices"
  exit
 }

#+--------------------------------------------------------------------------+
function mount_CDs
# Check that the 6 ISO images exist then mount and copy them
#   arg 1: service pack level (0 = vanilla)
#+--------------------------------------------------------------------------+
 {
  echo "Mounting ISO images loopback ..."
  echo "  Mounting $rdir/$sles/CD1/ ..."
  mount -o loop,ro $cd1 $rdir/$sles/CD1/
  if [ $? -ne 0 ]; then clean_mounts $1; fi
  echo "  Mounting $rdir/$core/CD1/ ..."
  mount -o loop,ro $cd2 $rdir/$core/CD1/
  if [ $? -ne 0 ]; then clean_mounts $1; fi
  echo "  Mounting $rdir/$core/CD2/ ..."
  mount -o loop,ro $cd3 $rdir/$core/CD2/
  if [ $? -ne 0 ]; then clean_mounts $1; fi
  echo "  Mounting $rdir/$core/CD3/ ..."
  mount -o loop,ro $cd4 $rdir/$core/CD3/
  if [ $? -ne 0 ]; then clean_mounts $1; fi
  if [ -f $cd5 ]; then
    echo "  Mounting $rdir/$core/CD4/ ..."
    mount -o loop,ro $cd5 $rdir/$core/CD4/
#    if [ $? -ne 0 ]; then clean_mounts $1; fi
  fi
  if [ -f $cd6 ]; then
    echo "  Mounting $rdir/$core/CD5/ ..."
    mount -o loop,ro $cd6 $rdir/$core/CD5/
#    if [ $? -ne 0 ]; then clean_mounts $1; fi
  fi

# check for Service Packs
  if [ $1 = 1 ]; then # then SP1 ISO images exist
    echo "Mounting SLES9 SP1 ISO images loopback ..."
    echo "  Mounting $rdir/$sp1/CD1/ ..."
    mount -o loop,ro $sp1cd1 $rdir/$sp1/CD1
    if [ $? -ne 0 ]; then clean_mounts $1; fi
    echo "  Mounting $rdir/$sp1/CD2/ ..."
    mount -o loop,ro $sp1cd2 $rdir/$sp1/CD2
    if [ $? -ne 0 ]; then clean_mounts $1; fi
  elif [ $1 = 2 ]; then # then SP2 ISO images exist
    echo "Mounting SLES9 SP2 ISO images loopback ..."
    echo "  Mounting $rdir/$sp2/CD1/ ..."
    mount -o loop,ro $sp2cd1 $rdir/$sp2/CD1
    if [ $? -ne 0 ]; then clean_mounts $1; fi
    echo "  Mounting $rdir/$sp2/CD2/ ..."
    mount -o loop,ro $sp2cd2 $rdir/$sp2/CD2
    if [ $? -ne 0 ]; then clean_mounts $1; fi
    echo "  Mounting $rdir/$sp2/CD3/ ..."
    mount -o loop,ro $sp2cd3 $rdir/$sp2/CD3
    if [ $? -ne 0 ]; then clean_mounts $1; fi
  elif [ $1 = 3 ]; then # then SP3 ISO images exist
    echo "Mounting SLES9 SP3 ISO images loopback ..."
    echo "  Mounting $rdir/$sp3/CD1/ ..."
    mount -o loop,ro $sp3cd1 $rdir/$sp3/CD1
    if [ $? -ne 0 ]; then clean_mounts $1; fi
    echo "  Mounting $rdir/$sp3/CD2/ ..."
    mount -o loop,ro $sp3cd2 $rdir/$sp3/CD2
    if [ $? -ne 0 ]; then clean_mounts $1; fi
    echo "  Mounting $rdir/$sp3/CD3/ ..."
    mount -o loop,ro $sp3cd3 $rdir/$sp3/CD3
#    if [ $? -ne 0 ]; then clean_mounts $1; fi
  elif [ $1 = 4 ]; then # then SP4 ISO images exist
    echo "Mounting SLES9 SP4 ISO images loopback ..."
    echo "  Mounting $rdir/$sp4/CD1/ ..."
    mount -o loop,ro $sp4cd1 $rdir/$sp4/CD1
    if [ $? -ne 0 ]; then clean_mounts $1; fi
    echo "  Mounting $rdir/$sp4/CD2/ ..."
    mount -o loop,ro $sp4cd2 $rdir/$sp4/CD2
    if [ $? -ne 0 ]; then clean_mounts $1; fi
    echo "  Mounting $rdir/$sp4/CD3/ ..."
    mount -o loop,ro $sp4cd3 $rdir/$sp4/CD3
    if [ $? -ne 0 ]; then clean_mounts $1; fi
    echo "  Mounting $rdir/$sp4/CD4/ ..."
    mount -o loop,ro $sp4cd4 $rdir/$sp4/CD4
    if [ $? -ne 0 ]; then clean_mounts $1; fi
  fi
 }

#+--------------------------------------------------------------------------+
function mk_order_files
# Create yast/instorder and yast/order files per SLES Install manual
#   arg 1: service pack level (0 = vanilla)
#+--------------------------------------------------------------------------+
 {
  echo "Creating yast/*order files ..."
  cd $rdir
  if [ $1 = 0 ]; then # the SP1 ISO images exist - add 3 entries
    echo "  Creating files for SLES9..."
    echo -e "/$sles/CD1 /$sles/CD1" > yast/instorder
    echo -e "/$core/CD1 /$core/CD1" >> yast/instorder
  elif [ $1 = 1 ]; then # the SP1 ISO images do not exist - add 2 entries
    echo "  Creating files for SLES9 + SP1..."
    echo -e "/$sp1/CD1 /$sp1/CD1" > yast/instorder
    echo -e "/$sles/CD1 /$sles/CD1" >> yast/instorder
    echo -e "/$core/CD1 /$core/CD1" >> yast/instorder
  elif [ $1 = 2 ]; then # the SP2 ISO images do not exist - add 2 entries
    echo "  Creating files for SLES9 + SP2..."
    echo -e "/$sp2/CD1 /$sp2/CD1" > yast/instorder
    echo -e "/$sles/CD1 /$sles/CD1" >> yast/instorder
    echo -e "/$core/CD1 /$core/CD1" >> yast/instorder
  elif [ $1 = 3 ]; then # the SP3 ISO images do not exist - add 2 entries
    echo "  Creating files for SLES9 + SP3..."
    echo -e "/$sp3/CD1 /$sp3/CD1" > yast/instorder
    echo -e "/$sles/CD1 /$sles/CD1" >> yast/instorder
    echo -e "/$core/CD1 /$core/CD1" >> yast/instorder
  elif [ $1 = 4 ]; then # the SP4 ISO images do not exist - add 2 entries
    echo "  Creating files for SLES9 + SP4..."
    echo "/sp4/CD1" > yast/instorder 
    echo "/sles9/CD1" >> yast/instorder
    echo "/core9/CD1" >> yast/instorder 
    echo "/sp4/CD1 /sp4/CD1" > yast/order 
    echo "/sles9/CD1 /sles9/CD1" >> yast/order 
    echo "/core9/CD1 /core9/CD1" >> yast/order
  fi
  cp yast/instorder yast/order
  cd ..
 }

#+--------------------------------------------------------------------------+
function merge_patches
# Merge patches from $rdir/sp-<n>/CD[1-3]/s390[x]/update/SUSE_CORE/9 to $rdir
#   arg 1: service pack level
#+--------------------------------------------------------------------------+
 {
  echo "Merging patches for service pack $1 ..."
  cd $rdir
  if [ $1 = 1 ]; then # merge service pack 1
    for i in 1 2; do
      rsync -HlogptrS $sp1/CD$i/$arch .
      rm -fr $sp1/CD$i/$arch
    done
    cd $arch/update
    ln -fs SUSE-CORE SUSE-SLES
  elif [ $1 = 2 ]; then # merge service pack 2
    for i in 1 2; do
      rsync -HlogptrS $sp2/CD$i/$arch .
      if [ $mounts = "no" ]; then rm -fr $sp2/CD$i/$arch; fi
    done
    echo "Hacking mediamap file for service pack $1 ..."
    cd $arch/update/SUSE-CORE/9/patches
    cp mediamap mediamap.orig
    sed -e 's/ 2/ 1/g' mediamap.orig > mediamap
    cd $arch/update
    ln -fs SUSE-CORE SUSE-SLES
  elif [ $1 = 3 ]; then # merge service pack 3
    for i in 1 2; do
      rsync -HlogptrS $sp3/CD$i/$arch .
      if [ $mounts = "no" ]; then rm -fr $sp3/CD$i/$arch; fi
    done
    echo "Hacking mediamap file for service pack $1 ..."
    cd $arch/update/SUSE-CORE/9/patches
    cp mediamap mediamap.orig
    sed -e 's/ 2/ 1/g' mediamap.orig > mediamap
    cd $arch/update
    ln -fs SUSE-CORE SUSE-SLES
  elif [ $1 = 4 ]; then # merge service pack 4
    for i in 1 2; do
      rsync -HlogptrS $sp4/CD$i/$arch .
      if [ $mounts = "no" ]; then rm -fr $sp4/CD$i/$arch; fi
    done
   echo "Hacking mediamap file for service pack $1 ..."
   cd $arch/update/SUSE-CORE/9/patches
   cp mediamap mediamap.orig
   sed -e 's/ 2/ 1/g' mediamap.orig > mediamap
   cd ../../../../..
  fi
  cd ..
 }

#+--------------------------------------------------------------------------+
function process_args
#
#   args: all arguments passed to the script - valid values are:
#     s390     31-bit architecture
#     s390x    64-bit architecture
#     -m       use loopback mounts for CDs, don't copy
#     --mounts use loopback mounts for CDs, don't copy
#+--------------------------------------------------------------------------+
 {
  while (( "$#" )); do
    case $1 in
      "s390")
        arch=$1
        ;;
      "s390x")
        arch=$1
        ;;
      "-m")
        mounts="yes"
        ;;
      "--mounts")
        mounts="yes"
        ;;
      *)
        echo "Error: Unrecognized parameter: $1"
        usage
    esac
    shift
  done # while there are more parms
 }

#+--------------------------------------------------------------------------+
function set_file_names 
#   args: none 
#+--------------------------------------------------------------------------+
 {
  if [[ $arch = "s390" ]]; then # set the SLES9 s390 .iso file names
  # hard-coded file names for the 6 31-bit SLES9 CD .iso images
    cd1="SLES-9-s390-RC5a-CD1.iso"
    cd2="SLES-9-s390-RC5-CD2.iso"
    cd3="SLES-9-s390-RC5-CD3.iso"
    cd4="SLES-9-s390-RC5-CD4.iso"
    cd5="SLES-9-s390-RC5-CD5.iso"
    cd6="SLES-9-s390-RC5-CD6.iso"
  # hard-coded file names for the 2 31-bit SLES9 Service Pack 1 CD .iso images
    sp1cd1="SLES-9-SP-1-s390-RC5-CD1.iso"
    sp1cd2="SLES-9-SP-1-s390-RC5-CD2.iso"
  # hard-coded file names for the 3 31-bit SLES9 Service Pack 2 CD .iso images
    sp2cd1="SLES-9-SP-2-s390-GM-CD1.iso"
    sp2cd2="SLES-9-SP-2-s390-GM-CD2.iso"
    sp2cd3="SLES-9-SP-2-s390-GM-CD3.iso"
  # hard-coded file names for the 3 31-bit SLES9 Service Pack 3 CD .iso images
    if [ -f SLES-9-SP-3-s390-GM-CD1a.iso ]; then # the latest CD1 file name exists
      sp3cd1="SLES-9-SP-3-s390-GM-CD1a.iso"
    else # try the original file name
      sp3cd1="SLES-9-SP-3-s390-GM-CD1.iso"
    fi
    sp3cd2="SLES-9-SP-3-s390-GM-CD2.iso"
    if [ -f SLES-9-SP-3-s390-RC4a-CD3.iso ]; then # the latest CD3 file name exists
      sp3cd3="SLES-9-SP-3-s390-RC4a-CD3.iso"
    else # try the original file name
      sp3cd3="SLES-9-SP-3-s390-GM-CD3.iso"
    fi
  # hard-coded file names for the 4 31-bit SLES9 Service Pack 4 CD .iso images
    sp4cd1="SLES-9-SP4-CD-s390-RC7-CD1.iso"
    sp4cd2="SLES-9-SP4-CD-s390-RC7-CD2.iso"
    sp4cd3="SLES-9-SP4-CD-s390-RC7-CD3.iso"
    sp4cd4="SLES-9-SP4-CD-s390-RC7-CD4.iso"
  elif [[ $arch = "s390x" ]]; then # set the SLES9 s390x .iso file names
  # hard-coded file names for the 6 64-bit SLES9 CD .iso images
    cd1="SLES-9-s390x-RC5a-CD1.iso"
    cd2="SLES-9-s390x-RC5-CD2.iso"
    cd3="SLES-9-s390x-RC5-CD3.iso"
    cd4="SLES-9-s390x-RC5-CD4.iso"
    cd5="SLES-9-s390x-RC5-CD5.iso"
    cd6="SLES-9-s390x-RC5-CD6.iso"
  # hard-coded file names for the 2 64-bit SLES9 Service Pack 1 CD .iso images
    sp1cd1="SLES-9-SP-1-s390x-RC5-CD1.iso"
    sp1cd2="SLES-9-SP-1-s390x-RC5-CD2.iso"
  # hard-coded file names for the 3 64-bit SLES9 Service Pack 2 CD .iso images
    sp2cd1="SLES-9-SP-2-s390x-GM-CD1.iso"
    sp2cd2="SLES-9-SP-2-s390x-GM-CD2.iso"
    sp2cd3="SLES-9-SP-2-s390x-GM-CD3.iso"
  # hard-coded file names for the 3 31-bit SLES9 Service Pack 3 CD .iso images
    if [ -f SLES-9-SP-3-s390x-GM-CD1a.iso ]; then # the latest CD1 file name exists
      sp3cd1="SLES-9-SP-3-s390x-GM-CD1a.iso"
    else # try the original file name
      sp3cd1="SLES-9-SP-3-s390x-GM-CD1.iso"
    fi
    sp3cd2="SLES-9-SP-3-s390x-GM-CD2.iso"
    if [ -f SLES-9-SP-3-s390x-RC4a-CD3.iso ]; then # the latest CD3 file name exists
      sp3cd3="SLES-9-SP-3-s390x-RC4a-CD3.iso"
    else # try the original file name
      sp3cd3="SLES-9-SP-3-s390x-GM-CD3.iso"
    fi
  # hard-coded file names for the 4 64-bit SLES9 Service Pack 4 CD .iso images
    sp4cd1="SLES-9-SP4-CD-s390x-RC7-CD1.iso"
    sp4cd2="SLES-9-SP4-CD-s390x-RC7-CD2.iso"
    sp4cd3="SLES-9-SP4-CD-s390x-RC7-CD3.iso"
    sp4cd4="SLES-9-SP4-CD-s390x-RC7-CD4.iso"
  else # the one required parameter is not correct - give help
    echo "Error: Missing parameter 's390' or 's390x'"
    usage
  fi
  # set the directory names that will be created beneath the root
  sles="sles9"
  core="core9"
  sp1="sp1-9"
  sp2="sp2-9"
  sp3="sp3-9"
  sp4="sp4"
 }

# main() - mainline code starts here - modify file names below if necessary
mounts="no"
arch="not set"
process_args $@
set_file_names # set hard coded file and directory names
echo "Making a SLES9 install tree ..."
check_for_discs
spLevel=$?
if [ $arch = "s390" ]; then
  rdir="sles9"
else
  rdir="sles9x"
fi
if [ $spLevel = 0 ]; then
  rdir="$rdir""root"  
  echo "The tree named $rdir/ will be SLES9 without any service packs ..."
elif [ $spLevel = 1 ]; then
  rdir="$rdir""sp1root"  
  echo "The tree named $rdir/ will be SLES9 + SP1 ..."
elif [ $spLevel = 2 ]; then
  rdir="$rdir""sp2root" 
  echo "The tree named $rdir/ will be SLES9 + SP2 ..."
elif [ $spLevel = 3 ]; then
  rdir="$rdir""sp3root" 
  echo "The tree named $rdir/ will be SLES9 + SP3 ..."
elif [ $spLevel = 4 ]; then
  rdir="$rdir""sp4root" 
  echo "The tree named $rdir/ will be SLES9 + SP4 ..."
fi
mk_directory_structure $spLevel
if [ $mounts = "no" ]; then
  copy_CDs $spLevel
else # use loopback mounts
  mount_CDs $spLevel
fi
mk_symbolic_links $spLevel
mk_order_files $spLevel
if [ $spLevel -gt 0 ]; then # merge the patches for the service pack
  merge_patches $spLevel
fi
echo "The install tree is built under $rdir/"
