#!/usr/bin/env sh
#
# Build a static binary for the host OS/ARCH
#

set -eu

. ./scripts/build/.variables

if [ "$CGO_ENABLED" = "1" ] && [ "$(go env GOOS)" != "windows" ]; then
  GO_BUILDTAGS="$GO_BUILDTAGS pkcs11"
fi

echo "Building $GO_LINKMODE $(basename "${TARGET}")"

export GO111MODULE=auto

if [ "$(go env GOOS)" = "windows" ]; then
  if [ ! -x "$(command -v goversioninfo)" ]; then
    >&2 echo "goversioninfo not found, skipping manifesting binary"
  else
    ./scripts/build/mkversioninfo
    (set -x ; go generate -v "${SOURCE}")
  fi
fi

(set -x ; go build -o "${TARGET}" -tags "${GO_BUILDTAGS}" -ldflags "${GO_LDFLAGS}" ${GO_BUILDMODE} "${SOURCE}")

ln -sf "$(basename "${TARGET}")" "$(dirname "${TARGET}")/docker"
