#!/usr/bin/env sh

# Support bash to support `source` with fallback on $0 if this does not run with bash
# https://stackoverflow.com/a/35006505/6512
selfArg="$BASH_SOURCE"
if [ -z "$selfArg" ]; then
    selfArg="$0"
fi

self=$(realpath $selfArg 2> /dev/null)
if [ -z "$self" ]; then
    self="$selfArg"
fi

dir=$(cd "${self%[/\\]*}" > /dev/null; cd '../scripts' && pwd)

usage() {
  echo -n "${scriptName} [OPTION]... [FILE]...

Returns 'branch' if local working copy is a branch checkout.

Returns 'tag' if a tag is checked out.

"
}

BRANCH=`git symbolic-ref --quiet --short HEAD 2> /dev/null`
TAG=`git describe --tags --exact-match 2> /dev/null`

# Set up options for --type and --help.
while [ $# -gt 0 ]; do
  case "$1" in
    -t|--type)
        if [[ -n $BRANCH ]]; then
          echo "branch"
        elif [[ -n $TAG ]]; then
          echo "tag"
        else
          exit 1
        fi

        exit 0
      ;;
    -h|--help) usage >&2; exit 0;;
    *)
      echo "Invalid option: $1"
      exit 1
  esac
  shift
done

# Print the branch or tag name
if [ -n "$BRANCH" ]; then
  echo 'branch'
elif [ -n "$TAG" ]; then
  echo 'tag'
else
  git remote -v > /dev/null 2>&1
  retVal=$?
  if [ $retVal -ne 0 ]; then
      echo "Not a git repo"
      exit 1
  fi
  echo 'sha'
fi

