#!/bin/sh

set -eu

libexecdir="${UNIXBENCH_LIBEXECDIR:-@LIBEXECDIR@/unixbench}"
package_version="@PACKAGE_VERSION@"
case "${XDG_STATE_HOME:-}" in
    /*) state_home=$XDG_STATE_HOME ;;
    *) state_home="${HOME:?HOME is not set}/.local/state" ;;
esac
workdir="${UNIXBENCH_WORKDIR:-${state_home}/unixbench}"
prepare_only=no
print_workdir=no

usage()
{
    cat <<EOF
Usage: unixbench [OPTION]... [TEST]...
       unixbench prepare
       unixbench --print-workdir

Run the packaged UnixBench suite with per-user results and temporary files.

Commands:
  prepare          prepare the per-user workspace without running tests

Wrapper options:
  --print-workdir  prepare and print the workspace path
  -h, --help       show this help

All other options and test names are passed to the upstream UnixBench Run
script. With no arguments, UnixBench runs the standard index benchmark.
EOF
}

die()
{
    echo "unixbench: $*" >&2
    exit 1
}

case "$workdir" in
    /*) ;;
    *) die "UNIXBENCH_WORKDIR must be an absolute path" ;;
esac

case "${1:-}" in
    -h|--help)
        usage
        exit 0
        ;;
    prepare)
        prepare_only=yes
        shift
        [ "$#" -eq 0 ] || die "prepare does not accept arguments"
        ;;
    --print-workdir)
        print_workdir=yes
        shift
        [ "$#" -eq 0 ] || die "--print-workdir does not accept arguments"
        ;;
    *)
        ;;
esac

[ -x "$libexecdir/Run" ] || die "packaged runner not found in $libexecdir"
[ -d "$libexecdir/pgms" ] || die "packaged benchmark programs not found"
[ -d "$libexecdir/testdir" ] || die "packaged test data not found"

umask 077
marker="$workdir/.unixbench-workspace"
if [ ! -e "$workdir" ] && [ ! -L "$workdir" ]; then
    mkdir -p -- "$workdir"
    printf '%s\n' "$package_version" > "$marker"
fi
[ -d "$workdir" ] && [ ! -L "$workdir" ] && [ -f "$marker" ] ||
    die "refusing to use an unmanaged workspace: $workdir"
workdir=$(cd -- "$workdir" && pwd -P)

for dir in results testdir; do
    [ ! -L "$workdir/$dir" ] ||
        die "unexpected symbolic link in workspace: $workdir/$dir"
    mkdir -p -- "$workdir/$dir"
done

for file in "$libexecdir"/testdir/*; do
    target="$workdir/testdir/${file##*/}"
    if [ -e "$target" ] && [ ! -L "$target" ]; then
        die "unexpected file in workspace: $target"
    fi
    ln -sfn -- "$file" "$target"
done

if [ -z "${UB_TMPDIR:-}" ]; then
    UB_TMPDIR="/var/tmp/unixbench-$(id -u)"
    mkdir -p -- "$UB_TMPDIR"
    [ -d "$UB_TMPDIR" ] && [ ! -L "$UB_TMPDIR" ] && [ -O "$UB_TMPDIR" ] ||
        die "$UB_TMPDIR is not owned by the current user; set UB_TMPDIR to a private directory"
    chmod 0700 "$UB_TMPDIR"
fi

UB_BINDIR="${UB_BINDIR:-$libexecdir/pgms}"
UB_TESTDIR="${UB_TESTDIR:-$workdir/testdir}"
UB_RESULTDIR="${UB_RESULTDIR:-$workdir/results}"
UB_SKIP_BUILD=1
export UB_BINDIR UB_TESTDIR UB_RESULTDIR UB_TMPDIR UB_SKIP_BUILD

if [ "$print_workdir" = yes ]; then
    printf '%s\n' "$workdir"
    exit 0
fi

if [ "$prepare_only" = yes ]; then
    printf 'UnixBench workspace prepared at %s\n' "$workdir"
    printf 'Results: %s\n' "$UB_RESULTDIR"
    printf 'File I/O temporary directory: %s\n' "$UB_TMPDIR"
    exit 0
fi

exec "$libexecdir/Run" "$@"
