#!/usr/bin/env bash

set -uexo pipefail

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

test $# -eq 0 || misuse

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


## Test handling an unknown option
rc=0
./tk --invalid-option 1>"$tmpdir/out" 2>"$tmpdir/err" || rc=$?
cat "$tmpdir/out" "$tmpdir/err"
test "$rc" -eq 1
grep -F 'Unknown option: "--invalid-option"' "$tmpdir/err"


## Test --help
rc=0
./tk --help  1>"$tmpdir/out" 2>"$tmpdir/err" || rc=$?
cat "$tmpdir/out" "$tmpdir/err"
test "$rc" -eq 0
grep -F 'Path to bootstrap config file' "$tmpdir/out"
test $(grep -c -F 'Path to bootstrap config file' "$tmpdir/out") -eq 1
test $(wc -c < "$tmpdir/out") -eq 650


## Test handling a missing bootstrap file
rc=0
./tk frobnicate ... 1>"$tmpdir/out" 2>"$tmpdir/err" || rc=$?
cat "$tmpdir/out" "$tmpdir/err"
test "$rc" -eq 1
grep -F 'Unable to find bootstrap.cfg file via --bootstrap-config' "$tmpdir/err"
