#!/bin/sh
# you need a shell that understands (( ... ))
# On irix sh==ksh, for Linux/Cygwin use bash
# reason: windows takes ages to create processes, and we need a=$a+1 a lot

# 
# script to create a tarball of the current state of OpenSG
#

# it mainly just checks out the CVS and removes the stuff we don't want 
# distributed.

# utility functions

# Main script starts here

# setup: error handling etc.

##set -e
##trap errorexit  ERR HUP INT QUIT
##trap normalexit EXIT

if test ${#*} -lt 1
then
    OSGPOOL=`pwd`/OpenSG
else
    OSGPOOL=$1
    shift
fi

echo "Making source/doc dist in " $OSGPOOL

# does the OSGPOOL exist? If not, check it out.

if ! test -d $OSGPOOL
then
    # Check out the sources
    
    # little trick to get around the password input
    # not nice but works. Anyone got a better solutions?
    
    cvs_ok=`fgrep -l anonymous@cvs.opensg.sourceforge.net:/cvsroot/opensg $HOME/.cvspass`
    if test "x$cvs_ok" = x
    then
        echo ":pserver:anonymous@cvs.opensg.sourceforge.net:/cvsroot/opensg A" >> $HOME/.cvspass
    fi
    
    # go where it's suppoosed to be
    cd `dirname $OSGPOOL`
    
    # cvs needs to be in the path!
    cvs -d:pserver:anonymous@cvs.opensg.sourceforge.net:/cvsroot/opensg co -P OpenSG 

    # mv OpenSG `basename $OSGPOOL`
fi

# lets do it
cd $OSGPOOL

# get the version
VERSION=`cat VERSION`

# remove the internal stuff
rm -rf dist

# remove the CVS stuff
find . -name CVS | xargs rm -rf

# pack it up
cd ..

# move existing docs out of the way
if test -d OpenSG/Doc/Code/user
then
    mv OpenSG/Doc/Code/user .
fi
if test -d OpenSG/Doc/Code/ext
then
    mv OpenSG/Doc/Code/ext .
fi
if test -d OpenSG/Doc/Code/dev
then
    mv OpenSG/Doc/Code/dev .
fi

mv $OSGPOOL OpenSG-$VERSION

echo "Pack sources..."
tar cf - OpenSG-$VERSION | gzip -9 > OpenSG-$VERSION-src.tgz

# move existing docs back
if test -d user
then
    mv user OpenSG-$VERSION/Doc/Code
fi
if test -d ext
then
    mv ext OpenSG-$VERSION/Doc/Code
fi
if test -d dev
then
    mv dev OpenSG-$VERSION/Doc/Code
fi

# go back and make the docs
cd OpenSG-$VERSION

./configure

if test x$OSG_DIST_NO_MAKEDOC = x
then
    echo "Make docs..."
    make alldocs
else
    echo "OSG_DIST_NO_MAKEDOC set, skipping doc make"
fi

# copy the starter guides

cp Doc/Code/user/latex/StarterGuide.pdf ../OpenSG-$VERSION-UserStarter.pdf
cp Doc/Code/dev/latex/StarterGuide.pdf ../OpenSG-$VERSION-DevStarter.pdf

cp Doc/Code/user/latex/StarterGuide.pdf Doc/Code/user/UserStarter.pdf
cp Doc/Code/dev/latex/StarterGuide.pdf Doc/Code/dev/DevStarter.pdf

# get rid of the LaTeX temps

rm -rf Doc/Code/user/latex Doc/Code/dev/latex

# make the design document

cp Doc/design/opensg_design.html ../OpenSG-$VERSION-Design.html

cd ..

# pack it all up 

echo "Pack sources & docs..."
tar cf - OpenSG-$VERSION | gzip -9 > OpenSG-$VERSION-src+doc.tgz

# pack just the docs

cd OpenSG-$VERSION/Doc/Code

mkdir OpenSG-$VERSION-srcdoc
mv user dev OpenSG-$VERSION-srcdoc

echo "Pack docs..."
tar cf - OpenSG-$VERSION-srcdoc | gzip -9 > ../../../OpenSG-$VERSION-srcdoc.tgz

mv OpenSG-$VERSION-srcdoc/* .
rm -rf OpenSG-$VERSION-srcdoc

# that's it!

cd ../../..

mv OpenSG-$VERSION OpenSG

