#!/usr/bin/env bash
set -e

ORG_PATH="github.com/appc"
REPO_PATH="${ORG_PATH}/spec"

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)
export GOOS GOARCH

if [ "${GOOS}" = "freebsd" ]; then
    # /usr/bin/cc is clang on freebsd, but we need to tell it to go to
    # make it generate proper flavour of code that doesn't emit
    # warnings.
    export CC=clang
fi

echo "Building actool..."
go build -o $GOBIN/actool ${REPO_PATH}/actool

if ! [[ -d "$(go env GOROOT)/pkg/${GOOS}_${GOARCH}" ]]; then
	echo "go ${GOOS}/${GOARCH} not bootstrapped, not building ACE validator"
else
	echo "Building ACE validator..."
	CGO_ENABLED=0 go build -a -installsuffix ace -ldflags '-extldflags "-static"' -o $GOBIN/ace-validator ${REPO_PATH}/ace
fi
