#!/usr/bin/env bash
# service-account.sh - the shell front door to pipeline/lib/service-account.mjs.
#
# Shell callers need the same answers Node callers get, and the one way to be
# sure they do is for there to be one implementation. This script resolves the
# module beside it and hands the arguments straight through; the exit status
# and both streams are the module's own.
#
# Usage:
#   service-account.sh select      --service S [--account ID] [--url U|--host H]
#                                  [--environment E] [--project-id P] [--owner O]
#                                  [--identity I] [--item NAME]
#   service-account.sh token       --service S (--url U|--host H) [--slot SLOT] [selectors]
#   service-account.sh curl-config --service S --url U [--scheme SCHEME] [selectors]
#   service-account.sh item-ref    --service appstore_connect --slot password_item [--account ID]
#   service-account.sh items       [--service S]
#   service-account.sh list        [--service S]
#   service-account.sh check
#
# Schemes: bearer (default), basic, basic-token-user, basic-or-bearer,
# figma-pat, fortify-token, graylog-pat.
#
# stdout: `token` the raw secret with no trailing newline, `curl-config` a curl
# config, everything else one JSON line. On a refusal stdout is empty and one
# JSON line goes to stderr.
set -euo pipefail

HERE="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"

if ! command -v node >/dev/null 2>&1; then
  printf '%s\n' '{"ok":false,"reason":"invalid-request","message":"node is not on PATH.","remedy":"Install Node, which the rest of the pipeline already needs."}' >&2
  exit 3
fi

exec node "$HERE/service-account.mjs" "$@"
