#!/usr/bin/env bash
# nerf-gh-pr-create -- Create a pull request from the current branch. Pushes the branch if needed.
# Generated from gh manifest. Do not edit directly.
# nerf:threat:read=remote
# nerf:threat:write=remote

set -euo pipefail

_NERF_DRY_RUN=""

usage() {
  cat >&2 <<'EOF'
Usage: nerf-gh-pr-create [--draft|-d] [--body|-b <body>] [--base|-B <base>] [--reviewer|-r <reviewer>] <title>

Switches:
  --draft, -d
      Create as a draft PR

Options:
  --body, -b <body>
      PR body/description
  --base, -B <base>
      Base branch (default is the repo default branch)
  --reviewer, -r <reviewer>
      Request review from this user

Arguments:
  <title> (required)
      PR title

Maps to: gh pr create <draft> --title <title> <body> <base> <reviewer>

Create a pull request from the current branch. Pushes the branch if needed.
EOF
  exit 1
}

DRAFT=""
BODY=""
_BODY_SET=""
BASE=""
_BASE_SET=""
REVIEWER=""
_REVIEWER_SET=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --draft|-d) if [[ -n "${DRAFT}" ]]; then echo "error: --draft can only be specified once" >&2; exit 1; fi; DRAFT="true"; shift 1 ;;
    --body|-b) if [[ -n "${_BODY_SET}" ]]; then echo "error: --body can only be specified once" >&2; exit 1; fi; BODY="$2"; _BODY_SET=true; shift 2 ;;
    --base|-B) if [[ -n "${_BASE_SET}" ]]; then echo "error: --base can only be specified once" >&2; exit 1; fi; BASE="$2"; _BASE_SET=true; shift 2 ;;
    --reviewer|-r) if [[ -n "${_REVIEWER_SET}" ]]; then echo "error: --reviewer can only be specified once" >&2; exit 1; fi; REVIEWER="$2"; _REVIEWER_SET=true; shift 2 ;;
    --nerf-dry-run) _NERF_DRY_RUN="true"; shift 1 ;;
    -h|--help) usage ;;
    --) shift; break ;;
    *) break ;;
  esac
done

_TITLE_SET=""
if [[ $# -gt 0 ]]; then
  TITLE="$1"
  _TITLE_SET=true
  shift
else
  TITLE=""
fi
if [[ $# -gt 0 ]]; then
  echo "error: nerf-gh-pr-create: unexpected extra arguments: $*" >&2
  echo "  hint: switches and options must come before positional arguments" >&2
  exit 1
fi

if [[ -n "${_TITLE_SET}" ]] && [[ "${TITLE}" == -* ]]; then
  echo "error: nerf-gh-pr-create: <title> cannot start with '-'" >&2
  echo "  hint: use -- before positional arguments if needed" >&2
  exit 1
fi

if [[ -z "${TITLE}" ]]; then
  echo "error: nerf-gh-pr-create: missing required argument <title>" >&2
  echo "  hint: provide a value for <title>" >&2
  usage
fi

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  _NERF_DRY_CMD=(gh pr create ${DRAFT:+"--draft"} --title "${TITLE}" ${_BODY_SET:+"--body"} ${_BODY_SET:+"$BODY"} ${_BASE_SET:+"--base"} ${_BASE_SET:+"$BASE"} ${_REVIEWER_SET:+"--reviewer"} ${_REVIEWER_SET:+"$REVIEWER"})
  printf 'dry-run:'
  for _a in "${_NERF_DRY_CMD[@]}"; do printf " %q" "$_a"; done
  echo
  exit 0
fi

exec gh pr create ${DRAFT:+"--draft"} --title "${TITLE}" ${_BODY_SET:+"--body"} ${_BODY_SET:+"$BODY"} ${_BASE_SET:+"--base"} ${_BASE_SET:+"$BASE"} ${_REVIEWER_SET:+"--reviewer"} ${_REVIEWER_SET:+"$REVIEWER"}
