#!/usr/bin/env bash

[ -n "$GVM_DEBUG" ] && {
  set -x
}

if [ -z "$GVM_ROOT" ]; then
	tput sgr0
	tput setaf 1
	echo 'ERROR: GVM_ROOT is not set! Please add the following line to your .bashrc or .profile

	[[ -s "$HOME/.gvm/scripts/gvm" ]] && source "$HOME/.gvm/scripts/gvm"
' >&2
	tput sgr0
	exit 1
fi

. "$GVM_ROOT/scripts/functions" || exit 1

command=$1
if [[ $command == "implode" ]]; then
  gvm_implode
	exit 0
fi

"$GVM_ROOT/scripts/gvm-check"
if [[ "$?" != "0" ]]; then
	display_fatal "Missing requirements."
fi

if [[ $command == "version" ]]; then
	display_message "Go Version Manager v$GVM_VERSION installed at $GVM_ROOT"
else
	if [ -f "$GVM_ROOT/scripts/$command" ]; then
		shift
		"$GVM_ROOT/scripts/$command" "$@"
	elif [[ -n $command ]]; then
		display_fatal "Unrecognized command line argument: '$command'"
	else
		echo "Usage: gvm [command]

Description:
  GVM is the Go Version Manager

Commands:
  version    - print the gvm version number
  get        - gets the latest code (for debugging)
  use        - select a go version to use
  diff       - view changes to Go root
  implode    - completely remove gvm
  install    - install go versions
  uninstall  - uninstall go versions
  cross      - install go cross compilers
  linkthis   - link this directory into GOPATH
  list       - list installed go versions
  listall    - list available versions
  alias      - manage go version aliases
  pkgset     - manage go packages sets
  pkgenv     - edit the environment for a package set
"
	fi
fi
