#!/usr/bin/env bash

# Usage Instructions:

# 1. Add the following with your own values to .bash_profile
ORCH_GITHUB_USER=my_github_handle
GITHUB_ACCESS_TOKEN=access_token_value_here

# 2. Copy the functions below into your .bash_profile and refresh any open terminals.

# 3. Use the functions to fork orchard repos (orchfork), clone the fork (orchfork), and rebase your fork onto master (orchup)

# 4. CLI commands - replace "reponame" with the repo you're interested in
# > orchfork reponame # Creates a fork from the orchard repo "reponame"
# > orchclone reponame # Clones the repo locally, cd's into it, and disables pushing to the orchard repo
# > orchup # Checksout master, rebases it onto orchard/master, and pushes the changes to your fork

# fork and clone
function orchfork() {
    curl -u "${ORCH_GITHUB_USER}":"${GITHUB_ACCESS_TOKEN}" "https://api.github.com/repos/theorchard/${1}/forks" -d '' -H "Accept: application/vnd.github.v4.full+json"
    curl --include -u "${ORCH_GITHUB_USER}":"${GITHUB_ACCESS_TOKEN}" "https://api.github.com/repos/${ORCH_GITHUB_USER}/${1}" -H "Accept: application/vnd.github.v4.full+json"
}

# check out
function orchclone() {
    git clone git@github.com:"${ORCH_GITHUB_USER}"/"${1}".git
    cd "${1}"/ || exit 1
    git remote add theorchard git@github.com:theorchard/"${1}".git
    git remote set-url --push theorchard DISABLE
    orchup
}

# update and rebase
function orchup () {
    git checkout master
    git fetch -p
    git fetch theorchard -p
    git rebase theorchard/master
    git push
}
