#!/bin/sh -e

TESTSOURCES="dhtest dhtest.nostrip dhtest.noargs dhtest.archspecific dhtest.tmpdirarg dhtest.compat1 dhtest.compat1override dhtest.compatenvvar dhtest.dbg dhtest.dbg2 dhtest.dbg3 cdbstest"

print_result() {
    if [ -n "$fail" ]; then
	echo "FAIL: $1"
	echo "Output:"
	echo '---------------------------------------------------------------------'
	echo "$2"
	echo '---------------------------------------------------------------------'
	echo Please remove testsuite.tmp after examination
	exit 1
    else
	echo "PASS: $1"
    fi
    unset fail
    return 0
}

# Arguments: <command> <test suite description>
# execute given command, pass test if it exits successfully, fail test if not.
# In the latter case, print command output and abort
check_command() {
    out=`$1 2>&1` || fail=1	
    print_result "$2" "$out"
}

# Arguments: <application deb> <corresponding dbgsym deb>
check_pkg_pair() {
    deb="$1"
    ddeb="$2"
    mkdir unpacked
    dpkg -x $deb unpacked
    dpkg -x $ddeb unpacked

    for bin in unpacked/usr/bin/*; do
	# check for debuglink section
	objdump -j .gnu_debuglink -s $bin | grep -q .gnu_debuglink || fail=1
	print_result "$bin has .gnu_debuglink section" "`objdump -j .gnu_debuglink -s $bin`"

	# check for good backtrace
	bt=`gdb --batch -ex "symbol-file unpacked/usr/lib/debug/${bin##unpacked/}" -ex run -ex 'bt' $bin`
	echo "$bt" | grep -q 'g (x=1, y=42)' || fail=1
	print_result "$bin: executable and debug symbols produce good backtrace" "$bt"
    done
    rm -rf unpacked
}

ROOTDIR=$(readlink -f `dirname $0`/..)
cd $ROOTDIR/tests
ORIGDIR=`pwd`
mkdir testsuite.tmp
cd testsuite.tmp

for s in $TESTSOURCES; do
    rm -rf *

    # copy source package into test directory
    cp -r ../$s .

    # build it
    cd $s
    check_command "env PATH=$ROOTDIR:$PATH fakeroot dpkg-buildpackage -us -uc -b" "Building source package $s"
    # clean it
    check_command "fakeroot debian/rules clean" "Cleaning source package $s"
    cd ..
    # check for leftovers
    check_command "diff -ur $s ../$s" "No leftover files after cleaning $s"

    for deb in *.deb; do
	pkg=`echo $deb|cut -d_ -f1`
	version=`echo $deb|cut -d_ -f2`
	version=${version#*:} # strip off epoch
	arch=`echo ${deb%.deb}|cut -d_ -f3`

	if echo $pkg | grep -q -- '-dbg$'; then
	    check_pkg_pair "${pkg%-dbg}_${version}_${arch}.deb" "${pkg}_${version}_${arch}.deb"
	else
	    if dpkg -c $deb | grep -q usr/bin && ! echo $deb | grep -q nostrip; then
		check_command "test -f ${pkg}-dbgsym_${version}_${arch}.ddeb" "dbgsym ddeb for $deb exists"
		check_pkg_pair "${pkg}_${version}_${arch}.deb" "${pkg}-dbgsym_${version}_${arch}.ddeb"
	    else
		check_command "test ! -f ${pkg}-dbgsym_${version}_${arch}.ddeb" "No dbgsym ddeb for $deb"
	    fi
	fi
    done

    # check that all existing -dbg packages are conflicted to in -dbgsym
    dbg_pkgs=`ls *-dbg_*deb 2>/dev/null| cut -f1 -d_`
    for ddeb in *.ddeb; do
        for c in $dbg_pkgs; do
            dpkg -I $ddeb | grep '^[[:space:]]*Conflicts: ' | grep -qw $c || fail=1
            print_result "$ddeb conflicts to $c" "`dpkg -I $ddeb`"
        done
    done
done

cd "$ORIGDIR"
rm -r testsuite.tmp
