#!/bin/bash

#----------------------------------------------------------------------

if [ ! -d wxPython ]; then  # TODO: make this test more robust
    echo "Please run this script from the root wxPython directory."
    exit 1
fi


VERSION=`python -c "import setup;print setup.VERSION"`
CONTRIBS="ogl gizmos"
DEST=wxPython-$VERSION/docs


# **** Make a directory to build up a distribution tree
mkdir -p _build_docs/$DEST
cd _build_docs
mkdir $DEST/wx

WXDIR=../..
INI=$WXDIR/docs/latex/wx/tex2rtf.ini

# **** Build the main docs using tex2rtf
echo "****" main "****"
cp $WXDIR/docs/latex/wx/*.gif $DEST/wx
cp $WXDIR/docs/latex/wx/*.css $DEST/wx
tex2rtf $WXDIR/docs/latex/wx/manual.tex $DEST/wx/wx.html -twice -html -macros $INI
cp $DEST/wx/wx_contents.html $DEST/wx/index.html
cp $DEST/wx/* $WXDIR/docs/html/wx

# **** and the contribs
for c in $CONTRIBS; do
    echo "****" $c "****"
    mkdir $DEST/$c
    cp $WXDIR/contrib/docs/latex/$c/*.gif $DEST/$c
    cp $WXDIR/contrib/docs/latex/$c/*.bmp $DEST/$c
    tex2rtf $WXDIR/contrib/docs/latex/$c/$c.tex $DEST/$c/$c.html -twice -html -macros $INI
    cp $DEST/$c/$c.html $DEST/$c/index.html || cp $DEST/$c/${c}_contents.html $DEST/$c/index.html
done


# **** zip the docs into "books"
pushd $DEST
pushd wx
zip ../wx.zip *
popd
rm -r wx

for c in $CONTRIBS; do
    pushd $c
    zip ../$c.zip *
    popd
    rm -r $c
done

popd
cp ../distrib/viewdocs.py $DEST
cp ../distrib/README.viewdocs.txt $DEST/README.txt
cp ../docs/xml/wxPython-metadata.xml $DEST

rm -f ../dist/wxPython-docs-$VERSION.tar.gz
tar cvf ../dist/wxPython-docs-$VERSION.tar $DEST
gzip -9 ../dist/wxPython-docs-$VERSION.tar


# **** Cleanup
cd ..
rm -r _build_docs