#! /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.

# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL

# Absolute directory containing the top of the apt repository 
# Apache default ROOT directory is in /var/www/html
rh_url="http://linux.nchc.org.tw"
drbl_setup_path="/redhat/desktop/download/stable/RPMS"
drbl_setup_rpm="drbl-setup-current.i386.rpm"
DRBL_RPMS_DIR="RPMS.drbl RPMS.drbl-test RPMS.drbl-unstable RPMS.updates"

# Load DRBL setting and functions
if [ ! -f "/opt/drbl/sbin/drbl-conf-functions" ]; then
  echo "Unable to find /opt/drbl/sbin/drbl-conf-functions! Program terminated!" 
  exit 1
fi
. /opt/drbl/sbin/drbl-conf-functions

#
check_if_root

#
MODE="$1"
RH_VER="$2"
DVD_ISO="$3"
if [ $# -ne 3 -o \( "$MODE" != "cd" -a "$MODE" != "net" \) ]; then
	echo "Usage: $0 [cd|net] RH_VER [yes|no]"
	echo "       cd|net: create DRBL apt repository from CD or network"
	echo "       RH_VER: Version to e create, Ex. 8.0, 9, FC1, FC2, FC3"
	echo "       yes|no: Create the DVD ISO image or not" 
	echo "Ex:    $0 net 9 yes"
	exit 1
fi

if ! which lftp &> /dev/null; then
  echo "Unable to find lftp!!! Program terminated!"
fi
#
case "$RH_VER" in
8|8.0)
      DISTRIBUTION="RedHat"
      RH_Version="8.0"
      apt_dir="redhat/apt/"
      apt_list="redhat/$RH_Version/i386"
      RH_PKG_tree="redhat/linux/$RH_Version/en/os/i386"
      core_dir="os"
      ;;
9|9.0)
      DISTRIBUTION="RedHat"
      RH_Version="9"
      apt_dir="redhat/apt/"
      apt_list="redhat/$RH_Version/i386"
      RH_PKG_tree="redhat/linux/$RH_Version/en/os/i386"
      core_dir="os"
      ;;
FC[1-3]|fc[1-3])
      VerNo=${RH_VER:2}
      DISTRIBUTION="Fedora"
      RH_Version="FC${VerNo}"
      apt_dir="fedora/apt/"
      apt_list="fedora/linux/${VerNo}/i386"
      RH_PKG_tree="fedora/linux/core/${VerNo}/i386/os"
      core_dir="core"
      ;;
esac

case "$DVD_ISO" in
[nN]|[nN][oO])
      DVD_ISO="no"
      ;;
*)
      DVD_ISO="yes"
esac


#
wd=`pwd`
# for local HD directory
www_root=$wd/redhat-${RH_Version}-apt

# create the directory tree as that of apt repository in the server
www_root_link=$wd/$RH_PKG_tree

rpmpkgs_url="$rh_url/$RH_PKG_tree"
aptrpmpkgs_url="$rh_url/$apt_dir/$apt_list/"


# Begin

echo "The RedHat version number for you select: $RH_Version"

RH_APTDIR=$www_root/
mkdir -p $RH_APTDIR || exit 1

case "$MODE" in 
    cd)
     echo "Creating DRBL apt repository from CD..."
     echo "Do you have RedHat $RH_Version CD and the DRBL CD ?"
     echo "[y/N] "
     read cd_answer
     case "$cd_answer" in
     y|Y|[yY][eE][sS])
       for disk in `echo 1 2 3`; do
          echo
          echo "Please insert the Redhat $RH_Version CD $disk into CD-ROM drive, then press <Enter>."
          read
          mount /mnt/cdrom
          mkdir -p $RH_APTDIR/$DISTRIBUTION/apt/RPMS.$core_dir || exit 1
          cp -v -r /mnt/cdrom/$DISTRIBUTION/RPMS/* $RH_APTDIR/$DISTRIBUTION/apt/RPMS.$core_dir
          eject
          echo
       done
       echo "Please insert the DRBL CD into CD-ROM drive, then press <Enter>."
       read
       mount /mnt/cdrom
       cp -v -r /mnt/cdrom/RH$RH_Version/RPMS.drbl $RH_APTDIR/$DISTRIBUTION/apt/
       eject
       ;;
     *)
       [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
       echo "Without the CDs, we can NOT go on...Program terminated!"
       [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
       exit 1
     esac

     ;;
    *)
     echo "Creating DRBL apt repository from network..."
     cd $RH_APTDIR
     lftp -e "o $rpmpkgs_url && mirror --delete --verbose -x "SRPMS" && quit"

     # put the RPMS.* in $RH_APTDIR/$DISTRIBUTION
     mkdir -p $RH_APTDIR/$DISTRIBUTION/apt || exit 1
     cd $RH_APTDIR/$DISTRIBUTION/apt
     # 2nd, get the files for RPMS.drbl RPMS.drbl-test RPMS.drbl-unstable RPMS.updates
     for pkgs in $DRBL_RPMS_DIR; do 
      lftp -e "o $aptrpmpkgs_url && mirror --delete --verbose $pkgs && quit"
     done
esac

#cp -f genbasedir-0.5 genpkglist-0.5 $APTDIR/scripts
# Install apt, we need genbasedir genpkglist
rpm -q apt &>/dev/null
if [ "$?" -gt 0 ]; then 
   echo "Install APT ..."
   rpm -ivh $RH_APTDIR/$DISTRIBUTION/apt/RPMS.drbl/apt-[0-9]*.i386.rpm
else
   echo "***************************************************************"
   echo "apt is already installed. Amazing!!! You are excellent!"  
   echo "***************************************************************"
fi
echo

###########################################################################
# Create directories
# 1st, clean the stalled files 
[ -d $RH_APTDIR/$DISTRIBUTION/apt/base ] && rm -rf $RH_APTDIR/$DISTRIBUTION/apt/base
mkdir -p $RH_APTDIR/$DISTRIBUTION/apt/base || exit 1
cd $RH_APTDIR/$DISTRIBUTION/apt/
[ -d RPMS.$core_dir ] && rm -rf RPMS.$core_dir
ln -fs ../RPMS RPMS.$core_dir

# Put the symlinks for the base OS packages
cd $RH_APTDIR/$DISTRIBUTION/apt/
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
$DRBL_SCRIPT_PATH/gen_apt_base $RH_APTDIR/$DISTRIBUTION/apt

# put the DRBL package in /drbl
mkdir -p $www_root/DRBL
wget -P $www_root/DRBL/ $rh_url/$drbl_setup_path/$drbl_setup_rpm
cd $www_root/DRBL
rpm2cpio $drbl_setup_rpm | cpio -idm

# TO_MOD_2
# put the .discinfo for RH installer program. 
# Ref:
# https://listman.redhat.com/archives/psyche-list/2002-October/msg00252.html
cd $www_root
case "$RH_Version" in
	"8.0")
cat <<-DISCINFO_END > $www_root/.discinfo
1031690066.345824
Psyche 8.0
i386
1,2,3
RedHat/base
RedHat/RPMS
RedHat/pixmaps
DISCINFO_END
           ;;

	"9")
cat <<-DISCINFO_END > $www_root/.discinfo
1047611055.799229
Red Hat Linux 9
i386
1,2,3
RedHat/base
RedHat/RPMS
RedHat/pixmaps
DISCINFO_END
           ;;

        "FC1")
cat <<-DISCINFO_END > $www_root/.discinfo
1067903218.245772
Fedora Core 1
i386
1,2,3
Fedora/base
Fedora/RPMS
Fedora/pixmaps
DISCINFO_END
           ;;

        "FC2")
cat <<-DISCINFO_END > $www_root/.discinfo
1084440410.630525
Fedora Core 2
i386
1,2,3,4
Fedora/base
Fedora/RPMS
Fedora/pixmaps
DISCINFO_END
           ;;

        "FC3")
cat <<-DISCINFO_END > $www_root/.discinfo
1099518735.522378
Fedora Core 3
i386
1,2,3,4
Fedora/base
Fedora/RPMS
Fedora/pixmaps
DISCINFO_END
           ;;
esac

# put autoron
cat <<-AUTORUN_END > $www_root/autorun
#!/bin/bash
#
# Autorun script for Red Hat Linux
# Copyright (c) 1999-2002 Red Hat, Inc.

dir=\$(echo \$0 |sed 's/autorun//')
cd \$dir
tree=\$(pwd)

# Change directories so we can unmount the CD-ROM Drive
cd /
exec /usr/bin/redhat-cdinstall-helper \$tree
AUTORUN_END
chmod a+x $www_root/autorun

#
cd $www_root

# link the directory to be the usual one.
www_root_link_dir=$(dirname $www_root_link)
mkdir -p $www_root_link_dir
ln -fs $www_root $www_root_link

# create the iso image
if [ "$DVD_ISO" = "yes" ]; then
  mkisofs -o ../DRBL-RH${RH_Version}-i386-dvd.iso \
        -A "DRBL for Red Hat Linux/i386 $RH_Version" -V "DRBL for Red Hat Linux/i386 $RH_Version" \
	-b isolinux/isolinux.bin -c isolinux/boot.cat \
	-no-emul-boot -boot-load-size 4 -boot-info-table \
	-R -m TRANS.TBL \
	-J -T .
  echo "The output DVD ISO image for DRBL for Red Hat Linux/i386 $RH_Version is DRBL-RH${RH_Version}-i386-dvd.iso"
fi
