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

if [ -z "$1" ]; then
    # Remove windows platform because of
    # https://github.com/mailgun/log/issues/10
    OS_PLATFORM_ARG=(linux windows darwin)
else
    OS_PLATFORM_ARG=($1)
fi

if [ -z "$2" ]; then
    OS_ARCH_ARG=(386 amd64 arm)
else
    OS_ARCH_ARG=($2)
fi

# Get rid of existing binaries
rm -f bundles/libcompose-cli*

BUILDTIME=$(date --rfc-3339 ns | sed -e 's/ /T/') &> /dev/null
GITCOMMIT=$(git rev-parse --short HEAD)

# Build binaries
for OS in ${OS_PLATFORM_ARG[@]}; do
    for ARCH in ${OS_ARCH_ARG[@]}; do
        OUTPUT_BIN="bundles/libcompose-cli_$OS-$ARCH"
        if test "$ARCH" = "arm"; then
            if test "$OS" = "windows" || test "$OS" = "darwin"; then
                # windows/arm and darwin/arm does not compile without cgo :-|
                continue
            fi
        fi
        if test "$OS" = "windows"; then
            OUTPUT_BIN="${OUTPUT_BIN}.exe"
        fi
        echo "Building binary for $OS/$ARCH..."
        GOARCH=$ARCH GOOS=$OS CGO_ENABLED=0 go build \
              -ldflags="-w -X github.com/docker/libcompose/version.GITCOMMIT=${GITCOMMIT} -X github.com/docker/libcompose/version.BUILDTIME=${BUILDTIME} -X github.com/docker/libcompose/version.SHOWWARNING=${SHOWWARNING}" \
              -o ${OUTPUT_BIN} ./cli/main
    done
done
