#! /bin/bash

set -o errexit

CHECKOUT=pristine-checkout
DIST=downward-`date --iso-8601`
PACKAGE=$DIST.tar.gz
SEARCH_OPTIONS="
    --heuristic h1=cg()
    --heuristic h2=ff()
    --search lazy_greedy(h1,h2,preferred=(h1,h2))"

echo -n "Looking for validator... "
which validate || (echo "not found!" && exit 1)

if [[ -e $CHECKOUT ]]; then
    echo Removing existing working copy $CHECKOUT...
    rm -rf $CHECKOUT
fi

if [[ -e $DIST ]]; then
    echo Removing existing distribution directory $DIST...
    rm -rf $DIST
fi

if [[ -e $DIST ]]; then
    echo Removing existing distribution package $PACKAGE...
    rm $PACKAGE
fi

echo "Checking out code..."
svn checkout --quiet svn+ssh://downward/trunk/downward $CHECKOUT
rm -rf $(find $CHECKOUT -regex '.*/\.svn$')

echo "Preparing distribution directory..."

mkdir $DIST

cp -r $CHECKOUT/translate $DIST/
cp -r $CHECKOUT/preprocess $DIST/
cp -r $CHECKOUT/search $DIST/
cp $CHECKOUT/build_all $DIST/

COMPLAINED=false
cd $DIST
for filename in $(find . -type f); do
    ## Test if file is known to be good (don't do anything),
    ## known not to belong into the distribution (remove it), or
    ## not known (complain).
    if [[ \
        $filename =~ ^\./build_all$ || \
        $filename =~ ^\./translate/.*\.py$ || \
        $filename =~ ^\./(preprocess|search)/.*\.(cc|h)$ || \
        $filename =~ ^\./(preprocess|search)/Makefile$ || \
        $filename =~ ^\./search/lp/(setup|Osi-0\.103\.0\.tgz)$ || \
        0 == 1 ]]; then
        :
    elif [[ \
        $filename =~ ^\./translate/no-invariants\.patch$ || \
        $filename =~ ^\./translate/regression-tests/.*$ || \
        $filename =~ ^\./translate/run-additive-hmax$ || \
        $filename =~ ^\./preprocess/output-format\.txt$ || \
        $filename =~ ^\./search/Doxyfile$ || \
        $filename =~ ^\./search/downward\.cfg$ || \
        0 == 1 ]]; then
        rm $filename
    else
        echo "unknown file: $filename"
        COMPLAINED=true
    fi
done
cd ..

if [[ $COMPLAINED == true ]]; then
    echo "Found unknown files -- exiting."
    echo "If there is nothing wrong, update the filename patterns in the"
    echo "make_dist script."
    exit 1
fi

mkdir $DIST/doc
cp data/README $DIST/
cp data/doc/sas-format.txt $DIST/doc/
cp data/doc/pre-format.txt $DIST/doc/
cp data/doc/fast-downward.pdf $DIST/doc/
cp data/doc/translator.pdf $DIST/doc/

# Packaging
echo "Packaging..."
tar czf $PACKAGE $DIST/
rm -rf $DIST

# Sanity tests
echo "Sanity test: Unpacking package..."
tar xzf $PACKAGE

echo "Sanity test: Compiling..."
cp -r data/tests $DIST/
cd $DIST
./build_all > /dev/null

function planner-test-run () {
    TESTID=$1
    echo "Sanity test: $TESTID..."
    translate/translate.py \
        tests/$TESTID-domain.pddl tests/$TESTID-problem.pddl \
        > /dev/null
    preprocess/preprocess < output.sas > /dev/null
    search/downward $SEARCH_OPTIONS < output > /dev/null

    diff -u sas_plan tests/$TESTID-plan || \
        echo WARNING: did not get the expected plan
    echo Validating...

    ## Note: As of now, VAL doesn't appear to always return a useful
    ## exit code, so we check for output instead.
    ## The "|| true" part makes sure that even if VAL does return an
    ## error exit, we still get the contents of validate.log reported.
    ## VAL seems to sometimes generate newlines on successful runs,
    ## hence we use grep to check for (and report) nonblank lines.
    validate -s tests/$TESTID-domain.pddl tests/$TESTID-problem.pddl \
        sas_plan &> validate.log || true
    grep . validate.log && MATCH=true || MATCH=false
    if [[ $MATCH == true ]]; then
        echo validator failed -- exiting
        exit 1
    fi
    rm -f output.sas output sas_plan
}

planner-test-run test1
planner-test-run test2

echo "Cleaning up..."
cd ..
rm -rf $CHECKOUT
rm -rf $DIST

echo "Publishing..."
cp $PACKAGE /lummerland/.public_html/
echo "Published: http://www.informatik.uni-freiburg.de/~helmert/$PACKAGE"
