#!/bin/bash

#set -x

echo check whether all.ast is up-to-date
make -C elsa all.ast
if cvs status elsa/all.ast | grep -q Up-to-date ; then
    echo all.ast is up-to-date
else
    echo please update else/all.ast before continuing the snapshot
    exit 1
fi

#elsarep=ithif59.inf.tu-dresden.de:/sun/theorie/tews/Privat/Store
if [ $(hostname) = "tandem" ] ; then
    elsarep=/home/tews/Store
else
    elsarep=tandem:/home/tews/Store
fi
repdir=src/elsa
create_tar_cache=1
snapshot_name=$(date +"elsa-2005.08.22b-olmar-%Y.%m.%d")
snapshot_tar=$snapshot_name.tar.gz

dir=/tmp/tews/elsa
cvscache=elsa.tar.gz

orig_elsa_release_tar=/home/tews/src/elsa-2005.08.22b.tar.gz
orig_elsa_release=/tmp/tews/elsa-2005.08.22b

rm -rf $(dirname $dir)/elsa*olmar*
rm -rf $dir
rm -rf $(dirname $dir)/build

elsa_cvs_dir=$(pwd)

echo ===========================================================================
echo ============================     make doc    ==============================
echo ===========================================================================

make doc

echo ===========================================================================
echo ============================     export    ================================
echo ===========================================================================


pushd $(dirname $dir)


if [ $create_tar_cache = 1 -a -f $cvscache ] ; then
    tar -xzf $cvscache
else
    cvs -d $elsarep export -d $(basename $dir) -r HEAD $repdir
    if [ $create_tar_cache = 1 ] ; then
	tar -czf $cvscache $(basename $dir)
    fi
fi

cd $dir

for d in smbase ast elkhound elkhound/c elsa asttools ; do
    cp $d/Makefile.in $d/Makefile
done

make distclean

# cp gendoc dirs
for d in smbase ast elkhound elsa ; do
    cp -a $elsa_cvs_dir/$d/gendoc $d
done

set -e
make -C $elsa_cvs_dir/asttools/doc TARGETDIR=$dir/asttools/doc copydoc-tar
set +e

echo ===========================================================================
echo ============================     ADDED FILES    ===========================
echo ===========================================================================

# added elsa/ChangeLog
# added elsa/ast/ast_util.ml
# added elsa/ast/ocamlhelp.cc
# added elsa/ast/ocamlhelp.h
# added elsa/ast/ocsstr.cc
# added elsa/ast/ocsstr.h
# added elsa/elsa/all.ast
# added elsa/elsa/ast_annotation.ml
# added elsa/elsa/ast_marshal.ml
# added elsa/elsa/caml_callbacks.ml
# added elsa/elsa/cc_ml_constructors.ml
# added elsa/elsa/cc_ml_types.ml
# added elsa/elsa/cc_ocaml.cc
# added elsa/elsa/cc_ocaml.h
# added elsa/elsa/doc/tracing-options.txt
# added elsa/elsa/elsa_util.ml
# added elsa/elsa/gnu_attribute_hack.ast
# added elsa/elsa/in/c/.cvsignore
# added elsa/elsa/in/c99/.cvsignore
# added elsa/elsa/in/gnu/bugs/.cvsignore
# added elsa/elsa/in/gnu/cil/.cvsignore
# added elsa/elsa/in/kandr/.cvsignore
# added elsa/elsa/in/msvc/.cvsignore
# added elsa/elsa/in/std/.cvsignore
# added elsa/elsa/ml_ctype.ast
# added elsa/elsa/ml_ctype_constructors.ml
# added elsa/elsa/ml_ctype.ml
# added elsa/elsa/mldeps.mk
# added elsa/asttools
# added elsa/snapshot
# added elsa/addcheck_ignored_files
#
# generated /tmp/tews/elsa-2005.08.22b/ast/agrampar.codes.h
# generated /tmp/tews/elsa-2005.08.22b/ast/agrampar.tab.cc
# generated /tmp/tews/elsa-2005.08.22b/ast/agrampar.tab.h
# generated /tmp/tews/elsa-2005.08.22b/elkhound/c/lexer1yy.cc
# generated /tmp/tews/elsa-2005.08.22b/elkhound/examples/arith/arithyy.cc
# generated /tmp/tews/elsa-2005.08.22b/elkhound/grampar.codes.h
# generated /tmp/tews/elsa-2005.08.22b/elkhound/grampar.tab.cc
# generated /tmp/tews/elsa-2005.08.22b/elkhound/grampar.tab.h
# compile errors	/tmp/tews/elsa-2005.08.22b/ast/exampletest.cc
# ?? /tmp/tews/elsa-2005.08.22b/elsa/gendoc/.xvpics
#       the file in there is also in elsa/doc


if [ ! -d $orig_elsa_release ] ; then
    if [ ! -f $orig_elsa_release_tar ] ; then
	echo "need location of elsa-2005.08.22b.tar.gz"
	exit 1
    fi
    
    pushd $(dirname $dir) > /dev/null
    tar -xzf $orig_elsa_release_tar
    if [ ! -d $orig_elsa_release ] ; then
	echo "something is wrong: untared original elsa but can't find it"
	exit 1
    fi
    popd > /dev/null
fi

cd $(dirname $dir)
diff -rq $(basename $dir) $orig_elsa_release | \
    grep -v -e 'differ$' | \
    sed -e 's/Only in //' -e 's|: |/|' | \
    grep -v -f $elsa_cvs_dir/addcheck_ignored_files

mv $(basename $dir) $snapshot_name

tar -czf $snapshot_tar $snapshot_name 

echo ===========================================================================
echo ===========================     tar generated    ==========================
echo ===========================================================================


echo -n "test distribution [Y/n]:"
read answer

if [  ${answer:=y} = y -o $answer = Y ] ; then
    mkdir build
    cd build
    tar -xzf ../$snapshot_tar
    cd $snapshot_name
    $elsa_cvs_dir/reconfigure
    make

    echo
    echo =======================================================================
    echo -n "run tests [Y/n]:"
    read answer
    if [  ${answer:=y} = y -o $answer = Y ] ; then
	(cd elsa; ./regrtest -ocaml)

	echo
	echo ===================================================================
	echo -n "continue tests [Y/n]:"
	read answer
	if [  ${answer:=y} = y -o $answer = Y ] ; then
	    (cd asttools; ./regtest-graph -parse)
	fi
    fi
fi


cd $elsa_cvs_dir
echo -n "copy olmar docs [Y/n]:"
read answer

if [  ${answer:=y} = y -o $answer = Y ] ; then
    make -C asttools/doc new-doc
fi

