#!/usr/bin/env bash
. "$GVM_ROOT/scripts/functions"

function show_usage() {
	echo "Usage: gvm linkthis [package-name] [options]"
	echo "    -h, --help     Display this message."
	echo
	echo "If the [package-name] is provided, it will be used in the path based"
	echo "at \${GOPATH%%:*}/src, e.g.:"
	echo
	echo "    gvm linkthis github.com/moovweb/gpkg"
	echo
	echo "If omitted, the [package-name] will be the basename of the current"
	echo "directory, e.g. '$package_name_basename'."
}

function read_command_line() {
	for i in "$@"; do
		case $i in
			-h|--help*)
				show_usage
				exit 0
			;;
			-*|--*)
				echo "Invalid option $i"
				show_usage
				exit 65 # Bad arguments
			;;
			*)
				package_name="$i"
			;;
		esac
	done
}

package_name="$(basename "$PWD")"
package_name_basename="$package_name"

read_command_line "$@"

target="${GOPATH%%:*}/src/$package_name"

mkdir -p "$(dirname "$target")"
ln -sv "$PWD" "$target"
