#!/usr/bin/env bash

set -eu

NAME="ignition"
ORG_PATH="github.com/coreos"
REPO_PATH="${ORG_PATH}/${NAME}/v2"
GLDFLAGS=${GLDFLAGS:-}

if [ -z ${VERSION+a} ]; then
	echo "Using version from git..."
	VERSION=$(git describe --dirty --always)
fi

GLDFLAGS+="-X github.com/coreos/ignition/v2/internal/version.Raw=${VERSION}"

if [[ -n "$(git status -s)" ]]; then
    echo "git repo not clean"
    exit 1
fi

if [ -z ${BIN_PATH+a} ]; then
	export BIN_PATH=${PWD}/bin/releases/
fi

export GO11MODULE=on
export CGO_ENABLED=0
export GOFLAGS='-mod=vendor'

build_release() {
    export NAME="ignition-validate-${1}"
    echo "building ${NAME}"
    go build -ldflags "${GLDFLAGS}" -o ${BIN_PATH}/${NAME} ${REPO_PATH}/validate
}

export GOOS=linux
export GOARCH=amd64
build_release x86_64-unknown-linux-gnu

export GOOS=darwin
export GOARCH=amd64
build_release x86_64-apple-darwin

export GOOS=windows
export GOARCH=amd64
build_release x86_64-pc-windows-gnu.exe

export GOOS=linux
export GOARCH=arm64
build_release aarch64-unknown-linux-gnu
