#!/bin/bash -e

CDIR=$(cd `dirname $0` && pwd)
cd $CDIR

ORG_PATH="github.com/coreos"
REPO_PATH="${ORG_PATH}/fleet"
VERSION=$(git describe --dirty)

if [ ! $(command -v go 2>/dev/null) ]; then
  echo "build: failed to locate Go binaries."
  exit 1
fi

source build-env

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

if [ ${GOOS} = "linux" ]; then
  echo "Building fleetd..."
  CGO_ENABLED=0 go build -o bin/fleetd -a -installsuffix netgo -ldflags "${GLDFLAGS}" ${REPO_PATH}/fleetd
else
  echo "Not on Linux - skipping fleetd build"
fi

echo "Building fleetctl..."
CGO_ENABLED=0 go build -o bin/fleetctl -a -installsuffix netgo -ldflags "${GLDFLAGS}" ${REPO_PATH}/fleetctl
