#!/usr/bin/env bash

set -uexo pipefail

usage() { echo "Usage: [PDB_JAR=JAR] $(basename "$0")"; }
misuse() { usage 1>&2; exit 2; }

test $# -eq 0 || misuse

jdkver=$(ext/bin/jdk-info --print major)

expected_help_warnings=0
expected_version_warnings=0
case "$jdkver" in
    8)
        ;;
    10)
        expected_help_warnings=0
        expected_version_warnings=535
        ;;
    *)
        echo "JDK version '$jdkver' is not supported" 1>&2
        exit 3
        ;;
esac

tmpdir="$(mktemp -d "test-top-level-cli-XXXXXX")"
tmpdir="$(cd "$tmpdir" && pwd)"
trap "$(printf 'rm -rf %q' "$tmpdir")" EXIT

rc=0
./pdb frobnicate ... 1>"$tmpdir/out" 2>"$tmpdir/err" || rc=$?
cat "$tmpdir/out" "$tmpdir/err"
test "$rc" -eq 1
grep -F 'Available subcommands:' "$tmpdir/err"
grep -E 'Display version information' "$tmpdir/err"

rc=0
./pdb help 1>"$tmpdir/out" 2>"$tmpdir/err" || rc=$?
cat "$tmpdir/out" "$tmpdir/err"
test "$rc" -eq 0
grep -F 'Available subcommands:' "$tmpdir/out"
grep -E 'Display version information' "$tmpdir/out"
# FIXME: this should and will be 0 once we fix the pos-int?, dynapath,
# etc. replacement warnings.
test $(wc -c < "$tmpdir/err") -eq $expected_help_warnings

rc=0
./pdb version 1>"$tmpdir/out" 2>"$tmpdir/err" || rc=$?
cat "$tmpdir/out" "$tmpdir/err"
test "$rc" -eq 0
grep -E '^version=' "$tmpdir/out"
grep -E '^target_schema_version=' "$tmpdir/out"
# FIXME: this should and will be 0 once we fix the pos-int?, dynapath,
# etc. replacement warnings.
test $(wc -c < "$tmpdir/err") -eq $expected_version_warnings
