#!/bin/sh

if [ -z "$1" ]; then
    echo "Please specific path to click package"
    exit 1
fi
c="$1"
if [ ! -f "$c" ]; then
    echo "Could not find '$c'"
    exit 1
fi

# prefer local script for testing, otherwise use system location
prefer_local() {
    path="$(dirname $(readlink -f "$0"))/$1"
    if [ -x $path ]; then
        "$path" "$2" || set_rc "$?"
        return
    fi
    "$(which $1)" "$2" || set_rc "$?"
}

rc="0"
set_rc() {
    # return worst offending rc
    if [ "$1" = "1" ]; then
        if [ "$rc" != "2" ]; then
            rc="$1"
        fi
    elif [ "$1" = "2" ]; then
        rc="$1"
    fi

}

prefer_local click-show-files "$c"

echo ""
echo "= click-check-lint ="
prefer_local click-check-lint "$c"

echo ""
echo "= click-check-desktop ="
prefer_local click-check-desktop "$c"

echo ""
echo "= click-check-security ="
prefer_local click-check-security "$c"

echo ""
echo "= click-check-functional ="
prefer_local click-check-functional "$c"

echo ""
echo ""
if [ "$rc" = "1" ]; then
    echo "** Warnings found **"
elif [ "$rc" = "2" ]; then
    echo "** Errors found **"
fi

echo -n "$c: "
if [ "$rc" = "0" ]; then
    echo "pass"
else
    echo "FAIL"
fi

exit $rc
