#!/usr/bin/env bash

set -e

usage () {
  printf "usage: $(basename $0) <owner>[/]<repo>\n" >&2
  exit 1
}

already_exists () {
  printf "%s\n" "$2"
  exit 2
}

root=${GITHUB_ROOT:-"$HOME/src"}
owner="$1"
repo="$2"

[ -z "$owner" ] && usage

if [ -z "$repo" ]; then
  IFS='/' read -ra parsed <<< "$owner"
  owner="${parsed[0]}"
  repo="${parsed[1]}"
  if [ -z "$owner" ] || [ -z "$repo" ]; then
    usage
  fi
fi

dest="$root/$owner"
dir="$dest/$repo"

[ -d "$dir" ] && already_exists "$owner/$repo" "$dir"

mkdir -p "$dest"
cd "$dest"
git clone "git@github.com:$owner/$repo"

git config \
    --file="$dir/.git/config" \
    --add remote.origin.fetch '+refs/pull/*/head:refs/pull/origin/*'

git config \
    --file="$dir/.git/config" \
    --add remote.origin.fetch '+refs/pull/*/merge:refs/merge/origin/*'

printf "%s\n" "$dest/$repo"