#!/bin/bash 

echo ""
echo "Start this script from a copy of GAPDoc within the GAP root you want"
echo "to use for compiling the documentation. Also,"
echo "did you adjust (yN)?: Version, Date, ArchiveURL in ./PackageInfo.g"
echo "                ./version"
echo "                Date. copyright date, Version in doc/gapdoc.xml"
echo "                                                 example/example.xml"
echo ""
read -n 1 start
echo
if [ $start'X' != 'yX' ]; then
  echo Ok, please adjust and try again.
  exit 2
fi

# the GAP version to use for building documentation (with
# relative external links)
USEGAP=/usr/local/ca/gap-rsync

CDIR=`pwd`
dirnam=GAPDoc-`cat ./version`

cd /cache
mkdir -p gaptmp
cd gaptmp
rsync -av $USEGAP/ ./
cd pkg
rm -rf GAPDoc*
echo "Checking out current version into /cache/gaptmp/pkg/"$dirnam" . . ."
mkdir $dirnam
cd $dirnam
cp -a $CDIR/.git .
git co master .
echo "building documentation (twice for cross refs)"
echo "" | ../../gap -r -l "../..;"  makedocrel.g 
echo "" | ../../gap -r -l "../..;"  makedocrel.g 

echo "creating test files"
rm -rf tst
mkdir tst
echo "" | ../../gap -r -l "../..;" maketest.g

echo "removing unwanted files . . ."
rm -r pack TODO clean index.html pkgreadme.css
rm -rf .git .gitignore
rm -rf */*.{log,aux,blg,brf,bbl,idx,ilg,ind,pnr} */*.xml.bib */.cvsignore
rm -f ./makedoc.g ./maketxt ./doku toweb

echo "creating archive . . ."
cd ..
tar cf $dirnam.tar $dirnam
bzip2 -9 $dirnam.tar
echo Cleaning up
mv  $dirnam.tar.bz2 $CDIR/
cd $CDIR

echo "Remove copy of GAP in /cache/gaptmp (y/n)?"
read -n 1 start
echo
if [ $start'X' == 'yX' ]; then
  echo "Ok, removing ..."
  rm -rf /cache/gaptmp
fi

echo New archive in ./$dirnam.tar.bz2
echo If ok then call
echo "      repack.py "$dirnam".tar.bz2"

echo "If you are happy with this archive 
#tag the current state before"
echo "use the ./toweb script to update the webpage."
echo "Then change version number back to '1.dev'."

