#!/bin/bash

fast=0

function usage(){
    echo "usage: make-distribution"
    exit 1
}


while [ $# \!= 0 ]; do
    case $1 in
	# -fast)		fast=1;
	#     		shift;;
	*)		echo `basename $0` : unknown option $1;
		        usage;
			shift;;
    esac;
done


modified=$(cvs status 2>/dev/null | grep Status | grep -v Up-to-date | wc -l)
if [ $modified != 0 ] ; then
    echo working copy not up-to-date --- commit first
    exit 1
fi

make -C doc clean otags.html

branch=$(cvs status Makefile.in | awk  '/Sticky Tag/ {print $3;}')
if [ $branch = '(none)' ] ; then
    branch=HEAD
fi

echo using branch $branch

export CVSROOT=$(cat CVS/Root)
echo using CVSROOT $CVSROOT


tmpdir=/tmp/tews
otags_version=$(./configure -abra-print-version)
name="otags-reloaded-$otags_version"

rm -rf $tmpdir/$name

pushd $tmpdir
set -x
cvs export -d $name -r $branch src/otags > /dev/null
set +x

filestodelete="test/ccsl.* test/otags.* test/loc_t.ml"

for f in $filestodelete ; do
    rm -f $name/$f
done


popd

filestocopy="doc/otags.html"

for f in $filestocopy ; do
    cp $f $tmpdir/$name/$f
done

pushd $tmpdir

tar -czf $name.tar.gz $name

popd

rm -rf $tmpdir/test-otags*

set -e
if [ $fast = 0 ] ; then
    ./test-distribution byte --bytecode
fi
./test-distribution native
set +e

webdir=/home/tews/public_html/alfa/otags

echo
echo cp doc/otags.man CHANGES $tmpdir/$name.tar.gz $webdir
echo -n [N/y]?
read answer

if [  ${answer:=n} = n -o $answer = N ] ; then
    exit 0
fi

cp doc/otags.html $webdir/otags.man.html
cp $tmpdir/$name.tar.gz $webdir
cp CHANGES $webdir
