#!/bin/bash
# Starts GitHub's device login, floats its code over Android's browser and opens the approval page.
url=https://github.com/login/device
inbox=${DD_AGENT_INBOX:-}

# DroidDeck takes $inbox/device-code within a few seconds when it can float the code.
float_code() {
  [ -n "$inbox" ] && [ -d "$inbox" ] || return 1
  rm -f "$inbox/device-code.result"
  printf '%s %s\n' "$1" "$url" > "$inbox/device-code.part" && mv -f "$inbox/device-code.part" "$inbox/device-code" || return 1
  i=0
  while [ $i -lt 40 ] && [ -e "$inbox/device-code" ]; do
    sleep 0.2
    i=$((i + 1))
  done
  if [ -e "$inbox/device-code" ]; then
    rm -f "$inbox/device-code"
    return 1
  fi
  # DroidDeck writes "ok" or "failed" once it has tried to open the page; no answer counts as failed.
  i=0
  while [ $i -lt 50 ] && [ ! -e "$inbox/device-code.result" ]; do
    sleep 0.2
    i=$((i + 1))
  done
  result=$(cat "$inbox/device-code.result" 2>/dev/null)
  rm -f "$inbox/device-code.result"
  [ "$result" = ok ]
}

# git pushes through gh's sign-in and commits as the signed-in account, with its no-reply address.
configure_git() {
  gh auth setup-git || return 1
  [ -n "$(git config --global user.name)" ] && [ -n "$(git config --global user.email)" ] && return 0
  local user id login name
  user=$(gh api user -q '"\(.id) \(.login) \(.name // .login)"') || return 1
  read -r id login name <<<"$user"
  [ -n "$id" ] && [ -n "$login" ] || return 1
  [ -n "$(git config --global user.name)" ] || git config --global user.name "${name:-$login}"
  [ -n "$(git config --global user.email)" ] || git config --global user.email "$id+$login@users.noreply.github.com"
}
if [ "${1:-}" = --git ]; then configure_git; exit; fi

coproc GH { BROWSER=true gh auth login --hostname github.com --git-protocol https --web 2>&1; }
gh_pid=$GH_PID
exec {ghout}<&"${GH[0]}" {ghin}>&"${GH[1]}"
shown=
while IFS= read -r line <&"$ghout"; do
  code=$(grep -oE '[A-Z0-9]{4}-[A-Z0-9]{4}' <<<"$line" | head -1)
  # gh's own prompts would push the code out of the one line the setup card shows.
  case "$line" in
    *"one-time code"* | *"Press Enter"* | *"Open this URL"*) ;;
    *) printf '%s\n' "$line" ;;
  esac
  if [ -n "$code" ] && [ -z "$shown" ]; then
    shown=1
    echo >&"$ghin"
    printf 'GitHub device code: %s · github.com/login/device\n' "$code"
    float_code "$code" || android-open "$url"
  fi
done
wait "$gh_pid"
status=$?
if [ $status = 0 ] && ! configure_git; then
  echo "Signed in, but git could not be set up: run dd-gh-login --git"
  status=1
fi
[ -n "$shown" ] && [ -n "$inbox" ] && [ -d "$inbox" ] && : > "$inbox/device-code"
exit $status
