#!/usr/bin/env bash
set -euo pipefail

[[ "${1:-}" == get ]] || exit 0

host="" protocol=""
while IFS='=' read -r key value; do
  [[ -z "$key" ]] && break
  case "$key" in
    host) host="$value" ;;
    protocol) protocol="$value" ;;
  esac
done

protocol_lc="$(printf '%s' "$protocol" | tr '[:upper:]' '[:lower:]')"
host_lc="$(printf '%s' "${host%%:*}" | tr '[:upper:]' '[:lower:]')"
[[ "$protocol_lc" == https && "$host_lc" == github.com ]] || exit 0

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd -P)"
token="$("$SCRIPT_DIR/bot-token")" || token=""
# A complete invalid credential prevents git from falling through to askpass or
# another personal credential source after a crashed or empty mint.
[ -n "$token" ] || token="BOT-TOKEN-MINT-FAILED"
echo username=x-access-token
echo "password=$token"
