#!/usr/bin/env bash
set -euo pipefail

APP_NAME="nyxtex"
OUT_DIR="dist"

cd "$(dirname "$0")"
mkdir -p "$OUT_DIR"

VERSION="${VERSION:-$(date -u +%Y%m%d-%H%M%S)}"
LDFLAGS="-s -w"

TARGETS=(
  "darwin/amd64"
  "darwin/arm64"
  "linux/amd64"
  "linux/arm64"
  "linux/386"
  "windows/amd64"
  "windows/arm64"
  "freebsd/amd64"
)

echo "Building $APP_NAME version=$VERSION -> $OUT_DIR/"

for target in "${TARGETS[@]}"; do
  goos="${target%/*}"
  goarch="${target#*/}"

  ext=""
  if [ "$goos" = "windows" ]; then
    ext=".exe"
  fi

  out="$OUT_DIR/${APP_NAME}-${VERSION}-${goos}-${goarch}${ext}"
  echo "  -> $out"
  GOOS="$goos" GOARCH="$goarch" CGO_ENABLED=0 \
    go build -trimpath -ldflags "$LDFLAGS" -o "$out" .
done

echo "Done."
