#!/usr/bin/bash

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/git operations.
echo
echo Count to ten ... slowly ...
sleep 10

#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

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

# TODO Could set BRANCH from cmdline arg
BRANCH=master
git clone --branch $BRANCH --depth 1 git://sourceware.org/git/systemtap.git

# Checkout the htdocs CVS^H^H^Hgit

git clone ssh://sourceware.org/git/systemtap-htdocs.git 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/

cd $BASEDIR/htdocs
git rm $BASEDIR/htdocs/langref/*
git 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/
git add langref/*.html
git add langref/*.css
git commit -m 'add new langref/*'
cd -

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

cd $BASEDIR/htdocs
git rm $BASEDIR/htdocs/tutorial/*
git 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
git add tutorial/*.html
git add tutorial/*.css
git 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
git add tapsets/*.html && git commit -m 'add new tapsets/*.html'

#echo 'tapsets html cp'

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

cd $BASEDIR/htdocs
git rm man/*.html && git commit -m 'removed man pages'

cd $BASEDIR/htdocs/man

./REGEN.sh $BASEDIR/systemtap/local

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

echo Documentation generated, now make sure to cd $BASEDIR/htdocs
echo then review the results and git push to update the website.

