. ../locations.include
MYPWD=`pwd`
export MYPWD
MYPKGORDERBIN=$MYPWD/pkgorder.site
if [  ! -x $MYPKGORDERBIN ] ; then
   echo "$MYPKGORDERBIN is not found,  it is required to run this program"
   exit 1
fi
if [ $ARCH != `uname -i` ] ; then
  echo "You seem to be trying to build arch $ARCH , on a `uname -i` system"
  echo "exiting"
  exit 1
fi
MEDIA=$1
if [ -z $MEDIA ] ; then
        echo "You must pass either \"CD\" or \"DVD\" "
        exit 1
fi
if [ $MEDIA = "dvd" -o $MEDIA = "DVD" ] ; then
   echo "You selected to create DVD iso images"
   TOTBINISO=$TOTBINDVD
   TOTISO=$TOTDVD
   TOTSRCISO=$TOTSRCDVD
   CONTRIB_ISO=$TOTISO
   CONTRIB_ISO_SRPM=$TOTISO
   BUILD_ISO=$TOTISO
   FASTBUGS_ISO=$TOTISO
   SECURITY_ISO=$TOTISO
   DISCSIZE=$DISCSIZEDVD
   MEDIA="DVD"
else
  if [ $MEDIA = "cd" -o $MEDIA = "CD" ] ; then
   	echo "You selected to create CD iso images"
   	TOTBINISO=$TOTBINCD
   	TOTISO=$TOTCD
   	TOTSRCISO=$TOTSRCCD
        CONTRIB_ISO=$CONTRIB_CD
        FASTBUGS_ISO=$FASTBUGS_CD
        SECURITY_ISO=$SECURITY_CD
   	DISCSIZE=$DISCSIZECD
   	MEDIA="CD"
  else
        echo "You must pass either \"CD\" or \"DVD\" "
        exit 1
  fi
fi
#clean up old ones
echo "Cleaning up old directories"
for i in $(seq $TOTCD) 
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
mkdir -p $RELEASEDIR/$ARCH-disc0
# Make disc0
#copy in isolinux and special *.iso config files
#sh $MYPWD/subscripts/fix.isolinux.sh
mkdir -p $RELEASEDIR/$ARCH-disc0/isolinux
cp -fa $RELEASEDIR/$ARCH/$SITEDIR/isolinux/* $RELEASEDIR/$ARCH-disc0/isolinux/
if [ -f $RELEASEDIR/$ARCH/$SITEDIR/build/isolinux/isolinux.cfg.iso ] ; then
 cp -f $RELEASEDIR/$ARCH/$SITEDIR/build/isolinux/isolinux.cfg.iso $RELEASEDIR/$ARCH-disc0/isolinux/isolinux.cfg
fi
if [ -f $RELEASEDIR/$ARCH/$SITEDIR/build/isolinux/boot.msg.iso ] ; then
 cp -f $RELEASEDIR/$ARCH/$SITEDIR/build/isolinux/boot.msg.iso $RELEASEDIR/$ARCH-disc0/isolinux/boot.msg
fi
if [  ! -d $RELEASEDIR/$ARCH-disc0/$SITEDIR ] ; then
  mkdir -p  $RELEASEDIR/$ARCH-disc0/$SITEDIR
fi
if [ -d $RELEASEDIR/$ARCH/$SITEDIR/RHupdates ] ; then
  if [ ! -d $RELEASEDIR/$ARCH-disc0/$SITEDIR ] ; then
      mkdir -p $RELEASEDIR/$ARCH-disc0/$SITEDIR 
  fi
  cp -a $RELEASEDIR/$ARCH/$SITEDIR/RHupdates $RELEASEDIR/$ARCH-disc0/$SITEDIR/
fi
#make updates.img from original area and copy in
cd $MYPWD/..
./make.updates.img.sh 
#Copy over misc things
cp $RELEASEDIR/$ARCH/.discinfo $RELEASEDIR/$ARCH-disc0/
#copy in $SITEDIR/RPMS which is really just RPMS 
echo "Copying over the rpms"
mkdir -p $RELEASEDIR/$ARCH-disc0/$SITEDIR/RPMS
cd $RELEASEDIR/$ARCH/$SITEDIR/RPMS
ln *.rpm $RELEASEDIR/$ARCH-disc0/$SITEDIR/RPMS
##SITERPMS
cd $RELEASEDIR/$ARCH/$SITEDIR/SITERPMS
echo "Adding in SITERPMS rpms now"
ln -f *.rpm $RELEASEDIR/$ARCH-disc0/$SITEDIR/RPMS
cd $RELEASEDIR/$ARCH-disc0/$SITEDIR/RPMS
RPMTMP=/tmp/rpmtemp
if [ ! -d $RPMTMP ] ; then
  mkdir $RPMTMP 
else
  rm  $RPMTMP/*rpm
  rmdir $RPMTMP
fi
repomanage -c -o . | xargs -n1 -I = mv -v = $RPMTMP 
##worktgroups
cd $RELEASEDIR/$ARCH/$SITEDIR/workgroups
echo "Adding in workgroup rpms now"
find . -name "*-tag*.rpm" -print | xargs cp -av --target-directory=$RELEASEDIR/$ARCH-disc0/$SITEDIR/RPMS/
find . -name "*-tree*.rpm" -print | xargs cp -av --target-directory=$RELEASEDIR/$ARCH-disc0/$SITEDIR/RPMS/
if [ -d $RELEASEDIR ] ; then
   cd $RELEASEDIR
   cd ..
else
   echo "I am lost"
   exit 1
fi
mkdir $RELEASEDIR/$ARCH-disc0/repodata/
mkdir -p $RELEASEDIR/$ARCH-disc0/build/base/
cp -av $RELEASEDIR/$ARCH/$SITEDIR/build/base/* $RELEASEDIR/$ARCH-disc0/build/base/
cp -av $RELEASEDIR/$ARCH/$SITEDIR/SITERPMS/repodata/comps-site.xml $RELEASEDIR/$ARCH-disc0/repodata/
#
echo "Doing a createrepo of $RELEASEDIR/$ARCH-disc0 in quiet mode"
cd $RELEASEDIR/$ARCH-disc0
createrepo  -q -g $RELEASEDIR/$ARCH-disc0/repodata/comps-site.xml .
cd -
echo "Using $RELEASEDIR/$ARCH-disc0/build/base/pkgorder as the fileorder file"
#echo "and not making a new one"
echo "and making a new one"
$MYPWD/pkgorder.site  $RELEASEDIR/$ARCH-disc0/ $ARCH $SITEDIR > /tmp/pkgorder

#openafs-kernel-source is not getting on the cd so adding it manually
grep openafs-kernel-source /tmp/pkgorder
if [ $? -eq 1 ] ; then 
	cd $RELEASEDIR/$ARCH-disc0/$SITEDIR/RPMS 
	ls openafs-kernel-source* >> /tmp/pkgorder
	cd -
fi
cp -v /tmp/pkgorder $RELEASEDIR/$ARCH-disc0/build/base/pkgorder
#Run splittree.py to split the $SITEDIR/RPMS onto multiple cd's

echo "Total Binary dvd is $TOTBINISO, Total SRC dvd is $TOTSRCISO"
echo "Running splittree to decide on how to span the DVD/CD iso images"
echo "$MYPWD/splittree.py --arch=$ARCH --total-discs=$TOTISO " \
 "--bin-discs=$TOTBINISO --src-discs=$TOTSRCISO --release-string=$SITE " \
 "--pkgorderfile=/tmp/pkgorder " \
 "--disc-size=$DISCSIZE " \
 "--srcdir=$SLRELEASEDIR/SRPMS/SL/ --distdir=$RELEASEDIR/$ARCH-disc0 " \
 "--productpath=$SITEDIR --reserve-size=$ISO1RESERVESIZE"
$MYPWD/splittree.py --arch=$ARCH --total-discs=$TOTISO \
  --bin-discs=$TOTBINISO --src-discs=$TOTSRCISO --release-string=$SITEDIR \
  --pkgorderfile=/tmp/pkgorder \
  --disc-size=$DISCSIZE \
  --srcdir=$SLRELEASEDIR/SRPMS/SL/ --distdir=$RELEASEDIR/$ARCH-disc0 \
  --productpath=$SITEDIR --reserve-size=$ISO1RESERVESIZE


cd $RELEASEDIR
mkdir -p $RELEASEDIR/$ARCH-disc0-disc1/$SITEDIR/SITERPMS/repodata
cp -av $RELEASEDIR/$ARCH/$SITEDIR/SITERPMS/repodata/comps-site.xml $RELEASEDIR/$ARCH-disc0-disc1/$SITEDIR/SITERPMS/repodata
#The following is because the x86_64 version needs a rpm in the repo, or it dies
ln  $RELEASEDIR/$ARCH/$SITEDIR/SITERPMS/Fermi-release-5* $RELEASEDIR/$ARCH-disc0-disc1/$SITEDIR/SITERPMS/
cd $RELEASEDIR/$ARCH-disc0-disc1/$SITEDIR/SITERPMS/
echo "running createrepo with the quiet option"
createrepo -q -g repodata/comps-site.xml .
cd $MYPWD 

#TMPBINAREA=`echo $ARCH-disc0-disc[1-$TOTBINISO]/$SITEDIR/RPMS $ARCH-disc0-disc1/$SITEDIR/SITERPMS $ARCH-disc0-disc1/$SITEDIR/updates/security`
TMPBINAREA=`echo $ARCH-disc0-disc[1-$TOTBINISO]/$SITEDIR/RPMS $ARCH-disc0-disc1/$SITEDIR/SITERPMS `
echo "Running createrepo against $TMPBINAREA in quiet mode"
MDATE=`date "+%s"`
echo "/usr/bin/createrepo " \
     "--groupfile $RELEASEDIR/$ARCH/$SITEDIR/RPMS/repodata/comps-sl.xml " \
     "--baseurl media://$MDATE.836690 " \
     "--outputdir $RELEASEDIR/$ARCH-disc0-disc1/$SITEDIR/ " \
     "--basedir $RELEASEDIR " \
     "--split $TMPBINAREA"
cd $RELEASEDIR
/usr/bin/createrepo -q \
   --groupfile $RELEASEDIR/$ARCH/$SITEDIR/RPMS/repodata/comps-sl.xml \
   --baseurl media://$MDATE.836690 \
   --outputdir $RELEASEDIR/$ARCH-disc0-disc1/$SITEDIR/ \
   --basedir $RELEASEDIR \
   --split $TMPBINAREA

if [ -d $RELEASEDIR/$ARCH-disc0-disc1/$SITEDIR/repodata ] ; then
 mv $RELEASEDIR/$ARCH-disc0-disc1/$SITEDIR/repodata $RELEASEDIR/$ARCH-disc0-disc1/$SITEDIR/RPMS 
fi
echo "Copying things to disc1"
mkdir -p $RELEASEDIR/$ARCH-disc0-disc1/images
echo "CCC"
if [ -d $TREE/$SITEDIR/images ] ; then
  echo "CCD"
  cd $TREE/$SITEDIR/images
  find . -print | cpio -pvdum $RELEASEDIR/$ARCH-disc0-disc1/images
  cd $RELEASEDIR/$ARCH-disc0-disc1/$SITEDIR/
  ln -s ../../images images
fi
echo $TREE

mkdir $RELEASEDIR/$ARCH-disc0-disc1/RPM-GPG-KEYs 
cp -a $RELEASEDIR/$ARCH/RPM-GPG-KEYs $RELEASEDIR/$ARCH-disc0-disc1/RPM-GPG-KEYs
#copy in the documentation
if [ -d $RELEASEDIR/$ARCH/$DEFAULT.documentation ] ; then
  mkdir $RELEASEDIR/$ARCH-disc0-disc1/$DEFAULT.documentation
  cp -ra $RELEASEDIR/$ARCH/$DEFAULT.documentation/ $RELEASEDIR/$ARCH-disc0-disc1/
fi
cp -p $RELEASEDIR/$ARCH/$SITEDIR/$SITE.releasenote* $RELEASEDIR/$ARCH-disc0-disc1/
cp -p $RELEASEDIR/$ARCH/README $RELEASEDIR/$ARCH-disc0-disc1/
ln -f $RELEASEDIR/$ARCH/GPL $RELEASEDIR/$ARCH-disc0-disc1/
ln -f $RELEASEDIR/$ARCH/EULA $RELEASEDIR/$ARCH-disc0-disc1/
cp -p $RELEASEDIR/$ARCH/RELEASE-NOTES*.html $RELEASEDIR/$ARCH-disc0-disc1/
#workgroups need the comps files and repomd.xml
mkdir -p $RELEASEDIR/$ARCH-disc0-disc1/$SITEDIR/workgroups/
cd $RELEASEDIR/$ARCH/$SITEDIR/workgroups
find . -print | cpio -pvdum $RELEASEDIR/$ARCH-disc0-disc1/$SITEDIR/workgroups/
cd $RELEASEDIR/$ARCH-disc0-disc1/$SITEDIR/workgroups/
for i in *
do
  if [ -d CVS ] ; then
     rm -r CVS
  fi
  cd $i
#  $RELEASEDIR/$ARCH-disc0-disc1/$SITEDIR/workgroups/FermiGenericDesktop
  #We already copied the workgroup tag and tree rpms to RPMS
  rm RPMS/$i*tag*rpm 
  rm RPMS/$i*tree*rpm 
  if [ -d CVS ] ; then
     rm -r CVS
  fi
  if [ -d docs ] ; then
     rm -r docs
  fi
  if [ -d configfiles ] ; then
    rm -r configfiles
  fi
  if [ -d scripts ] ; then
    rm -r scripts
  fi
  createrepo -g repodata/comps-workgroup.xml .
  cd -
done
cd $MYPWD 
if [ -d $RELEASEDIR/$ARCH/$SITEDIR/contrib/SRPMS ] ; then
   mkdir -p $RELEASEDIR/$ARCH-disc0-disc$CONTRIB_ISO_SRPM/SRPMS/contrib
   cd $RELEASEDIR/$ARCH/$SITEDIR/contrib/SRPMS/
   for i in *
   do
	if [ -d $i ] ; then
           if [ $i != "ruby193" ] ; then
             ln $RELEASEDIR/$ARCH/$SITEDIR/contrib/SRPMS/$i/*.src.rpm $RELEASEDIR/$ARCH-disc0-disc$CONTRIB_ISO_SRPM/SRPMS/contrib/
           fi
	fi
   done
   if [ $ARCH = "x86_64" -a $MEDIA = "CD" ] ; then
       SRPMISO=$TOTISO 
   fi
fi
if [ -d $TREE/$SITEDIR/updates/security/ ] ; then
  mkdir -p $RELEASEDIR/$ARCH-disc0-disc1/$SITEDIR/updates/security/
#This is in here because anaconda has a fit with a empty repo
  ln $TREE/$SITEDIR/SITERPMS/Fermi-release-5*rpm $RELEASEDIR/$ARCH-disc0-disc1/$SITEDIR/updates/security/
  cd $RELEASEDIR/$ARCH-disc0-disc1/$SITEDIR/updates/security
  createrepo .
fi
#Now copy SRPMS/sites/$SITE 
if [ -d $RELEASEDIR/SRPMS/sites/$SITE ] ; then
  ln $RELEASEDIR/SRPMS/sites/$SITE/*.src.rpm $RELEASEDIR/$ARCH-disc0-disc$TOTISO/SRPMS/
  if [ -d$RELEASEDIR/$ARCH-disc0-disc$TOTISO/SRPMS/ ] ; then
    repomanage -c -o $RELEASEDIR/$ARCH-disc0-disc$TOTISO/SRPMS/ | xargs -n1 -I = mv -v = $RPMTMP 
  fi
fi
#Not enough space so putting on one lower
#if [ $ARCH = "x86_64" -a $MEDIA = "CD" ] ; then
#  TOTISO=`expr $TOTISO - 1`
#fi
#now copy security
UPDATEDIR=security
if [ -d $TREE/$SITEDIR/updates/$UPDATEDIR/ ] ; then
  mkdir -p $RELEASEDIR/$ARCH-disc0-disc$SECURITY_ISO/$UPDATEDIR/
  cd $TREE/$SITEDIR/updates/$UPDATEDIR/
  ln $TREE/$SITEDIR/updates/$UPDATEDIR/*.rpm $RELEASEDIR/$ARCH-disc0-disc$SECURITY_ISO/$UPDATEDIR
  cd $RELEASEDIR/$ARCH-disc0-disc$SECURITY_ISO/$UPDATEDIR/
  rm *debuginfo*.rpm
  cd -
  createrepo $RELEASEDIR/$ARCH-disc0-disc$SECURITY_ISO/$UPDATEDIR
fi

#now copy fastbugs
UPDATEDIR=fastbugs
if [ -d $TREE/$SITEDIR/updates/$UPDATEDIR/ ] ; then
  mkdir -p $RELEASEDIR/$ARCH-disc0-disc$FASTBUGS_ISO/$UPDATEDIR/
  cd $TREE/$SITEDIR/updates/$UPDATEDIR/
  ln $TREE/$SITEDIR/updates/$UPDATEDIR/*.rpm $RELEASEDIR/$ARCH-disc0-disc$FASTBUGS_ISO/$UPDATEDIR/
  cd $RELEASEDIR/$ARCH-disc0-disc$FASTBUGS_ISO/$UPDATEDIR/
  rm *debuginfo*.rpm
  cd -
  createrepo $RELEASEDIR/$ARCH-disc0-disc$FASTBUGS_ISO/$UPDATEDIR
fi
#Now copy the build scripts
if [ -d $TREE/$SITEDIR/build ] ; then
  mkdir -p $RELEASEDIR/$ARCH-disc0-disc$BUILD_ISO/build/
  cp -a $TREE/$SITEDIR/build/*  $RELEASEDIR/$ARCH-disc0-disc$BUILD_ISO/build/
  if [ -d $RELEASEDIR/$ARCH-disc0/build/base ] ; then
     mv -f $RELEASEDIR/$ARCH-disc0/build/base/* $RELEASEDIR/$ARCH-disc0-disc$BUILD_ISO/build/base/
  fi
  if [ -d $RELEASEDIR/$ARCH-disc0-disc1/build/base ] ; then
  	rm -r $RELEASEDIR/$ARCH-disc0-disc1/build/base
  	rmdir $RELEASEDIR/$ARCH-disc0-disc1/build
  fi
  if [ -d $RELEASEDIR/$ARCH-disc0-disc$BUILD_ISO/build/scripts/archive ] ; then
  	rm -r $RELEASEDIR/$ARCH-disc0-disc1/build/scripts/archive
  fi
fi
#Now copy contrib
if [ -d $RELEASEDIR/$ARCH/$SITEDIR/contrib/ ] ; then
   mkdir -p $RELEASEDIR/$ARCH-disc0-disc$CONTRIB_ISO/contrib/
   cd $RELEASEDIR/$ARCH/$SITEDIR//contrib/
   for i in *
   do
     if [ ! $i = "headers" ] ; then
	if [ -d $i ] ; then
        	mkdir $RELEASEDIR/$ARCH-disc0-disc$CONTRIB_ISO/contrib/$i
        	ln $RELEASEDIR/$ARCH/$SITEDIR/contrib/$i/* $RELEASEDIR/$ARCH-disc0-disc$CONTRIB_ISO/contrib/$i/
	fi
     fi
   done
   if [ -d $RELEASEDIR/$ARCH-disc0-disc$CONTRIB_ISO/contrib/SRPMS ] ; then
      echo "The src.rpm's for contrib rpms are on disc $TOTISO in the SRPMS/ directory" > $RELEASEDIR/$ARCH-disc0-disc$CONTRIB_ISO/contrib/SRPMS/README
   fi
   createrepo $RELEASEDIR/$ARCH-disc0-disc$CONTRIB_ISO/contrib
fi
#Clean up repodata
if [ -d $RELEASEDIR/$ARCH-disc0-disc1/repodata ] ; then
        rm -r $RELEASEDIR/$ARCH-disc0-disc1/repodata
fi
echo `date` > $RELEASEDIR/$ARCH-disc0-disc1/.$MEDIA
