#!/bin/bash
. $GVM_ROOT/scripts/functions

function show_usage() {
	echo "Usage: gvm install [version] [options]"
	echo "    -s, --source=SOURCE       Install Go from specified source."
	echo "    -h, --help                Display this message."
}

read_command_line() {
	VERSION=$1
	shift
	GO_SOURCE_URL=https://go.googlecode.com/hg/
	for i in $*; do
		case $i in
			-s=*|--source=*)
				GO_SOURCE_URL=$i
			;;
			-h|--help*)
				show_usage
				exit 0
			;;
			*)
				echo "Invalid option $i"
				show_usage
				exit 65 # Bad arguments
			;;
		esac
	done
}

download_source() {
	GO_CACHE_PATH=$GVM_ROOT/archive/go
	[ -d $GO_CACHE_PATH ] && return
	display_message "Downloading Go source..."
	hg clone $GO_SOURCE_URL $GO_CACHE_PATH >> $GVM_ROOT/logs/go-download.log  2>&1 ||
		display_error "Couldn't download Go source"
}

check_tag() {
	version=`hg tags -R $GO_CACHE_PATH | awk '{print $1}' | sort | grep $VERSION | head -n 1 | grep $VERSION`
}

update_source() {
	display_message "Updating Go source..."
	hg pull -R $GO_CACHE_PATH >> $GVM_ROOT/logs/go-download.log 2>&1 || 
		display_error "Couldn't get latest Go version info"
}

copy_source() {
	hg clone -u $version $GO_CACHE_PATH $GO_INSTALL_ROOT >> $GVM_ROOT/logs/go-$version-install.log 2>&1 ||
		display_error "Couldn't copy source to target folder"
}


compile_go() {
	display_message " * Compiling..."
	unset GOARCH && unset GOOS && unset GOPATH && unset GOBIN && unset GOROOT &&
	export GOBIN=$GO_INSTALL_ROOT/bin && 
	export PATH=$GOBIN:$PATH && 
	export GOROOT=$GO_INSTALL_ROOT &&
	#cd $GO_INSTALL_ROOT/src && ./all.bash &> $GVM_ROOT/logs/go-$version-compile.log || 
	cd $GO_INSTALL_ROOT/src && ./make.bash &> $GVM_ROOT/logs/go-$version-compile.log || 
		(rm -rf $GO_INSTALL_ROOT && display_error "Failed to compile")
}

create_enviroment() {
	new_env_file=$GVM_ROOT/environments/$version
	echo "export GVM_ROOT; GVM_ROOT=\"$GVM_ROOT\"" > $new_env_file
	echo "export gvm_go_name; gvm_go_name=\"$version\"" >> $new_env_file
	echo "export gvm_pkgset_name; gvm_pkgset_name=\"global\"" >> $new_env_file
	echo "export GOROOT; GOROOT=\"\$GVM_ROOT/gos/$version\"" >> $new_env_file
	echo "export GOPATH; GOPATH=\"\$GVM_ROOT/pkgsets/$version/global\"" >> $new_env_file
	echo "export PATH; PATH=\"\$GVM_ROOT/pkgsets/$version/global/bin:\$GVM_ROOT/gos/$version/bin:\$GVM_ROOT/bin:\$PATH\"" >> $new_env_file
	. $GVM_ROOT/scripts/env/use
	gvm_use $version &> /dev/null ||
		display_error "Failed to use installed version"
	gvm pkgset create global
	unset GOPATH
}

install_go() {
	GO_INSTALL_ROOT=$GVM_ROOT/gos/$version
	#trap 'rm -rf $GO_INSTALL_ROOT; display_error "Canceled!"' INT

	# Check for existing install
	if [ -d $GO_INSTALL_ROOT ]; then
		[ -f $GO_INSTALL_ROOT/manifest ] &&
			display_error "Already installed!"
		display_warning "Removing corrupt install..."
		gvm uninstall $version
	fi

	display_message "Installing $version..."

	# Create the global package set folder
	mkdir -p $GVM_ROOT/pkgsets/$version >> $GVM_ROOT/logs/go-$version-install.log 2>&1 || 
		display_error "Couldn't create global package set folder"

	copy_source
	compile_go
	create_enviroment
}

install_gpkg() {
	display_message " * Installing gpkg..."
	$GVM_GOINSTALL github.com/moovweb/gpkg > $GVM_ROOT/logs/$version-gpkg.log 2>&1
	if [[ $? -ne 0 ]]; then
		echo "Failed to install gpkg"
		return 1
	fi
}

install_gb() {
	display_message " * Installing gb..."
	$GVM_GOINSTALL github.com/jbussdieker/go-gb/gb > $GVM_ROOT/logs/$version-gb.log 2>&1
	if [[ $? -ne 0 ]]; then
		echo "Failed to install gb"
		return 1
	fi
}

install_goprotobuf() {
	display_message " * Installing goprotobuf..."
	$GVM_GOINSTALL goprotobuf.googlecode.com/hg/proto > $GVM_ROOT/logs/$version-pb-compiler.log 2>&1
	if [[ $? -ne 0 ]]; then
		echo "Failed to install goprotobuf"
		return 1
	fi
	#if [[ $version == "release.r60.3" ]]; then
		cd $GVM_ROOT/gos/$version/src/pkg/goprotobuf.googlecode.com/hg/compiler
		make install >> $GVM_ROOT/logs/$version-pb-compiler.log 2>&1
		if [[ $? -ne 0 ]]; then
			echo "Failed to install goprotobuf compiler"
			return 1
		fi
	#fi
}

main() {
	trap 'display_error "Canceled!"' INT
	read_command_line $@
	[ -z $VERSION ] && display_error "No version specified"
	download_source
	check_tag || (update_source && check_tag) || display_error "Unrecognized Go version"
	install_go
	GVM_GOINSTALL="goinstall"
	which goinstall &> /dev/null ||
		GVM_GOINSTALL="go get"
	x="`hg tags -R ~/.gvm/archive/go`"; echo ${x#*b0819469a6df} | grep "$version " &> /dev/null
	if [[ "$?" == "1" ]]; then
		install_gpkg
		install_gb
		install_goprotobuf
	fi
	cd $GO_INSTALL_ROOT && find . > manifest
}

main $@

