#!/bin/sh
# Linux-Mandrake ISO CD Creation Script (thanks to Kenny Graunke for this one!)
# Fixes and modifications to handle using a preexisting mirror (without
#      messing up that mirror) added by Guy T. Rice
# Fixes and modifications to handle burning the main CD automatically plus adding
#      most of the commentaries below  --  by Daniel Hammer (dhammer@cybercable.fr)
#
#	20000904 warly <warly@mandrakesoft.com>: support for last rpmtools tools 
#
# Before You Start: 
#   o  Check all variables and insert a CD-R/RW into you CD-writer
#   o  In most settings you must run this script as "root"
#   o  It makes sense to get rid of lots of useless files which you
#      get during the download but which you don't need for the CD's
#      - these are for example lots of ".listing" files and some forgotten
#      junk-files like "locale.dir~", etc.
#      Cleaning them off is easily made by the following (perhaps not most
#      elegant) procedure:
#        * change into the appropriate directory of your mirror
#        * type "find . -name .listing > rmdotlisting&&chmod +x rmdotlisting"
#        * using "sed" or just by eding the rmdotlisting file replace "./"
#          at the beginning of each line by "rm "
#        * then run "rmdotlisting" by typing "./rmdotlisting"
#  
#   o check if you don't have any two versions of any package in the RPMS/RPMS2
#     directories; this can happen if you use "wget -rm" since you get all newer
#     versions of the packages and the outdated ones remain in your mirror 
#     directory 
#     Example: you've already downloaded certainpackage-1.2.3mdk.rpm and when 
#              updating you get the newer certainpackage-1.2.5mdk.rpm now you'll 
#              have both packages in the RPMS directory of your local mirror & when 
#              later creating deplists this produces "depslist.ordered does not match ..."
#  
# Preferably run this script by typing: "sh isomake.sh 2>&1 | tee -a ~/.isomake.log"
#    since if you get any errors you'll have a log-file to check what went wrong


# You MUST change these variables for your particular configuration:

shopt -s extglob

# Set this to the location of where you want to download Linux-Mandrake to.
mdkLOCALROOT=/root/1/mdkroot
# Set these to the location where you want your ISO images to be created.
mdkISO1DIR=/root/1/mdkiso1
mdkISO2DIR=/root/1/mdkiso2
# Set this to 1 to download Linux-Mandrake.
mdkMIRROR=0
# Set these to your FTP mirror and directory, respectively:
#mdkFTPSITE=sunsite.uio.no
#mdkFTPDIR=/pub/unix/Linux/Mandrake-devel/cooker
# Set this to a non-empty string to copy an already existing local mirror
mdkPREEXISTING=/root/1/mdk_7.2beta1/*

# You should not have to change anything below this point.

# If there's a preexisting mirror, copy it to our build location
if [ -n "$mdkPREEXISTING" ]; then
  rm -rf $mdkLOCALROOT/mirror
  mkdir -p $mdkLOCALROOT/mirror
  cp -a $mdkPREEXISTING $mdkLOCALROOT/mirror
fi

# mirror from ftp preserving permissions and symlinks
# Be careful altering the download-method, e.g. using 
# "wget -rm $mdkFTPSITE$mdkFTPDIR 2>&1 | tee -a ~/.wget.log"
#   If you do so and if you had an existing mirror before, you can get
#   a list of all updated packages by "grep RETR ~/.wget.log"
#   You MUST kick off their outdated versions since otherwise it will produce a
#   wrong Installation-CD (see commentaries above). Therefore, to manage the 
#   cleaning this script should be cut in two after having finished the download.
#
if [ "$mdkMIRROR" = "1" ]; then
  mkdir $mdkLOCALROOT/mirror
  cd $mdkLOCALROOT/mirror
  lftp -c "open $mdkFTPSITE\; cd $mdkFTPDIR\; mirror"
fi

# make sure tools needed by this script are available and up to date
# rpm complains that other packages depend on these for no apparent reason
# (we're not removing them) so I've added --nodeps to stop it from complaining
#rpm -Uvh --nodeps $mdkLOCALROOT/mirror/Mandrake/RPMS/popt-* # needed by rpm
#rpm -Uvh --nodeps $mdkLOCALROOT/mirror/Mandrake/RPMS/rpm-* # needed by rpmtools
#rpm -Uvh --nodeps $mdkLOCALROOT/mirror/Mandrake/RPMS/rpmtools-* # we need these

# create new directory to place the rpms for the second CD
mkdir -p $mdkLOCALROOT/cd2/Mandrake/RPMS2

# move all files in the directory for CD2
mv $mdkLOCALROOT/mirror/Mandrake/RPMS/* $mdkLOCALROOT/cd2/Mandrake/RPMS2
mv $mdkLOCALROOT/mirror $mdkLOCALROOT/cd1

# move the apropriate files in the directory for first CD
for i in $(sed <$mdkLOCALROOT/cd1/Mandrake/base/rpmslist -e 's/\-[^-]+\-[^-].*$//'); do
  mv $mdkLOCALROOT/cd2/Mandrake/RPMS2/$i-!(*-*)-!(*-*)mdk*.rpm $mdkLOCALROOT/cd1/Mandrake/RPMS
done        

# put the image in a separate dir to be sure the CD will boot everywhere
mkdir -p $mdkLOCALROOT/images
mv $mdkLOCALROOT/cd1/images $mdkLOCALROOT/images

# change to directory of main CD
cd $mdkLOCALROOT/cd1

# the utilities in misc don't work unless they're in the PATH
export PATH=$mdkLOCALROOT/cd1/misc:$PATH

# generate the dependance files

rm -f $mdkLOCALROOT/cd1/Mandrake/base/{hdlists,hdlist.cz2}
./misc/genhdlist_cz2 -o $mdkLOCALROOT/cd1/Mandrake/base/hdlist1.cz $mdkLOCALROOT/cd1/Mandrake/RPMS
echo "hdlist1.cz     Mandrake/RPMS  CD1 Installation CD" > $mdkLOCALROOT/cd1/Mandrake/base/hdlists

rm -f $mdkLOCALROOT/cd1/Mandrake/base/hdlist2.cz2
./misc/genhdlist_cz2 -o $mdkLOCALROOT/cd1/Mandrake/base/hdlist2.cz $mdkLOCALROOT/cd2/Mandrake/RPMS2
echo "hdlist2.cz     Mandrake/RPMS2  CD2 Extension CD" >> $mdkLOCALROOT/cd1/Mandrake/base/hdlists

rm -f $mdkLOCALROOT/cd1/Mandrake/base/{depslist,deplist.ordered,filelist}
./misc/genbasefiles $mdkLOCALROOT/cd1/Mandrake/base/ $mdkLOCALROOT/cd1/Mandrake/base/hdlist1.cz $mdkLOCALROOT/cd1/Mandrake/base/hdlist2.cz
./misc/usr/bin/genfilelist $mdkLOCALROOT/cd1/Mandrake/RPMS $mdkLOCALROOT/cd2/Mandrake/RPMS2 > $mdkLOCALROOT/cd1/Mandrake/base/filelist

# order the hdlist with the depslist to get faster accesses
./misc/genhdlist_cz2 --noclean --ordered-depslist Mandrake/base/depslist.ordered \
	-o Mandrake/base/hdlist1.cz $mdkLOCALROOT/cd1/Mandrake/RPMS \
	-o $mdkLOCALROOT/cd2/Mandrake/base/hdlist2.cz Mandrake/RPMS2

# create the first iso image (Main Installation CD)
mkisofs -J -r \
  -o $mdkISO1DIR/Cooker-CD1.iso \
  -b images/all.img \
  -c images/boot.cat \
  $mdkLOCALROOT/images \
  $mdkLOCALROOT/cd1

# create the second iso image (Extensions CD)
mkisofs -J -r \
  -o $mdkISO2DIR/Cooker-CD2.iso \
  $mdkLOCALROOT/cd2
