#!/bin/bash

WEBDIR=$HOME"/MyWWWHome/web/~Frank.Luebeck/gap/GAPDoc/"
echo "Is ./index.html updated (Version, subdir, archive names, last updated)?"
echo "Copying files to this web directory, is that ok?"
echo "       "$WEBDIR
echo "Assuming that in this directory exist archives "
echo "       "GAPDoc-`cat version`"{.tar.bz2,,tar.gz,-win.zip}"

echo "Shall I start (y/N)?"
read -n 1 -p "-->" start
echo
if [ $start'X' != 'yX' ]; then
  echo Ok, please adjust and try again.
  exit 2
fi

mkdir -p $WEBDIR

echo making nocolor ps-versions of manual and example and HTML with external
echo links to www.gap-system.org . . .
cd doc
./clean
cd ../example
./clean
cd ..
rm -f tttttmppre
echo 'SetGapDocLaTeXOptions("nocolor");pathtoroot:="https://www.gap-system.org/Manuals";' >  tttttmppre
grep -v SetGapDocLaTeXOptions makedocrel.g >> tttttmppre
../../gap  < tttttmppre > /dev/null
../../gap  < tttttmppre > /dev/null
rm -f tttttmppre
pdftops doc/manual.pdf doc/manual.ps
pdftops example/manual.pdf example/manual.ps


echo making text versions of manual and example for web page . . .
rm -f gapdoc.txt example.txt example-nc.txt gapdoc-nc.txt
cat doc/chap{0,1,2,3,4,5,6,7,A,B,C,Bib,Ind}.txt > gapdoc.txt
cat example/chap{0,1,2,A,B,Bib,Ind}.txt > example.txt
../../gap   >/dev/null <<EOF
SetGAPDocTextTheme();
s:=StringFile("gapdoc.txt");
FileString("gapdoc-nc.txt", StripEscapeSequences(s));
FileString("gapdoc.txt", SubstituteEscapeSequences(s, GAPDocTextTheme));
s:=StringFile("example.txt");
FileString("example-nc.txt", StripEscapeSequences(s));
FileString("example.txt", SubstituteEscapeSequences(s, GAPDocTextTheme));
quit;
EOF

echo copying documentation to web page . . .
cp gapdoc.txt example.txt example-nc.txt gapdoc-nc.txt $WEBDIR

HERE=`pwd`

cd $WEBDIR
mkdir -p doc
mkdir -p example
mkdir ttttmpdir
cd ttttmpdir
tar xjf $HERE/GAPDoc-`cat $HERE/version`.tar.bz2 
cd *
mv CHANGES ../../CHANGES.txt
mv README.md ../../README.txt
mv PackageInfo.g ../../
mv doc/* ../../doc
mv example/* ../../example
cd ../..
rm -rf ttttmpdir

cd $HERE
cp doc/manual.ps doc/chap*.html $WEBDIR/doc/
cp example/manual.ps example/chap*.html $WEBDIR/example/

echo copying index.html and pkgreadme.css . . .
cp index.html pkgreadme.css $WEBDIR

echo copying archives . . .
cp GAPDoc-`cat version`{.tar.bz2,.tar.gz,-win.zip} $WEBDIR

echo "creating SERVE files (maybe restart server)"
cd $WEBDIR
rm -f SERVE
ls *.html > SERVE.tmp
sed -e 's/$/:text\/html;charset=utf-8/' SERVE.tmp > SERVE
rm SERVE.tmp
echo "gapdoc.txt:text/plain;charset=utf-8" >> SERVE
echo "gapdoc-nc.txt:text/plain;charset=utf-8" >> SERVE
echo "example.txt:text/plain;charset=utf-8" >> SERVE
echo "example-nc.txt:text/plain;charset=utf-8" >> SERVE
cd doc
rm -f SERVE
ls *.html > SERVE.tmp
sed -e 's/$/:text\/html;charset=utf-8/' SERVE.tmp > SERVE
rm SERVE.tmp
cd ../example
rm -f SERVE
ls *.html > SERVE.tmp
sed -e 's/$/:text\/html;charset=utf-8/' SERVE.tmp > SERVE
rm SERVE.tmp
cd $HERE


