#!/bin/bash

if [ "$(id -u)" -ne 0 ]; then
    echo >&2 "ERR: root needed"
    exit 1
fi

set -u
set -e

DIR="$(pwd)/workdir"
PKGDIR="$DIR/pkg"
LOCDIR="$DIR/loc"
PKGNAME="pkgname"
PKGF="$PKGDIR/conffile"
LOCF="$LOCDIR/conffile"
PKGRF="$PKGDIR/renamedconffile"
LOCRF="$LOCDIR/renamedconffile"
REFDIR="reference"
PKGREF="$REFDIR/pkg"
LOCREF="$REFDIR/loc"
PKGRREF="$REFDIR/renamedpkg"
LOCRREF="$REFDIR/renamedloc"
LOCNEWF="$DIR/localnewfile"

TESTEE="ucf-helper-functions.sh"
if [ -x "./$TESTEE" ]; then
    . ./$TESTEE
fi

cd tests

cleanup() {
    for fn in $LOCF $LOCNEWF $LOCRF; do
        ucf --purge $fn
        ucfr --purge $PKGNAME $fn
    done
    rm -rf $DIR $PKGDIR $LOCDIR $REFDIR
    rm -f result
}

prepare() {
    cleanup
    mkdir $DIR $PKGDIR $LOCDIR $REFDIR
    cp package-orig $PKGF
}

take_ref() {
    if [ -e "$PKGF" ]; then
        cp $PKGF $PKGREF
    else
        touch $PKGREF-deleted
    fi
    if [ -e "$LOCF" ]; then
        cp $LOCF $LOCREF
    else
        touch $LOCREF-deleted
    fi
    if [ -e "$PKGRF" ]; then
        cp $PKGRF $PKGRREF
    else
        touch $PKGRREF-deleted
    fi
    if [ -e "$LOCRF" ]; then
        cp $LOCRF $LOCRREF
    else
        touch $LOCRREF-deleted
    fi
}

check_ucfq_number() {
    EXPECTED="$1"
    if [ $(ucfq --with-colons $PKGNAME | wc -l) != "$EXPECTED" ]; then
        echo  >&2 "number of files registered to $PKGNAME not equal $EXPECTED, test failed"
        ucfq --with-colons $PKGNAME | wc -l
        ucfq $PKGNAME
        return 1
    else
        echo  >&2 "$EXPECTED file(s) registered to $PKGNAME, test passed"
        return 0
    fi
}


is_package() {
    local LOCF
    local REFF
    LOCF=$1
    REFF=${2:-$PKGREF}
    if ! cmp $LOCF $REFF; then
        echo  >&2 "$LOCF not equal $REFF, test failed"
        return 1
    else
        echo  >&2 "$LOCF equals $REFF, test passed"
        return 0
    fi
}

is_local() {
    local LOCF
    local REFF
    LOCF=$1
    REFF="${2:-$LOCREF}"
    if ! cmp $LOCF $REFF; then
        echo  >&2 "$LOCF not equal $REFF, test failed"
        return 1
    else
        echo  >&2 "$LOCF equals $REFF, test passed"
        return 0
    fi
}

is_deleted() {
    RET=0
    FN="$1"
    if [ -e "$FN" ]; then
        echo  >&2 "$FN does still exist, test failed"
        RET=1
    else
        echo  >&2 "$FN is deleted, test passed"
    fi
    return $RET
}

print_state() {
    printf -- "------- %s\n" "${1:-}"
    echo "ls -alR $DIR"
    ls -alR $DIR
    head -n-0 /dev/null $DIR/*/*
    printf "ucfq $PKGNAME knows about %d files\n" "$(ucfq --with-colons "$PKGNAME" | wc -l)"
    ucfq --with-colons "$PKGNAME"
}

DEBIAN_FRONTEND=readline
GLOB="${1:-*}"
for test in ./test_$GLOB; do
    prepare
    RET=0
    unset UCF_FORCE_CONFFOLD
    export -n UCF_FORCE_CONFFOLOLD
    unset UCF_FORCE_CONFFNEW
    export -n UCF_FORCE_CONFFNEW
    . $test || RET=$?
    if [ "$RET" -ne 0 ]; then
        echo "$test failed"
        print_state "$test failed"
        cleanup
        exit 1
    else
        echo "$test passed"
    fi
    cleanup
done

# vim:sw=4:sts=4:et:

