. ../locations.include
if [ -f ./locations.include.site ] ; then
	. ./locations.include.site
fi
MYPWD=`pwd`
export MYPWD

#clean up old ones
for i in 1 2 3 4 5
do
   if [ -d $RELEASEDIR/$ARCH-disc0-disc$i ] ; then
	rm -r $RELEASEDIR/$ARCH-disc0-disc$i
   fi
done
if [ -d $RELEASEDIR/$ARCH-disc0 ] ; then
	rm -r $RELEASEDIR/$ARCH-disc0
fi
# Make disc0
#link $SITE and $DEFAULT together to not have to change anaconda anymore
mkdir -vp $RELEASEDIR/$ARCH-disc0/$DEFAULT
mkdir -vp $RELEASEDIR/$ARCH-disc0/sites
cd $RELEASEDIR/$ARCH-disc0/
ln -s $DEFAULT $SITE
cd sites
ln -s ../$DEFAULT $SITE
cd $MYPWD
#copy in $SITE/base
mkdir -v $RELEASEDIR/$ARCH-disc0/$SITE/base
ln $RELEASEDIR/$ARCH/sites/$SITE/base/* $RELEASEDIR/$ARCH-disc0/$SITE/base
#remove things that we are going to modify/rebuild later so as to get our
#own copy, otherwise we change the original too
rm $RELEASEDIR/$ARCH-disc0/$SITE/base/hdlist
rm $RELEASEDIR/$ARCH-disc0/$SITE/base/hdlist2
rm $RELEASEDIR/$ARCH-disc0/$SITE/base/pkgorder
#copy in $SITE/documentation
mkdir $RELEASEDIR/$ARCH-disc0/documentation
ln $RELEASEDIR/$ARCH/sites/$SITE/documentation/* $RELEASEDIR/$ARCH-disc0/documentation
if [ -f $RELEASEDIR/$ARCH/sites/$SITE/documentation/$SITE.releasenote ] ; then
  cp $RELEASEDIR/$ARCH/sites/$SITE/documentation/$SITE.releasenote $RELEASEDIR/$ARCH-disc0/
fi
cp $RELEASEDIR/$ARCH/$DEFAULT.*releasenote $RELEASEDIR/$ARCH-disc0/
cp $RELEASEDIR/$ARCH/README $RELEASEDIR/$ARCH-disc0/
#copy in $SITE/isolinux and special *.iso config files
mkdir $RELEASEDIR/$ARCH-disc0/isolinux
cp -f $RELEASEDIR/$ARCH/sites/$SITE/isolinux/* $RELEASEDIR/$ARCH-disc0/isolinux/
if [ -f $RELEASEDIR/$ARCH/sites/$SITE/build/isolinux/isolinux.cfg.iso ] ; then
 cp -f $RELEASEDIR/$ARCH/sites/$SITE/build/isolinux/isolinux.cfg.iso $RELEASEDIR/$ARCH-disc0/isolinux/isolinux.cfg
fi
if [ -f $RELEASEDIR/$ARCH/sites/$SITE/build/isolinux/boot.msg.iso ] ; then
 cp -f $RELEASEDIR/$ARCH/sites/$SITE/build/isolinux/boot.msg.iso $RELEASEDIR/$ARCH-disc0/isolinux/boot.msg
fi
#make updates.img from original area and copy in
$MYPWD/../make.updates.img.sh 
#Copy over misc things
cp $RELEASEDIR/$ARCH/.discinfo $RELEASEDIR/$ARCH-disc0/
mkdir -p $RELEASEDIR/$ARCH-disc0/$SITE/build
cp -a $RELEASEDIR/$ARCH/sites/$SITE/build $RELEASEDIR/$ARCH-disc0/$SITE/
cp -a $RELEASEDIR/$ARCH/RPM-GPG-KEYs $RELEASEDIR/$ARCH-disc0/
cp -a $RELEASEDIR/$ARCH/RELEASE-NOTES*.html $RELEASEDIR/$ARCH-disc0/
#Check size of $SITE/Updates .  Current limitation is that is must fit on
#one cdr along with the other things that we need.  So this means it has to
#less than 
AVAIL=550
SIZE=`du -s -BM $RELEASEDIR/$ARCH-disc0 | cut -f1`
echo $SIZE
if [ $SIZE -gt $AVAIL ] ; then
   echo "Your $RELEASEDIR/sites/$SITE/Updates area is $SIZE MB big which is larger than a cdr along with the other files that we need, you need to use less than $AVAIL MB"
   exit 1
fi
#copy in $SITE/Updates which are RPMS for $SITE
mkdir $RELEASEDIR/$ARCH-disc0/$SITE/Updates
ln $RELEASEDIR/$ARCH/sites/$SITE/Updates/*.rpm $RELEASEDIR/$ARCH-disc0/$SITE/Updates
#copy in $DEFAULT/RPMS which are the default rpms
mkdir -p $RELEASEDIR/$ARCH-disc0/$SITE/RPMS
ln $RELEASEDIR/$ARCH/$DEFAULT/RPMS/*.rpm $RELEASEDIR/$ARCH-disc0/$SITE/RPMS/
#
#Need to move the big SRPMS/vendor directory out of the way as splittree.py
#cannot handle this, we put it back at the end, this also means that these
#will not be on the cd's
mv $RELEASEDIR/SRPMS/vendor .. 
#
echo "Using $RELEASEDIR/$ARCH/sites/$SITE/base/$FILEORDER as the fileorder file"
#echo "and not making a new one"
#cp -v $RELEASEDIR/$ARCH/sites/$SITE/base/$FILEORDER $RELEASEDIR/$ARCH-disc0/$SITE/base/
echo "and making a new one"
$ANACONDADIR/pkgorder $RELEASEDIR/$ARCH $ARCH $DEFAULT sites/$SITE > $RELEASEDIR/$ARCH-disc0/$SITE/base/$FILEORDER
#Run splittree.py to split the $DEFAULT/RPMS onto multiple cd's
echo "Running splittree to decide on how to span the cd images"
#Depending on how many RPMS you have in /Updates you might have to increase 
# LASTSRCDISK and LASTBINDISK , look at output and if you see messages about
# rpms not fitting then you need to increase these.
LASTSRCDISC=4
LASTBINDISC=3
TOTCD=$LASTSRCDISC
TOTSRCDISC=`expr $TOTCD - $LASTBINDISC `
$ANACONDADIR/splittree.py --arch=$ARCH --total-discs=$TOTCD --bin-discs=$LASTBINDISC --src-discs=$TOTSRCDISC --release-string=$SITE --default=$DEFAULT --pkgorderfile=$RELEASEDIR/$ARCH-disc0/$SITE/base/$FILEORDER --distdir=$RELEASEDIR/$ARCH-disc0 --srcdir=$RELEASEDIR/SRPMS/
mv ../vendor $RELEASEDIR/SRPMS/
#Need to remake these links on $ARCH-disc0-disc1 as splittree did not preserve 
echo "making special directories on $RELEASEDIR/$ARCH-disc0-disc1"
mkdir -vp $RELEASEDIR/$ARCH-disc0-disc1/sites
cd $RELEASEDIR/$ARCH-disc0-disc1/
ln -s $DEFAULT $SITE
cd sites
ln -s ../$DEFAULT $SITE
#Run genhdlist to create new hdlists with cd numbers 
echo "Running genhdlist against $RELEASEDIR/$ARCH-disc0-disc[1-$LASTBINDISC]"
TMPBINAREA=`echo $RELEASEDIR/$ARCH-disc0-disc[1-$LASTBINDISC]`
cd $RELEASEDIR
$ANACONDADIR/genhdlist --withnumbers --fileorder `pwd`/$ARCH-disc0/$SITE/base/$FILEORDER --default $DEFAULT $TMPBINAREA 
cd /
#Have to move contrib and notsupported areas to cds
CDSIZE=`expr 650 \* 1024`  # 650MB in k
EXTRA=`expr 5 \* 1024` 
NOTSUPPORTED=`du -ks $TREE/notsupported/RPMS | cut -f1`
CONTRIBRPMS=`du -ks $TREE/contrib/RPMS | cut -f1`
IMAGES=`du -ks $TREE/sites/$SITE/images | cut -f1`
SPACELEFTONBIN=`du -ks $RELEASEDIR/$ARCH-disc0-disc$LASTBINDISC | cut -f1`
SPACELEFTONLASTBIN=`expr $CDSIZE - $SPACELEFTONBIN`
SPACELEFTONSRPM=`du -ks $RELEASEDIR/$ARCH-disc0-disc$LASTSRCDISC | cut -f1`
SPACELEFTONLASTSRPM=`expr $CDSIZE - $SPACELEFTONSRPM`
notsupported="nope"
contrib="nope"
#copy in images/ to last bin cd if it fits
if [ $SPACELEFTONLASTBIN -gt $IMAGES ] ; then
  #copy in images 
  mkdir -p $RELEASEDIR/$ARCH-disc0-disc$LASTBINDISC/images
  cp -a $TREE/sites/$SITE/images/* $RELEASEDIR/$ARCH-disc0-disc$LASTBINDISC/images/
  SPACELEFTONBIN=`du -ks $RELEASEDIR/$ARCH-disc0-disc$LASTBINDISC | cut -f1`
  SPACELEFTONLASTBIN=`expr $CDSIZE - $SPACELEFTONBIN`
else
  #copy in images 
  mkdir -p $RELEASEDIR/$ARCH-disc0-disc$LASTSRCDISC/images
  cp -a $TREE/sites/$SITE/images/* $RELEASEDIR/$ARCH-disc0-disc$LASTSRCDISC/images/
  SPACELEFTONSRPM=`du -ks $RELEASEDIR/$ARCH-disc0-disc$LASTSRCDISC | cut -f1`
  SPACELEFTONLASTSRPM=`expr $CDSIZE - $SPACELEFTONSRPM`
fi
if [ $SPACELEFTONLASTBIN -gt $NOTSUPPORTED ] ; then
  #copy in notsupported/RPMS
  mkdir -p $RELEASEDIR/$ARCH-disc0-disc$LASTBINDISC/notsupported/RPMS
  ln $TREE/notsupported/RPMS/*.rpm $RELEASEDIR/$ARCH-disc0-disc$LASTBINDISC/notsupported/RPMS/
  SPACELEFTONLASTBIN=`expr $SPACELEFTONLASTBIN - $NOTSUPPORTED`
  notsupported="done"
  echo "Put notsupported rpms on cd $LASTBINDISC"
fi
# maybe contrib will fit
if [ $SPACELEFTONLASTBIN -gt $CONTRIBRPMS ] ; then
    #copy in contrib/RPMS
    mkdir -p $RELEASEDIR/$ARCH-disc0-disc$LASTBINDISC/contrib/RPMS
    ln $TREE/contrib/RPMS/*.rpm $RELEASEDIR/$ARCH-disc0-disc$LASTBINDISC/contrib/RPMS/
    contrib="done"
    echo "Put contrib rpms on cd $LASTBINDISC"
fi
SPACELEFTONSRPM=`du -ks $RELEASEDIR/$ARCH-disc0-disc$LASTSRCDISC | cut -f1`
SPACELEFTONLASTSRPM=`expr $CDSIZE - $SPACELEFTONSRPM`
if [ ! $notsupported = "done" ] ; then
    # notsupported didnt fit so lets try the last srpm disc
    if [ $SPACELEFTONLASTSRPM -gt $NOTSUPPORTED ] ; then
      #copy in notsupported/RPMS
      mkdir -p $RELEASEDIR/$ARCH-disc0-disc$LASTSRCDISC/notsupported/RPMS
      ln $TREE/notsupported/RPMS/*.rpm $RELEASEDIR/$ARCH-disc0-disc$LASTSRCDISC/notsupported/RPMS/
      SPACELEFTONLASTSRPM=`expr $SPACELEFTONLASTSRPM - $NOTSUPPORTED`
      notsupported="done"
      echo "Put notsupported rpms on cd $LASTSRCDISC"
    fi
fi
if [ ! $contrib = "done" ] ; then
    # contrib didnt fit so lets try the last srpm disc
    if [ $SPACELEFTONLASTSRPM -gt $CONTRIBRPMS ] ; then
      #copy in contrib/RPMS
      mkdir -p $RELEASEDIR/$ARCH-disc0-disc$LASTSRCDISC/contrib/RPMS
      ln $TREE/contrib/RPMS/*.rpm $RELEASEDIR/$ARCH-disc0-disc$LASTSRCDISC/contrib/RPMS/
      contrib="done"
      echo "Put contrib rpms on cd $LASTSRCDISC"
    fi
fi
if [ ! $notsupported = "done" ] ; then
	# notsupported did not fit on last SRPM disk either
        # For now just make a new disk and put notsupported on it
# check to see if notsupported is > 1 disk - need to handle it (later)
        if [ $NOTSUPPORTED -gt $CDSIZE ] ; then
	  echo "Notsupported is > 1 CD - the last iso will be too big, skipping"
        else
          TOTCD=`expr $LASTSRCDISC + 1 `
          mkdir -p $RELEASEDIR/$ARCH-disc0-disc$TOTCD/notsupported/RPMS
          ln $TREE/notsupported/RPMS/* $RELEASEDIR/$ARCH-disc0-disc$TOTCD/notsupported/RPMS/
	  # recalculate space since put things on it
          SPACELEFTONEXTRATMP=`du -ks $RELEASEDIR/$ARCH-disc0-disc$TOTCD`
          SPACELEFTONEXTRA=`expr $CDSIZE - $SPACELEFTONEXTRATMP`
          notsupported="last"
          echo "Put notsupported rpms on cd $TOTCD"
	fi
fi
if [ ! $contrib = "done" ] ; then
	# contrib did not fit on last SRPM disk either
        # For now just make a new disk and put contrib on it
# check to see if contrib is > 1 disk - needs to handle it (later)
        if [ $CONTRIBRPMS -gt $CDSIZE ] ; then
	  echo "Contribs is > 1 CD - the last iso will be too big, skipping"
        else	
          if [ $notsupported  = "last" ] ; then
	     # notwupported was put on extra last cd so check for space
             if [ $CONTRIBRPMS > $SPACELEFTONEXTRA ] ; then
                # contrib does not fit on this last cd so make a new one
	        TOTCD=`expr $TOTCD + 1`
                contrib="contribonlylast"
             fi
	  else
             TOTCD=`expr $LASTSRCDISC + 1 `
             contrib="contribonlylast"
          fi
          #copy in contrib/RPMS to either last with notsupported or new one
          mkdir -p $RELEASEDIR/$ARCH-disc0-disc$TOTCD/contrib/RPMS
          ln $TREE/contrib/RPMS/*.rpm  $RELEASEDIR/$ARCH-disc0-disc$TOTCD/contrib/RPMS/
          echo "Put contrib rpms on cd $TOTCD"
	fi
fi
  
#Now make the iso images
#CHECK that $CDISOAREA exists and make if it does not
if [ -d $CDISOAREA ] ; then
  echo "Creating CD iso images in $CDISOAREA"
else
  mkdir -p $CDISOAREA 
fi
CDNAME=$CDISOAREA/$DEFAULT.$SITE.$VERSION.$DATE.$ARCH
# Make disc1
CDNUM=1
cd $MYPWD/subscripts
$MYPWD/subscripts/makeapt.site.iso.sh $CDNUM
sh $MYPWD/subscripts/mkisoimage.disc$CDNUM.site.sh --subdir $ARCH-disc0-disc$CDNUM --dest $CDNAME.disc$CDNUM.iso
echo "implanting md5sum in disc$CDNUM now"
$ANACONDADIR/implantisomd5 $CDNAME.disc$CDNUM.iso
# Make discx
CDNUM=2
while [ $CDNUM -le $TOTCD ]
do
  cd $MYPWD/subscripts
  $MYPWD/subscripts/makeapt.site.iso.sh $CDNUM 
  $MYPWD/subscripts/mkisoimage.discx.site.sh --subdir $ARCH-disc0-disc$CDNUM --dest $CDNAME.disc$CDNUM.iso
  echo "implanting md5sum in disc$DISC now"
  $ANACONDADIR/implantisomd5 $CDNAME.disc$CDNUM.iso 
  CDNUM=`expr $CDNUM + 1`
done

cd $CDISOAREA
echo "Making md5sums now, this will take a while"
md5sum $CDNAME.disc?.iso > $CDNAME.md5sum
