#! /bin/sh
# Runs Fes on all projects in test/

scriptversion="1"

#
# 
# Copyright (C) 2025-2026 fSD
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

me=$0
version="$me/fSD v$scriptversion

Copyright (C) 2025-2026 fSD.
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law."

usage="Usage: $me [OPTION]... <arg1> <arg2>
Runs Fes on all projects in test/

Options:
   --help         print this help and exit
   --version      output version information"

say() {
    if [ -z "$q" ]; then
        echo "$me: $*"
    fi
}

lsay() {
    if [ -z "$q" ]; then
        echo " => $*"
    fi
}

check_dep() {
	printf 'checking for %s... ' "$1"
	if command -v "$1" > /dev/null; then
		echo "yes"
	else
	    echo "no"
	    exit 1
	fi
}

while [ $# -gt 0 ]; do
  case $1 in
    --help) echo "$usage"; exit 0 ;;
    --version) echo "$version"; exit 0 ;;
    -*) echo "$me: Unknown option '$1'." >&2; exit 1 ;;
  esac
done

if [ ! -d "modules" ]; then
    echo "$me: error: must be run in project root." >&2
    exit 1
fi

if [ ! -f "fes" ]; then
    echo "$me: error: run 'make'." >&2
    exit 1
fi

for dir in test/*; do
    ./fes run "$dir" >/dev/null 2>&1 &
    pid=$!

    printf "test '%s'" "$dir"

    printf "\nPress [Enter] to move to start the next..."
    read -r unused

    if kill -0 $pid 2>/dev/null; then
        kill $pid
    fi
done

echo "done"
kill $$

# BUGS
#
# doesn't kill the last test
