#!/usr/bin/env bash

#set -x

usage() {
    cat <<EOT
Usage: git f [COMMAND]

Available commands:

  workon   Switch to a branch for the given issue
  help     Shows this message

EOT
}

t_workon() {
    local branch

    if [[ $1 == "" ]]; then
        cat <<EOT
ERROR: No issue given

Usage: git f workon [ISSUE]
EOT
        return 1
    fi

    git fetch -p
    branch=$(git branch -r | grep "$1" | cut -d'/' -f2)

    if [[ $branch == "" ]]; then
        echo ERROR: no branch found for this issue
        return 2
    fi

    git checkout "$branch"
}

ACTION=$1
shift

case $ACTION in
    workon)
        t_workon "$1"
        ;;
    *)
        usage
        ;;
esac
