#!/bin/bash -e

function linker_dashX {
	local version=$(go version)
	local regex="go([0-9]+).([0-9]+)."
	if [[ $version =~ $regex ]]; then
		if [ ${BASH_REMATCH[1]} -eq "1" -a ${BASH_REMATCH[2]} -le "4" ]; then
			echo "$1 \"$2\""
		else
			echo "$1=$2"
		fi
	else
		echo "could not determine Go version"
		exit 1
	fi
}

ORG_PATH="github.com/coreos"
REPO_PATH="${ORG_PATH}/flannel"
VERSION=$(git describe --dirty)
GLDFLAGS="-X $(linker_dashX github.com/coreos/flannel/version.Version ${VERSION})"

if [ ! -h gopath/src/${REPO_PATH} ]; then
	mkdir -p gopath/src/${ORG_PATH}
	ln -s ../../../.. gopath/src/${REPO_PATH} || exit 255
fi

export GOBIN=${PWD}/bin
export GOPATH=${PWD}/gopath

eval $(go env)

if [ ${GOOS} = "linux" ]; then
	echo "Building flanneld..."
	go build -o ${GOBIN}/flanneld -ldflags "${GLDFLAGS}" ${REPO_PATH}
else
	echo "Not on Linux - skipping flanneld build"
fi
