#!/bin/sh -e

ORG_PATH="github.com/coreos"
REPO_PATH="${ORG_PATH}/etcd"

export GOPATH=${PWD}/gopath

rm -f $GOPATH/src/${REPO_PATH}
mkdir -p $GOPATH/src/${ORG_PATH}
ln -s ${PWD} $GOPATH/src/${REPO_PATH}

eval $(go env)

GIT_SHA=`git rev-parse --short HEAD || echo "GitNotFound"`

val=$(go version)
# if 'go version' contains string 'devel', it assumes the go tip branch,
# which is greater than go 1.5+.
if test "${val#*devel}" != "$val" 
then
	LINK_OPERATOR="="
else
	ver=$(echo $val | awk -F ' ' '{print $3}' | awk -F '.' '{print $2}')
	if [ $ver -gt 4 ]; then
		LINK_OPERATOR="="
	else
		LINK_OPERATOR=" "
	fi
fi

# Static compilation is useful when etcd is run in a container
CGO_ENABLED=0 go build -a -installsuffix cgo -ldflags "-s -X ${REPO_PATH}/version.GitSHA${LINK_OPERATOR}${GIT_SHA}" -o bin/etcd ${REPO_PATH}
CGO_ENABLED=0 go build -a -installsuffix cgo -ldflags "-s" -o bin/etcdctl ${REPO_PATH}/etcdctl
