#!/usr/bin/env bash

set -eu

## Functions/
usage() {
cat << EOF
SYNOPSIS

    gpm leverages the power of the go get command and the underlying version
    control systems used by it to set your Go dependencies to desired versions,
    thus allowing easily reproducible builds in your Go projects.

    A Godeps file in the root of your Go application is expected containing
    the import paths of your packages and a specific tag or commit hash
    from its version control system, an example Godeps file looks like this:

    $ cat Godeps
    # This is a comment
    github.com/nu7hatch/gotrail         v0.0.2
    github.com/replicon/fast-archiver   v1.02   #This is another comment!
    github.com/nu7hatch/gotrail         2eb79d1f03ab24bacbc32b15b75769880629a865

    gpm has a companion tool, called [gvp](https://github.com/pote/gvp) which
    provides vendoring functionalities, it alters your GOPATH so every project
    has its own isolated dependency directory, its usage is recommended.

USAGE
      $ gpm             # Same as 'gpm get'.
      $ gpm get         # Parses the Godeps file, gets dependencies and sets them
                        # to the appropriate version but does not install them.
      $ gpm install     # Parses the Godeps file, installs dependencies, sets
                        # them to the appropriate version and precompiles/installs them.
      $ gpm version     # Outputs version information
      $ gpm help        # Prints this message
EOF
}

is_in_use() {
  [[ -e "$1/.git/index.lock" || -e "$1/.hg/store/lock"  || -e "$1/.bzr/checkout/lock" ]]
}

# Exit with an error message to stderr. An optional second argument sets the
# exit status (defaults to `1`).
abort() {
  echo "$1" >&2;
  exit ${2:-1};
}

# Download dependencies and set their current versions to those specified in
# the Godeps file (the path to which should be passed as the first argument).
get_dependencies() {
  (command -v go >/dev/null) ||
    abort ">> Go is currently not installed or in your PATH"
  [ -z "${GOPATH:-}" ] && abort ">> GOPATH is not set"

  [[ -r "$1" ]] || abort ">> $1 file does not exist."
  local deps=$(sed 's/#.*//;/^\s*$/d' < "$1") || echo ""

  while read package _; do
    (
      echo ">> Getting package "$package"" >&3
      go get -u -d "$package"
    ) &
  done < <(echo "$deps")
  wait

  while read package version; do
    (
      local pkg_path=$(echo "$package" | awk -F/ '{print $1"/"$2"/"$3}')
      local install_path="${GOPATH%%:*}/src/${pkg_path%%/...}"
      echo ">> Setting $package to version $version" >&3
      cd "$install_path"
      is_in_use $install_path && wait

      [ -d .bzr ] && bzr revert   -q -r   "$version"
      [ -d .git ] && git checkout -q      "$version"
      [ -d .hg  ] && hg update    -q      "$version"
      [ -d .svn ] && svn update   -r      "$version"
    ) &
  done < <(echo "$deps")
  wait

  echo "$deps"
}

# Reads dependencies from input and installs the packages.
install_dependencies() {
  while read package _; do
    echo ">> Building package "$package"" >&3
    go install "$package"
  done

  echo ">> All Done" >&3
}

## /Functions

## Command Line Parsing
case "${1:-"get"}" in
  "version")
    echo ">> gpm v1.4.0"
    ;;
  "install")
    (get_dependencies "${2:-"Godeps"}" | install_dependencies) 3>&1
    ;;
  "get")
    get_dependencies "${2:-"Godeps"}" 3>&1 >/dev/null
    ;;
  "help")
    usage
    ;;
  *)
    ## Support for Plugins: if command is unknown search for a gpm-command executable.
    if command -v "gpm-$1" > /dev/null
    then
      plugin=$1 &&
      shift     &&
      gpm-$plugin $@ &&
      exit
    else
      echo ">> No command 'gpm $1'" >&2
      usage && exit 1
    fi
    ;;
esac
