#!/usr/bin/env bash
#
# Compile the Windows resources into the sources
#

set -eu -o pipefail

SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# shellcheck source=/go/src/github.com/docker/cli/scripts/build/.variables
source "$SCRIPTDIR"/../build/.variables

RESOURCES=$SCRIPTDIR/../winresources

TEMPDIR=$(mktemp -d)
trap 'rm -rf $TEMPDIR' EXIT

if [ "$(go env GOHOSTOS)" == "windows" ]; then
	WINDRES=windres
else
	# Cross compiling
	WINDRES=x86_64-w64-mingw32-windres
fi

# Generate a Windows file version of the form major,minor,patch,build (with any part optional)
VERSION_QUAD=$(echo -n "$VERSION" | sed -re 's/^([0-9.]*).*$/\1/' | tr . ,)

# Pass version and commit information into the resource compiler
defs=
[ -n "$VERSION" ]      && defs+=( "-D DOCKER_VERSION=\"$VERSION\"")
[ -n "$VERSION_QUAD" ] && defs+=( "-D DOCKER_VERSION_QUAD=$VERSION_QUAD")
[ -n "$GITCOMMIT" ]    && defs+=( "-D DOCKER_COMMIT=\"$GITCOMMIT\"")

function makeres {
	"$WINDRES" \
		-i "$RESOURCES/$1" \
		-o "$3" \
		-F "$2" \
		--use-temp-file \
		-I "$TEMPDIR" \
		${defs[*]}
}

makeres docker.rc pe-x86-64 rsrc_amd64.syso
makeres docker.rc pe-i386   rsrc_386.syso
