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

# 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/spare
# Set these to the location where you want your ISO images to be created.
mdkISO1DIR=/mnt/spare
mdkISO2DIR=/mnt/spare
# Set this to 1 to download Linux-Mandrake.
mdkMIRROR=0
# Set these to your FTP mirror and directory, respectively:
mdkFTPSITE=ftp.somewhere.org
mdkFTPDIR=/some/dir/hydrogen
# Set this to a non-empty string to copy an already existing local mirror
mdkPREEXISTING=/exports/mirrors/Mandrake/cooker

# 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 -f $mdkLOCALROOT/mirror
	cp -a $mdkPREEXISTING $mdkLOCALROOT/mirror
fi

# mirror from ftp preserving permissions and symlinks
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-3* # needed by rpmtools
rpm -Uvh --nodeps $mdkLOCALROOT/mirror/Mandrake/RPMS/rpmtools-* # we need these
cat >/tmp/gencompss <<"EOF"
#!/usr/bin/perl
open F, "bzip2 -dc @ARGV 2>/dev/null | hdlist2groups - |";
foreach (<F>) {
    chop;
    /(.*):(.*)/;
    push @{$l{$2}}, $1;
}
close F or die;
foreach (sort keys %l) {
    print "$_\n";
    print "\t$_\n" foreach sort @{$l{$_}};
    print "\n";
}
EOF
chmod +x /tmp/gencompss

# 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 `cat $mdkLOCALROOT/cd1/Mandrake/base/rpmslist`
do
    mv $mdkLOCALROOT/cd2/Mandrake/RPMS2/$i-!(*-*)-!(*-*)mdk*.rpm $mdkLOCALROOT/cd1/Mandrake/RPMS
done        

# 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/hdlist.cz2 $mdkLOCALROOT/cd1/Mandrake/RPMS
echo "hdlist.cz2      Mandrake/RPMS   Installation CD" > $mdkLOCALROOT/cd1/Mandrake/base/hdlists

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

rm -f $mdkLOCALROOT/cd1/Mandrake/base/{depslist,deplist.ordered,filelist}
./misc/gendepslist2 -o $mdkLOCALROOT/cd1/Mandrake/base/depslist $mdkLOCALROOT/cd1/Mandrake/base/hdlist.cz2 $mdkLOCALROOT/cd1/Mandrake/base/hdlist2.cz2
/usr/bin/genfilelist $mdkLOCALROOT/cd1/Mandrake/RPMS $mdkLOCALROOT/cd2/Mandrake/RPMS2 > $mdkLOCALROOT/cd1/Mandrake/base/filelist

/tmp/gencompss $mdkLOCALROOT/cd1/Mandrake/base/hdlist*.cz2 > $mdkLOCALROOT/cd1/Mandrake/base/compss

# create the first iso image
mkisofs -J -r \
        -o $mdkISO1DIR/Installation.iso \
        -b images/cdrom.img \
        -c images/boot.cat \
        $mdkLOCALROOT/cd1

# create the second iso image
mkisofs -J -r \
        -o $mdkISO2DIR/Extensions.iso \
        $mdkLOCALROOT/cd2

rm /tmp/gencompss
