#!/bin/sh
set -eu

if [ "$#" -ne 1 ]; then
    printf '%s\n' 'usage: ./scripts/release VERSION' >&2
    exit 2
fi

release_version=$1
commandkit_root=$(CDPATH= cd "$(dirname "$0")/.." && pwd)
CDPATH= cd "$commandkit_root"

mkdir -p .releasekit/bin

commandkit_host_os=$(GOWORK=off GOTOOLCHAIN=local go env GOHOSTOS)
commandkit_host_arch=$(GOWORK=off GOTOOLCHAIN=local go env GOHOSTARCH)
GOOS="$commandkit_host_os" \
GOARCH="$commandkit_host_arch" \
GOWORK=off \
GOTOOLCHAIN=local \
go build \
    -mod=readonly \
    -o "$commandkit_root/.releasekit/bin/releasekit" \
    gitea.maximumdirect.net/eric/ReleaseKit/cmd/releasekit

exec "$commandkit_root/.releasekit/bin/releasekit" publish \
    --repo "$commandkit_root" \
    --config "$commandkit_root/releasekit.json" \
    "$release_version"
