#!/usr/bin/env bash
#
# Build plugins examples for the host OS/ARCH
#

set -eu -o pipefail

# Disable CGO - we don't need it for these plugins.
#
# Important: this must be done before sourcing "./scripts/build/.variables",
# because some other variables are conditionally set whether CGO is enabled.
export CGO_ENABLED=0

source ./scripts/build/.variables

GO_LDFLAGS="${GO_LDFLAGS:-}"
GO_LDFLAGS="$GO_LDFLAGS -X \"github.com/docker/cli/cmd/docker-trust/internal/version.Version=${VERSION}\""

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

TARGET_PLUGIN="$(dirname "${TARGET}")/docker-trust-${GOOS}-${GOARCH}"
mkdir -p "$(dirname "${TARGET_PLUGIN}")"

echo "Building $GO_LINKMODE $(basename "${TARGET_PLUGIN}")"
(set -x ; GO111MODULE=auto go -C ./cmd/docker-trust build -o "../../${TARGET_PLUGIN}" -tags "${GO_BUILDTAGS}" -ldflags "${GO_LDFLAGS}" ${GO_BUILDMODE})

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