#!/bin/bash
set -e

DEST=$1

# explicit list of os/arch combos that support being a daemon
declare -A daemonSupporting
daemonSupporting=(
	[linux/amd64]=1
)

# if we have our linux/amd64 version compiled, let's symlink it in
if [ -x "$DEST/../binary/docker-$VERSION" ]; then
	mkdir -p "$DEST/linux/amd64"
	(
		cd "$DEST/linux/amd64"
		ln -s ../../../binary/* ./
	)
	echo "Created symlinks:" "$DEST/linux/amd64/"*
fi

for platform in $DOCKER_CROSSPLATFORMS; do
	(
		mkdir -p "$DEST/$platform" # bundles/VERSION/cross/GOOS/GOARCH/docker-VERSION
		export GOOS=${platform%/*}
		export GOARCH=${platform##*/}
		if [ -z "${daemonSupporting[$platform]}" ]; then
			export LDFLAGS_STATIC_DOCKER="" # we just need a simple client for these platforms
			export BUILDFLAGS=( "${BUILDFLAGS[@]/ daemon/}" ) # remove the "daemon" build tag from platforms that aren't supported
		fi
		source "$(dirname "$BASH_SOURCE")/binary" "$DEST/$platform"
	)
done