#!/bin/sh
set -e

cleanup() {
    cd $PDIR

    #Clean up after smoke-setup
    cleanup_file=debian/tests/pkg-perl/smoke-cleanup
    if [ -x $cleanup_file ]
    then
        ( export TDIR; $cleanup_file )
    fi

    rm -rf $TDIR
}

TEMP=${ADTTMP:-${TMPDIR:-/tmp}}

TDIR=$(mktemp -d $TEMP/smokeXXXXXX)

PDIR=`pwd`

trap cleanup EXIT

file_list=debian/tests/pkg-perl/smoke-files
if [ ! -r $file_list ]; then
    # backward compatibility squared for now
    file_list=debian/tests/pkg-perl/test-files
fi
if [ ! -r $file_list ]; then
    # backward compatibility for now
    file_list=debian/tests/test-files
fi

export AUTOMATED_TESTING=1
export NONINTERACTIVE_TESTING=1

# overridable with smoke-env
PKG_PERL_PROVE_ARGS="--merge"

env_list=debian/tests/pkg-perl/smoke-env
if [ ! -r $env_list ]; then
    env_list=debian/tests/pkg-perl/env-smoke
fi
if [ -r $env_list ]; then
    eval $(sed '/^ *\(#\|$\)/d; s/^/export /' $env_list)
fi

for dir in common handler portal manager; do
    TDIR2=${TDIR}/lemonldap-ng-$dir
    mkdir -p $TDIR2/blib/lib \
        $TDIR2/blib/arch \
        $TDIR2/lib/Debian/pkgperl \
        $TDIR2/blib/lib/Debian/pkgperl
    cp -a lemonldap-ng-$dir/t $TDIR2/
    cat <<'EOF' > $TDIR2/lib/Debian/pkgperl/Foobar.pm
package Debian::pkgperl::Foobar;
our $VERSION = '0.01';
1;
__END__
=head1 NAME

Debian::pkgperl::Foobar - dummy module for test checkers

=cut
EOF
    cp $TDIR2/lib/Debian/pkgperl/Foobar.pm $TDIR2/blib/lib/Debian/pkgperl

    if [ ! -e $TDIR2/MANIFEST ]; then
        cat <<'EOF' > $TDIR2/MANIFEST
lib/Debian/pkgperl/Foobar.pm
EOF
    fi

    if [ ! -e $TDIR2/MANIFEST.SKIP ]; then
        cp /dev/null $TDIR2/MANIFEST.SKIP
    fi
done

# Missing file
cp  lemonldap-ng-common/lemonldap-ng.ini ${TDIR}/lemonldap-ng-common

# Test that have no interest
rm -f $TDIR/*/t/99-pod.t \
    $TDIR/lemonldap-ng-manager/t/03-HTML-forms.t \
    $TDIR/lemonldap-ng-manager/t/20-test-coverage.t \
    $TDIR/lemonldap-ng-manager/t/80-attributes.t \
    $TDIR/lemonldap-ng-manager/t/90-translations.t

for INI in $(find $TDIR/lemonldap-ng-* -name lemonldap-ng.ini); do
    echo "Change $INI logLevel to debug" >&2
    perl -pi -e 's/^logLevel\s*=\s*\w+$/logLevel=debug/' $INI
done

for dir in common handler portal manager; do
    TDIR2=${TDIR}/lemonldap-ng-$dir

    # this is intended to be a last resort, please use it responsibly
    setup_file=debian/tests/pkg-perl/smoke-setup
    if [ -x $setup_file ]
    then
        ( export TDIR2; $setup_file )

        # Evaluate skip list a second time since smoke-setup might have
        # generated some of the to-be-skipped files.
        if [ -r $skip_list ]; then
            egrep -v '^ *(#|$)' $skip_list | while read file; do
                rm -f $TDIR2/$file
            done
        fi
    fi

    tests_file=$(pwd)/debian/tests/pkg-perl/smoke-tests
    cd $TDIR2
    if [ -r $tests_file ]; then
        test_targets=$(eval ls -d $(egrep -v '^#' $tests_file) 2>/dev/null || true)
    fi

    if command -v xvfb-run >/dev/null
    then
        XVFB="xvfb-run -a"
    else
        XVFB=
    fi

    test_targets=$(ls -d t/*.t 2>/dev/null || true)

    if [ ! -n "$test_targets" ]; then
        echo 'Nothing to prove, skipping.'
    else
        $XVFB prove -I"$TDIR2" --blib --verbose $PKG_PERL_PROVE_ARGS $test_targets 2>&1
    fi

    cd -

done
