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

function display_error() {
	tput sgr0
	tput setaf 1
	echo "ERROR: $1" >&2
	tput sgr0
	return 1
}

function display_message() {
	# GREEN!
	tput sgr0
	tput setaf 2
	echo "$1"
	tput sgr0
}

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"
}

