#!/bin/sh

set -e

HELP_MSG="Script argument must be one of: no-sudo, sudo"

if [ "$#" -ne 1 ]; then
    echo "$HELP_MSG"
    exit 1
fi

case $1 in
    no-sudo)
        echo "Running non-root tests..."
        export ADSYS_SKIP_INTEGRATION_TESTS=1
        PACKAGES_TO_TEST=./...
        ;;
    sudo)
        arch=$(dpkg --print-architecture)
        if [ "$arch" != "amd64" ] && [ "$arch" != "arm64" ]; then
            echo "Skipping root tests for non-amd64/arm64 architecture"
            exit 77
        fi

        echo "Running root tests..."
        DIR_NAME=$(dirname "$0")
        PACKAGES_TO_TEST=$(cat "$DIR_NAME"/.sudo-packages)
        ;;
    *)
        echo "$HELP_MSG"
        exit 1
        ;;
esac

# shellcheck disable=SC2086 # Splitting is intentional
/usr/lib/go-1.22/bin/go test -v -mod=vendor $PACKAGES_TO_TEST
