#!/bin/bash
# Steven Shiau <steven@nchc.org.tw>
# License: GPL
# This script should be run in the DRBL apt repository, such as
# linux.nchc.org.tw 
# Extra PATH for genbasedir/genpkglist etc
CD_code="drbl"
RH_VER="9"
RH_code="shrike"

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

for DISC_ID in `echo 1 2 3`; do
   echo "Now we enable the $RH_ISO..."
   RH_ISO="$RH_code-i386-disc$DISC_ID"
   target_iso="apt-enabled-$RH_ISO.iso"
   
   # Check if root or not
   whoiam=$(/usr/bin/id -nu)
   #partimaged_on=$(ps -ef |grep partimaged)
   if [ "$whoiam" != "root" ]; then 
     echo "Please run this program ($0) as root!"	
     exit 1
   fi
   #
   up_wd=`pwd`
   tmpwd=`mktemp -d RHCD.XXXXXX`
   mkdir -p $tmpwd/RedHat/apt
   mkdir $RH_ISO
   mount -o loop $RH_ISO.iso $RH_ISO
   
   # make it like in the CD directory architecture
   ln -fs $up_wd/$RH_ISO/RedHat/RPMS $up_wd/$tmpwd/RedHat/RPMS
   
   # all the file in $up_wd/$tmpwd/RedHat/apt/ is symbolic link... RPMS.update is
   # regular directory, but the files inside it are also symbolic link.
   cd $up_wd/$tmpwd/RedHat/apt/
   ln -fs ../RPMS RPMS.os
   
   cd $up_wd/$tmpwd/RedHat/apt/
   # create the apt dep, i.e. make it apt-enabled.
   # Put the symlinks for the base OS packages
   echo "Generates the contents of the base/ directory for apt repository, by creating the pkglists and the hash file..."
   echo "This will take a few minites..."
   # Generate APT indexes
   genbasedir --flat --bloat `pwd` os
   
   ####################################
   ########### Remake hash ############
   ####################################
   genbasedir --hashonly `pwd` os 
   
   cd $up_wd
   ## The first disc must be bootable, disk2,3 must also contain .discinfo
   #if [ "$DISC_ID = "1" ]; then
   #  cp -a $RH_ISO/isolinux $RH_code-i386-disc1/.discinfo .
   #else
   #  cp -a $RH_ISO/.discinfo .
   #fi
   
   # Create the iso image file
   # The first disc must be bootable, disk2,3 must also contain .discinfo
   if [ "$DISC_ID" = "1" ]; then
      cp -a $RH_ISO/isolinux .
      mkisofs -o $target_iso \
       -A "apt-enabled Red Hat $RH_VER DISC $DISC_ID" \
       -V "apt-enabled Red Hat $RH_VER DISC $DISC_ID" \
       -R -m TRANS.TBL \
       -b isolinux/isolinux.bin -c isolinux/boot.cat \
       -no-emul-boot -boot-load-size 4 -boot-info-table \
       -x $RH_code-i386-disc$DISC_ID/isolinux \
       -graft-points $RH_ISO \
        isolinux/=isolinux \
        RedHat/apt/=$up_wd/$tmpwd/RedHat/apt
        #RedHat/=$up_wd/$tmpwd/RedHat
   else
      # for disc 2, 3, not bootable
      mkisofs -o $target_iso \
       -A "apt-enabled Red Hat $RH_VER DISC $DISC_ID" \
       -V "apt-enabled Red Hat $RH_VER DISC $DISC_ID" \
       -R -m TRANS.TBL \
       -graft-points $RH_ISO \
        RedHat/apt/=$up_wd/$tmpwd/RedHat/apt
        #RedHat/=$up_wd/$tmpwd/RedHat
   fi
   
   # unmount all iso file
   umount $up_wd/$RH_ISO
   
   # Clean the tmp working directory
   echo "Cleaning the tmp working directory..."
   rm -rf $up_wd/$tmpwd $up_wd/RPMS $up_wd/$RH_ISO $up_wd/isolinux

done
