#!/usr/bin/env bash
#
# Open or create a GitHub PR in the browser

export GH_NO_UPDATE_NOTIFIER=1
unset BROWSER

currentbranch="$(git bn)"
if [ "$currentbranch" = "staging" ] || [ "$currentbranch" = "master" ]; then
  echo "Are you sure? Currently on branch '$currentbranch'"
  exit 1
fi

output=$(gh pr view -w 2>&1)
exitcode=$?

if [ $exitcode -eq 0 ]; then
  exit 0
fi

if echo "$output" | grep -q "no pull requests found"; then
  echo "Opening new PR..."
  gh pr create -w
else
  echo "$output"
  exit $exitcode
fi