#!/usr/bin/bash

#check for man2html
#TODO perhaps wget, untar, and automatically use, then remove if we want?
# http://dcssrv1.oit.uci.edu/indiv/ehood/tar/man2html3.0.1.tar.gz

echo This script regenerates the 'sourceware.org/systemtap/{tapset,man,...}' docs
echo from a NEW checkout of git main systemtap and a partial build.
echo
echo Consider running this script under ssh-agent, with an ssh-add-ed identity,
echo in order to prevent interactive blockage at the many ssh/cvs operations.
echo
echo Count to ten ... slowly ...
sleep 10


if which man2html &>/dev/null; then
 :
else
echo "Please install man2html package"
exit
fi

# Create the directory we'll be working in

DIR=`mktemp -d stapdocsXXX -p /tmp/`; cd $DIR

# We'll need to build the latest stap with docs, ensure a check that everything built properly or bail

git clone --depth 1 git://sourceware.org/git/systemtap.git

# Checkout the htdocs CVS

cvs -d :ext:sourceware.org:/cvs/systemtap checkout htdocs

BASEDIR=`pwd`
cd systemtap

mkdir build && mkdir local && cd build

../configure --enable-docs --enable-htmldocs --prefix=$BASEDIR/systemtap/local

make -C doc
make install-data
if [ $? -eq 0 ]; then
 : 
else
echo "Make failed, please address build errors"
exit
fi

### LANGREF
cp doc/langref.pdf $BASEDIR/htdocs/

rm $BASEDIR/htdocs/langref/*
cd $BASEDIR/htdocs
cvs rm && cvs commit -m 'removed langref/* bits'

cd $BASEDIR/htdocs/langref
htlatex $BASEDIR/systemtap/doc/langref.tex "html,2"
cp langref.html index.html
cd $BASEDIR/htdocs/
cvs add langref/*.html
cvs add langref/*.css
cvs commit -m 'add new langref/*'
cd -


### TUTORIAL
cp $BASEDIR/systemtap/build/doc/tutorial.pdf $BASEDIR/htdocs/

rm -f $BASEDIR/htdocs/tutorial/*
cd $BASEDIR/htdocs
cvs rm && cvs commit -m 'removed tutorial/* bits'

cd $BASEDIR/htdocs/tutorial
htlatex $BASEDIR/systemtap/doc/tutorial.tex "html,2"
cp tutorial.html index.html

cd $BASEDIR/htdocs
cvs add tutorial/*.html
cvs add tutorial/*.css
cvs commit -m 'add new tutorial/*'

# beginners guide
# we require xmlto version newer than 0.0.23-3
# TODO add a version check

cd $BASEDIR/systemtap/build/doc/beginners

cp SystemTap_Beginners_Guide.pdf $BASEDIR/htdocs/

#echo 'systemtap_beginners_guide.pdf cp'

rm $BASEDIR/htdocs/SystemTap_Beginners_Guide/*.html
cp -R SystemTap_Beginners_Guide $BASEDIR/htdocs/
#echo 'systemtap_beginners_guide recursive cp'
# tapsets 
cd ../..

cp $BASEDIR/systemtap/build/doc/SystemTap_Tapset_Reference/tapsets.pdf $BASEDIR/htdocs/

#echo 'tapsets.pdf cp'

rm $BASEDIR/htdocs/tapsets/*.html

cp -R $BASEDIR/systemtap/build/doc/SystemTap_Tapset_Reference/tapsets/*.html $BASEDIR/htdocs/tapsets/

cd $BASEDIR/htdocs
cvs add tapsets/*.html && cvs commit -m 'add new tapsets/*.html'

#echo 'tapsets html cp'

# systemtap examples
# run gitweb.php script?
# run examples-index-gen.pl script if needed?

rm man/*.html && cvs rm * && cvs commit -m 'removed man pages'

cd $BASEDIR/htdocs/man

./REGEN.sh $BASEDIR/systemtap/local

cd $BASEDIR/htdocs
cvs add man/*.html && cvs commit -m 'added new man pages'
echo -- $BASEDIR


