#!/bin/bash
if [ "$3" == "" ]; then
  echo "Please specify package name"
  exit 1
fi

cur_dir=`pwd`
cur_dirname=`pwd | awk '{ n=split($1,path,"/"); print path[n] }'`
cache_dir=$GVM_ROOT/archive/package/$3
source_dir=$GVM_ROOT/tmp/$3/src
build_dir=$GVM_ROOT/tmp/$3/build
package_name=$3
version=$4

function download_package() {
	if [ ! -d "$cache_dir" ]; then
		echo "Downloading $package_name..."
		mkdir -p $cache_dir
		git clone git@github.com:moovweb/$package_name $cache_dir > /dev/null 2>&1
		if [[ $? -ne 0 ]]; then
			echo "ERROR: Could not find $package_name in any sources"
			exit 1
		fi
	fi
}

function select_version() {
	if [ "$version" != "" ]; then
		cd $source_dir && git pull &&
		git checkout $version > /dev/null 2>&1
		if [[ $? -ne 0 ]]; then
			echo "Invalid version $version"
			exit 1
		fi
	fi
}

function setup_build_env() {
	rm -rf $build_dir && mkdir -p $build_dir
	rm -rf $source_dir
	cp -r $cache_dir/ $source_dir/
	select_version
}

function check_deps() {
	export GOPATH=$build_dir
	if [ -f "$source_dir/Package.gvm" ]; then
		for line in `cat $source_dir/Package.gvm | grep ^pkg | awk '{ print $2 }'`; do
			CUR_PKG=$GVM_ROOT/pkgs/pkg.gvm/$line/current
			if [ -e "$CUR_PKG/BUILD_VERSION" ]; then
				echo "$line: `cat $CUR_PKG/BUILD_VERSION`" >> $build_dir/manifest
			else
				#gvm pkg install $line
				#if [ $? -ne 0 ]; then
				#	echo "Failed to install dependency $line"
				#	exit 1
				#fi
				echo "ERROR: Couldnt find package: $line"
				exit 1
			fi
			export GOPATH=$GOPATH:$CUR_PKG
		done
	fi
	echo ":complete" >> $build_dir/manifest
}

function build_package() {
	echo "Installing $package_name..."
	if [[ -n $BUILD_NUMBER ]]; then
	  export BUILD_NUMBER="`cat VERSION`.$BUILD_NUMBER"
	else
		export BUILD_NUMBER="`cat $source_dir/VERSION`.dev"
	fi
	echo "$BUILD_NUMBER" > $build_dir/BUILD_VERSION
	cd $source_dir && gvmake > $build_dir/build.log 2>&1
	if [ $? -ne 0 ]; then
		echo "ERROR: Failed to build"
		cat "$build_dir/build.log"
		exit 1
	fi
}

function install_package() {
	mkdir -p $GVM_ROOT/pkgs/pkg.gvm/$package_name/$BUILD_NUMBER &&
	cp -r $build_dir/* $GVM_ROOT/pkgs/pkg.gvm/$package_name/$BUILD_NUMBER &&
	rm -f $GVM_ROOT/pkgs/pkg.gvm/$package_name/current &&
	ln -s $GVM_ROOT/pkgs/pkg.gvm/$package_name/$BUILD_NUMBER $GVM_ROOT/pkgs/pkg.gvm/$package_name/current &&
	if [ $? -ne 0 ]; then
		echo "ERROR: Failed to install"
		exit 1
	fi

	if [ -d $build_dir/bin ]; then
		mkdir -p $GVM_ROOT/pkgs/bin &&
		cp -f $build_dir/bin/* $GVM_ROOT/pkgs/bin/
	fi
}

function cleanup() {
	rm -rf $build_dir
	rm -rf $source_dir
}

#if [ "$cur_dirname" == "$package_name" ]; then
#	cache_dir=$cur_dir
#	BUILD_NUMBER=src
#	echo "Using local source"
#else
	download_package
#fi
setup_build_env
check_deps
build_package
install_package
cleanup

