#! /bin/sh

set -e
set -u

if ! test -f README.md
then
    echo "run from the base directory." >&2
    exit 1
fi

if test -d ./.tmsu/
then
    rm -r ./.tmsu/
fi

tmsu init

tmsu tag -c arch-x86_64 portable \
  needs-cpp needs-libc
tmsu imply c89 c99
tmsu imply c99 c11
tmsu imply needs-libc needs-cpp


for suite in single-exec
do
    for t in ./tests/"$suite"/*.c
    do
        tmsu tag "$t" "suite=$suite"

        while read -r tagline
        do
            if test -z "$tagline"
            then
                continue
            fi
            tmsu tag --tags="$tagline" "$t"
        done < "$t.tags"
    done
done


# Validation

q="(not c89) and (not c99) and (not c11)"
n=$(tmsu files "$q" | wc -l)
if test "$n" != "0"
then
    echo "Tests without a specified standard, aborting:"
    tmsu files "$q"
    exit 1
fi

q="not portable and not arch-x86_64"
n=$(tmsu files "$q" | wc -l)
if test "$n" != "0"
then
    echo "Tests without a specified arch, aborting:"
    tmsu files "$q"
    exit 1
fi
