#!/bin/sh
# Linux-Mandrake ISO CD Creation Script (thanks to Kenny Graunke for this one!)
# Modified for RE by AEN
# 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=/mnt/in
# Set these to the location where you want your ISO images to be created.
mdkISO1DIR=/mnt/out
mdkISO2DIR=/mnt/out
mdkISO3DIR=/mnt/out
# Set this to 1 to download Linux-Mandrake.
mdkMIRROR=0
# Set these to your RSYNC mirror and directory, respectively:
mdkRSYNCSITE=linuxteam.iplabs.ru
mdkRSYNCDIR=MandrakeRE/REbeta
# Set this to a non-empty string to copy an already existing local mirror
mdkPREEXISTING=/mnt/mirror/linuxteam.iplabs.ru/pub/distributions/MandrakeRE/REbeta/i586

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

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

# Mirror from rsync preserving permissions and symlinks;
# rsync >= 2.4.6-ipl4mdk is required for this command.
#
elif [ "$mdkMIRROR" = "1" ]; then
  mkdir -p "$mdkLOCALROOT/mirror"
  rsync -va --partial --stats --delete-after "$mdkRSYNCSITE::$mdkRSYNCDIR/" "$mdkLOCALROOT/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/rpm-* # needed by rpmtools
#rpm -Uvh --nodeps $mdkLOCALROOT/mirror/Mandrake/RPMS/rpmtools-* # we need these

# create new directories to place the rpms for the second and third CDs
rm -rf $mdkLOCALROOT/cd{1,2,3}
mkdir -p $mdkLOCALROOT/cd2/Mandrake/RPMS2
mkdir -p $mdkLOCALROOT/cd3/Mandrake/RPMS3
mkdir -p $mdkLOCALROOT/cd2/Mandrake/base
mkdir -p $mdkLOCALROOT/cd3/Mandrake/base

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

rm -f $mdkLOCALROOT/cd1/SRPMS.RE

# remove some documentation
rm -rf $mdkLOCALROOT/cd1/doc/install/{af,br,ca,cs,da,el,eo,et,eu,fi,ga,gl,id,it,nl,no,pt,sv,tr,zh_CN,zh_TW}

# move the apropriate files in the directory for third CD
for i in `cat $mdkLOCALROOT/cd1/Mandrake/base/RE_disk3`; do
mv $mdkLOCALROOT/cd2/Mandrake/RPMS2/$i-!(*-*)*.rpm $mdkLOCALROOT/cd3/Mandrake/RPMS3
done

# move the apropriate files in the directory for first CD
for i in `cat $mdkLOCALROOT/cd1/Mandrake/base/RE_disk1`; do
mv $mdkLOCALROOT/cd2/Mandrake/RPMS2/$i-!(*-*)*.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

# remove apt-specific files
rm -f $mdkLOCALROOT/cd1/Mandrake/base/hashfile*
rm -f $mdkLOCALROOT/cd1/Mandrake/base/srclist*
rm -f $mdkLOCALROOT/cd1/Mandrake/base/pkglist*

# generate the dependance files

rm -f $mdkLOCALROOT/cd1/Mandrake/base/{hdlists,hdlist.cz2,hdlist.cz}
./misc/genhdlist_cz2 -o $mdkLOCALROOT/cd1/Mandrake/base/hdlist1.cz $mdkLOCALROOT/cd1/Mandrake/RPMS
echo "hdlist1.cz     Mandrake/RPMS  CD1 Boot 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 Application CD" >> $mdkLOCALROOT/cd1/Mandrake/base/hdlists

rm -f $mdkLOCALROOT/cd1/Mandrake/base/hdlist3.cz2
./misc/genhdlist_cz2 -o $mdkLOCALROOT/cd1/Mandrake/base/hdlist3.cz $mdkLOCALROOT/cd3/Mandrake/RPMS3
echo "hdlist3.cz     Mandrake/RPMS3  CD3 Documentation CD" >> $mdkLOCALROOT/cd1/Mandrake/base/hdlists

rm -f $mdkLOCALROOT/cd1/Mandrake/base/{depslist,deplist.ordered,filelist,compss,provides}
./misc/genbasefiles $mdkLOCALROOT/cd1/Mandrake/base/ $mdkLOCALROOT/cd1/Mandrake/base/hdlist{1,2,3}.cz
/usr/bin/genfilelist $mdkLOCALROOT/cd1/Mandrake/RPMS $mdkLOCALROOT/cd2/Mandrake/RPMS2  $mdkLOCALROOT/cd3/Mandrake/RPMS3 > $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
./misc/genhdlist_cz2 --noclean --ordered-depslist Mandrake/base/depslist.ordered \
	-o $mdkLOCALROOT/cd2/Mandrake/base/hdlist2.cz $mdkLOCALROOT/cd2/Mandrake/RPMS2
./misc/genhdlist_cz2 --noclean --ordered-depslist Mandrake/base/depslist.ordered \
	-o $mdkLOCALROOT/cd3/Mandrake/base/hdlist3.cz $mdkLOCALROOT/cd3/Mandrake/RPMS3

cp -f $mdkLOCALROOT/cd3/Mandrake/base/hdlist3.cz $mdkLOCALROOT/cd1/Mandrake/base
cp -f $mdkLOCALROOT/cd2/Mandrake/base/hdlist2.cz $mdkLOCALROOT/cd1/Mandrake/base

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

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

# create the third iso image (Documentation CD)
mkisofs -J -r \
  -o $mdkISO2DIR/REbeta-CD3.iso \
  $mdkLOCALROOT/cd3

# burn the main CD
# the values for cd-writer speed and the scsi-bus target/lun must be set
# according to your local settings
#cd $mdkISO1DIR
#cdrecord -eject -v -data speed=4 dev=4,0 REbeta-CD1.iso

# the first CD comes out the CD-writer and the script comes out at the
# place where your second image file is - ready to perform
# 'cdrecor
#cd $mdkISO2DIR
#cdrecord -eject -v -data speed=4 dev=4,0 REbeta-CD2.iso

#cd $mdkISO2DIR
#cdrecord -eject -v -data speed=4 dev=4,0 REbeta-CD2.iso
