#!/bin/bash
. $GVM_ROOT/scripts/function/tools

function display_error() {
	if [ command -v tput >/dev/null 2>&1 ]; then
		tput sgr0
		tput setaf 1
		echo "ERROR: $1" >&2
		tput sgr0
	else
		echo "ERROR: $1" >&2
	fi
	return 1
}

function display_message() {
	if [ command -v tput >/dev/null 2>&1 ]; then
		# GREEN!
		tput sgr0
		tput setaf 2
		echo "$1"
		tput sgr0
	else
		echo "$1"
	fi
}

function gvm_use() {
	[[ "$1" != "" ]] || 
		display_error "Please specifiy the version" || return 1
	fuzzy_match=`$LS_PATH $GVM_ROOT/gos | $SORT_PATH | $GREP_PATH "$1" | $HEAD_PATH -n 1 | $GREP_PATH "$1"` ||
		display_error "Invalid version $1" || return 1

	export PATH=$GVM_PATH_BACKUP
	. $GVM_ROOT/environments/$fuzzy_match &> /dev/null || display_error "Couldn't source environment" || return 1
	if [[ "$2" == "--default" ]]; then
		cp $GVM_ROOT/environments/$fuzzy_match $GVM_ROOT/environments/default || display_error "Couldn't make $fuzzy_match default"
	fi

	display_message "Now using version $fuzzy_match"
}

