#!/bin/sh
export LANG=C.UTF-8
export LANGUAGE=en
set -eu

if which goctest >/dev/null; then
    goctest="goctest"
else
    goctest="go test"
fi

STATIC=""
UNIT=""
INTEGRATION=""

case "${1:-all}" in
    all)
        STATIC="yes"
        UNIT="yes"
        INTEGRATION="yes"
        ;;
    --static)
        STATIC="yes"
        ;;
    --unit)
        UNIT="yes"
        ;;
    --integration)
        INTEGRATION="yes"
        ;;
    *)
        echo "Wrong flag ${1}. To run a single suite use --static, --unit or --integration."
        exit 1
esac

CURRENTTRAP="true"
EXIT_CODE=99
store_exit_code() {
    EXIT_CODE=$?
}
exit_with_exit_code() {
    exit $EXIT_CODE
}
addtrap() {
    CURRENTTRAP="$CURRENTTRAP ; $1"
    trap "store_exit_code; $CURRENTTRAP ; exit_with_exit_code" EXIT
}

endmsg() {
    if [ $EXIT_CODE -eq 0 ]; then
        p="success.txt"
        m="All good, what could possibly go wrong."
    else
        p="failure.txt"
        m="Crushing failure and despair."
    fi
    echo
    if [ -t 1 -a -z "$STATIC" ]; then
        cat "data/$p"
    else
        echo "$m"
    fi
}
addtrap endmsg

# Append the coverage profile of a package to the project coverage.
append_coverage() {
    local profile="$1"
    if [ -f $profile ]; then
        cat $profile | grep -v "mode: set" >> .coverage/coverage.out
        rm $profile
    fi
}

sh ./get-deps.sh

if [ ! -z "$STATIC" ]; then
    # Run static tests.
    echo Checking docs
    ./mdlint.py docs/*.md

    echo Checking formatting
    fmt=$(gofmt -l .)

    if [ -n "$fmt" ]; then
        echo "Formatting wrong in following files"
        echo "$fmt"
        exit 1
    fi

    # go vet
    echo Running vet
    go vet ./...

    # golint
    echo Install golint
    go get github.com/golang/lint/golint
    export PATH=$PATH:$GOPATH/bin

    echo Running lint
    lint=$(golint ./...)
    if [ -n "$lint" ]; then
        echo "Lint complains:"
        echo "$lint"
        # don't exit 1
    fi

    # pot file
    echo Checking translations
    TMPF="$(mktemp)"
    addtrap "rm -f \"$TMPF\""
    ./update-pot "$TMPF"
    if ! diff -u --ignore-matching-lines=.*POT-Creation-Date.* po/snappy.pot $TMPF; then
        echo "You need to run ./update-pot"
        exit 1
    fi
fi

if [ ! -z "$UNIT" ]; then
    # Prepare the coverage output profile.
    rm -rf .coverage
    mkdir .coverage
    echo "mode: set" > .coverage/coverage.out

    echo Building
    go build -tags=excludeintegration -v github.com/ubuntu-core/snappy/...

    # tests
    echo Running tests from $(pwd)
    for pkg in $(go list ./... | grep -v integration-tests); do
        $goctest -tags=excludeintegration -v -coverprofile=.coverage/profile.out $pkg
        append_coverage .coverage/profile.out
    done

    echo Building the integration tests
    TMP_INTEGRATION="$(mktemp)"
    addtrap "rm -f \"$TMP_INTEGRATION\""
    go build -v -o $TMP_INTEGRATION github.com/ubuntu-core/snappy/integration-tests/

    # the rabbit hole
    echo Running the tests for the integration testutils
    for pkg in $(go list ./integration-tests/testutils/...); do
        $goctest -v -coverprofile=.coverage/profile.out $pkg
        append_coverage .coverage/profile.out
    done
fi

if [ ! -z "$INTEGRATION" ]; then
    # integration suite in kvm
    if which adt-run >/dev/null 2>&1; then
        echo "Running integration tests on rolling edge"
        result=0
        go run integration-tests/main.go --snappy-from-branch || result=$?
        # print the results.
        if which subunit2pyunit >/dev/null 2>&1; then
            subunit2pyunit /tmp/snappy-test/output/artifacts/results.subunit
        fi
        if [ "$result" -ne 0 ]; then
            exit $result
        fi
    fi
fi

UNCLEAN="$(git status -s|grep ^??)" || true
if [ -n "$UNCLEAN" ]; then
    cat <<EOF

There are files left in the git tree after the tests:

$UNCLEAN
EOF
    exit 1
fi
