#!/usr/bin/env sh
# Build a static omairc-tui release archive.
#
# version.pri stays the single version source: this reads it through
# bin/version (which also enforces the Arch pkgver shape) and injects the
# result into internal/version.Value at link time. OMAIRC_BUILD_VERSION
# overrides it, matching tui/bin/build. GOOS and GOARCH default to the host.
#
# Output is tui/dist/omairc-tui-$VERSION-$GOOS-$GOARCH.tar.gz, or .zip when
# GOOS is windows. The archive contains only the binary. The Windows zip also
# carries LICENSE. Archives are reproducible: ownership, order, and mtime are
# pinned, so the same commit and toolchain produce the same bytes.
#
# On Linux, a linux target fails when ldd reports a dynamic interpreter.
# A binary built for the host must print "omairc-tui $BUILD_VERSION".
set -eu

ROOT="$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd)"
TUI="$ROOT/tui"
cd "$TUI"

# The module requires go 1.25.0. Pin the toolchain so a release build fails
# loudly instead of silently downloading a different one, matching
# packaging/tui/PKGBUILD.
export GOTOOLCHAIN=local

VERSION="$("$ROOT/bin/version")"
BUILD_VERSION="${OMAIRC_BUILD_VERSION:-$VERSION}"
LDFLAGS="-s -w -X github.com/fredimachado/omairc/tui/internal/version.Value=$BUILD_VERSION"

HOST_OS="$(go env GOHOSTOS)"
HOST_ARCH="$(go env GOHOSTARCH)"
GOOS="${GOOS:-$HOST_OS}"
GOARCH="${GOARCH:-$HOST_ARCH}"
export GOOS GOARCH CGO_ENABLED=0

case "$GOOS" in
  windows) BIN_NAME="omairc-tui.exe" ;;
  *) BIN_NAME="omairc-tui" ;;
esac

DIST="$TUI/dist"
mkdir -p "$DIST"
STAGE="$(mktemp -d)"
cleanup() { rm -rf "$STAGE"; }
trap cleanup EXIT INT TERM

go build -trimpath -ldflags "$LDFLAGS" -o "$STAGE/$BIN_NAME" ./cmd/omairc-tui

if [ "$(uname -s)" = "Linux" ] && [ "$GOOS" = "linux" ]; then
  ldd_out="$(ldd "$STAGE/$BIN_NAME" 2>&1 || true)"
  if printf '%s\n' "$ldd_out" | grep -E -q 'ld-linux|ld-musl'; then
    echo "omairc-tui is dynamically linked; CGO_ENABLED=0 must produce a static binary." >&2
    printf '%s\n' "$ldd_out" >&2
    exit 1
  fi
  if command -v readelf >/dev/null 2>&1; then
    if readelf -l "$STAGE/$BIN_NAME" 2>/dev/null | grep -q 'program interpreter'; then
      echo "omairc-tui has a dynamic interpreter; CGO_ENABLED=0 must produce a static binary." >&2
      exit 1
    fi
  fi
fi

if [ "$GOOS" = "$HOST_OS" ] && [ "$GOARCH" = "$HOST_ARCH" ]; then
  got="$("$STAGE/$BIN_NAME" --version)"
  test "$got" = "omairc-tui $BUILD_VERSION" || {
    echo "omairc-tui --version must print exactly omairc-tui $BUILD_VERSION" >&2
    printf 'got: %s\n' "$got" >&2
    exit 1
  }
fi

if [ "$GOOS" = "windows" ]; then
  cp "$ROOT/LICENSE" "$STAGE/LICENSE"
  ARCHIVE="$DIST/omairc-tui-${BUILD_VERSION}-${GOOS}-${GOARCH}.zip"
  rm -f "$ARCHIVE"
  # writezip must run on the host. GOOS/GOARCH are the archive target.
  GOOS="$HOST_OS" GOARCH="$HOST_ARCH" go run ./bin/writezip "$ARCHIVE" "$STAGE"
else
  ARCHIVE="$DIST/omairc-tui-${BUILD_VERSION}-${GOOS}-${GOARCH}.tar.gz"
  rm -f "$ARCHIVE"
  if tar --version 2>/dev/null | head -n 1 | grep -q 'GNU tar'; then
    tar -C "$STAGE" --sort=name --owner=0 --group=0 --numeric-owner \
      --mtime='1980-01-01 00:00:00 UTC' -czf "$ARCHIVE" "$BIN_NAME"
  else
    tar -C "$STAGE" -czf "$ARCHIVE" "$BIN_NAME"
  fi
fi

printf '%s\n' "$ARCHIVE"
