MYPWD=`pwd`
up1=`dirname $MYPWD`
up2=`dirname $up1`
site=`basename $up2`
echo "SITE is $site"
up3=`dirname $up2`
up4=`dirname $up3`
arch=`basename $up4`
up5=`dirname $up4`
#The following is the update number of this release
update=2
if [ -z $site ] ; then
	echo "What would you like your site name to be, one word only please"
	read site 
fi
product="Scientific Linux ($site)"
echo "-------------------------------------------------------------"
echo "What would you like your site version to be"
read version 
#Fix up locations.include
/bin/sed -e "s/^SITE=.*$/SITE=$site/ ; s/^PRODUCT=\".*\"/PRODUCT=\"$product\"/ ; s/^VERSION=.*$/VERSION=$version/ ; s,^RELEASEDIR=.*$,RELEASEDIR=$up5, ; s/^ARCH=.*$/ARCH=$arch/ " locations.include > locations.include.new
head locations.include.new
echo "Are these ok, y or n"
read answer
if [ "$answer" = "y" ] ; then
  mv locations.include.new locations.include
else
  if [ "$answer" = "" ] ; then
    mv locations.include.new locations.include
  else
    echo "Since you did not like these I am not changing anything"
    exit
  fi
fi
#Install SRPM for example-release and turn it into $site-release
answer=""
answer=`ls ../../Updates/example-release-*.*.rpm 2>/dev/null`
if [ ! -z "$answer" ] ; then
  rpm -i ../../../example/SRPMS/example-release*.src.rpm
  /bin/sed -e "s/Example Linux/$product/ ; s/builtin_release_codename .*$/builtin_release_codename $site/ ; s/builtin_release_shortname .*$/builtin_release_shortname $site/ ; s/Release: .*$/Release: $update/ ; s/^%define release_version .*$/\%define release_version $version/" /usr/src/redhat/SPECS/example-release.spec > /usr/src/redhat/SPECS/$site-release.spec
  rpmbuild -ba /usr/src/redhat/SPECS/$site-release.spec 
  cp /usr/src/redhat/RPMS/$arch/$site-release-$version-$update.$arch.rpm ../../Updates/
  rm ../../Updates/example-release-*.$arch.rpm
  cp /usr/src/redhat/SRPMS/$site-release-$version-$update.src.rpm ../../SRPMS/
# Need to make sure that the comps.xml files point to this new $site-release rpm
  /bin/sed -e " s/example-release/$site-release/ " ../../base/comps.xml > ../../base/comps.xml.transform
  mv ../../base/comps.xml.transform ../../base/comps.xml
  if [ -f ../../base/comps.xml.main ] ; then
    /bin/sed -e " s/example-release/$site-release/ " ../../base/comps.xml.main > ../../base/comps.xml.main.transform
    mv ../../base/comps.xml.main.transform ../../base/comps.xml.main
  else
    cp ../../base/comps.xml ../../base/comps.xml.main
  fi
fi
#FIXME up yum-conf to point to new site yum mirror
#
#FIX up ../../base/scripts/comps.site.spec to have info about new site
/bin/sed -e "s/^%define site .*$/%define site $site/ ; s/^%define compsversion .*$/%define compsversion $version/ ; s,^%define treedir .*$,%define treedir $up5, " ../../base/scripts/comps.site.spec > ../../base/scripts/comps.site.spec.new 
cp ../../base/scripts/comps.site.spec.new ../../base/scripts/comps.site.spec 
rm ../../base/scripts/comps.site.spec.new
#
#Now fix up build.release.site.sh --product entry
/bin/sed -e " s/Scientific Linux (example)/$product/ " build.release.site.sh > build.release.site.sh.new 
cp build.release.site.sh.new build.release.site.sh
rm build.release.site.sh.new
#
#Now fix up isolinux/*cfg entries site=example -> site=$site 
#add code to add site= if not there already
#
echo "Fixing up ../isolinux/ files"
grep "site=" ../isolinux/isolinux.cfg 
if [ $? -eq 1 ] ; then 
   /bin/sed -e "/append/s/$/ site=$site/" ../isolinux/isolinux.cfg > ../isolinux/isolinux.cfg.new
   /bin/sed -e "/append/s/$/ site=$site/" ../isolinux/syslinux.cfg > ../isolinux/syslinux.cfg.new
   cp ../isolinux/isolinux.cfg.new ../isolinux/isolinux.cfg 
   cp ../isolinux/syslinux.cfg.new ../isolinux/syslinux.cfg 
   rm ../isolinux/isolinux.cfg.new
   rm ../isolinux/syslinux.cfg.new
fi
#
#Now fix up isolinux/boot.msg entries Example Linux -> $product 
/bin/sed -e "s/Scientific Linux (example)/$product/" ../isolinux/boot.msg > ../isolinux/boot.msg.new
cp ../isolinux/boot.msg.new ../isolinux/boot.msg 
rm ../isolinux/boot.msg.new
#
#This is for the case where site=example exists because they copied site/example
/bin/sed -e " s/site=example/site=$site/ " ../isolinux/isolinux.cfg > ../isolinux/isolinux.cfg.new 
/bin/sed -e " s/site=example/site=$site/ " ../isolinux/syslinux.cfg > ../isolinux/syslinux.cfg.new 
cp ../isolinux/isolinux.cfg.new ../isolinux/isolinux.cfg 
cp ../isolinux/syslinux.cfg.new ../isolinux/syslinux.cfg 
rm ../isolinux/isolinux.cfg.new
rm ../isolinux/syslinux.cfg.new
#
#fix up rpmdb
if [ -f rpmdb-example.spec.in ] ; then
  /bin/sed -e " s/^%define rpmdbname .*$/%define rpmdbname $site/ ; s/^%define rpmdbversion .*$/%define rpmdbversion $version/ ; s/^%define rpmmarketingname .*$/%define rpmmarketingname $product/ ; s,^%define rpmdbtree .*$,\%define rpmdbtree $up5, " rpmdb-example.spec.in > rpmdb-$site.spec.in 
  rm rpmdb-example.spec.in
fi
#
# Need to make sure that the comps.xml files point to this new rpmdb-$site rpm
/bin/sed -e " s/rpmdb-redhat/rpmdb-$site/ ; s/rpmdb-example/rpmdb-$site/ " ../../base/comps.xml > ../../base/comps.xml.transform
mv ../../base/comps.xml.transform ../../base/comps.xml
if [ -f ../../base/comps.xml.main ] ; then
  /bin/sed -e " s/rpmdb-redhat/rpmdb-$site/ ; s/rpmdb-example/rpmdb-$site/ " ../../base/comps.xml.main > ../../base/comps.xml.main.transform
  mv ../../base/comps.xml.main.transform ../../base/comps.xml.main
else
  cp ../../base/comps.xml ../../base/comps.xml.main
fi
#Now check that we do not already have a rpmdb-* in Updates with a different
#name include rpmdb-example
answer=`ls ../../Updates/rpmdb-*.$arch.rpm 2>/dev/null`
if [ ! -z "$answer" ] ; then
  echo "-------------------------------------------------------------"
  echo "You have rpmdb-*.$arch.rpm. in your new Updates area, I will "
  echo "remove it so as a new one will be rebuilt with your site name"
  rm ../../Updates/rpmdb-*.$arch.rpm
fi
#
echo "-------------------------------------------------------------"
echo "Now add/remove rpms in $up4/sites/$site/Updates for your site ."
echo "-------------------------------------------------------------"
echo "You will now need to"
echo "cd $up4/sites/$site/build/scripts"
echo "then run make.everything.sh answering y to the make images question"
echo " -------------------------------------------------------------"
