#!/bin/sh
#
# Copyright (C) 2002-2003 by NCHC, Steven Shiau, K. L. Huang
# (steven@nchc.org.tw, c00hkl00@nchc.org.tw)
#
# 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, 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.
#
# Steven Shiau, steven@nchc.org.tw
#
# Ref: 
# https://listman.redhat.com/archives/psyche-list/2002-October/msg00252.html
RH_code="shrike"
target_iso="$RH_code-i386-dvd.iso"

# Check if root or not
if [ ! "$UID" = "0" ]; then
  echo
  echo "[$LOGNAME] You need to run this script \"$0\" as root."
  echo
  exit 1
fi

echo "Available url for RH9 mirror site..."
echo "1: http://linux.nchc.org.tw/redhat/linux/9/en/iso/i386/"
echo "2: ftp://linux.sinica.edu.tw/redhat/redhat-9/en/iso/i386/"
echo "3: ftp://linux.nctu.edu.tw/iso/redhat/"
echo "Please choose the mirror site..."
echo -n "[1] "
read url_site
case "$url_site" in
   2)
     RH_iso_url="ftp://linux.sinica.edu.tw/redhat/redhat-9/en/iso/i386/"
     ;;
   3)
     RH_iso_url="ftp://linux.nctu.edu.tw/iso/redhat/"
     ;;
   *)
     RH_iso_url="http://linux.nchc.org.tw/redhat/linux/9/en/iso/i386/"
     ;;
esac

if [ ! -f $RH_code-i386-disc1.iso ]; then
  echo "Downloadling the $RH_code disc1..."
  wget $RH_iso_url/$RH_code-i386-disc1.iso
fi
if [ ! -f $RH_code-i386-disc2.iso ]; then
  echo "Downloadling the $RH_code disc2..."
  wget $RH_iso_url/$RH_code-i386-disc2.iso
fi
if [ ! -f $RH_code-i386-disc3.iso ]; then
  echo "Downloadling the $RH_code disc3..."
  wget $RH_iso_url/$RH_code-i386-disc3.iso
fi

current_wd=`pwd`
tmpwd=`mktemp -d rh9.XXXXXX`
cd $tmpwd
mkdir $RH_code-i386-disc{1,2,3} $RH_code-docs

mount -o loop ../$RH_code-i386-disc1.iso $RH_code-i386-disc1
mount -o loop ../$RH_code-i386-disc2.iso $RH_code-i386-disc2
mount -o loop ../$RH_code-i386-disc3.iso $RH_code-i386-disc3

cp -a $RH_code-i386-disc1/isolinux $RH_code-i386-disc1/.discinfo .

#5.  Edit the .discinfo file, replacing the fourth line with 1,2,3,4,5
#    if you are creating an image with all five discs or with 1,2,3 if
#    you are just using the three install discs.
# put the .discinfo for RH installer program. 
perl -p -i -e "s/^1$/1,2,3/g" .discinfo

#6.  Create the iso image.  I'm separating this mkisofs command into
#    multiple lines ending with \ for clarity.  You can type it that
#    way or as a long command.  I explain this command at the end.
#mkisofs -o shrike-i386-dvd.iso \
# -b isolinux/isolinux.bin -c isolinux/boot.cat \
# -no-emul-boot -boot-load-size 4 -boot-info-table \
# -R -m TRANS.TBL \
# -x shrike-i386-disc1/.discinfo -x shrike-i386-disc1/isolinux \
# -graft-points shrike-i386-disc1 .discinfo=.discinfo isolinux/=isolinux \
# RedHat/=shrike-i386-disc2/RedHat RedHat/=shrike-i386-disc3/RedHat \
# SRPMS/=shrike-i386-disc3/SRPMS SRPMS/=shrike-i386-disc4/SRPMS \
# SRPMS/=shrike-i386-disc5/SRPMS docs/=shrike-docs

mkisofs -o ../$target_iso \
 -A "DRBL for Red Hat Linux/i386 9" -V "DRBL for Red Hat Linux/i386 9" \
 -b isolinux/isolinux.bin -c isolinux/boot.cat \
 -no-emul-boot -boot-load-size 4 -boot-info-table \
 -R -m TRANS.TBL \
 -x $RH_code-i386-disc1/.discinfo -x $RH_code-i386-disc1/isolinux \
 -graft-points $RH_code-i386-disc1 .discinfo=.discinfo isolinux/=isolinux \
 RedHat/=$RH_code-i386-disc2/RedHat RedHat/=$RH_code-i386-disc3/RedHat

# unmount all iso file
umount $RH_code-i386-disc1
umount $RH_code-i386-disc2
umount $RH_code-i386-disc3

#
echo "Cleaning temp working directory..."
rm -rf $current_wd/$tmpwd

#
echo "The output DVD iso image is: $target_iso"

