#!/usr/bin/env sh
# Build the omairc-tui terminal client and the control-omairc-tui PTY driver
# with the repository version injected into both.
#
# 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 so CI snapshot versions match the Qt build; both binaries see
# the same value.
#
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 build fails loudly
# instead of silently downloading a different one, matching
# packaging/tui/PKGBUILD and tui/bin/package.
export GOTOOLCHAIN=local

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

go build -ldflags "$LDFLAGS" -o "$TUI/bin/omairc-tui$GOEXE" ./cmd/omairc-tui
go build -ldflags "$LDFLAGS" -o "$TUI/bin/control-omairc-tui$GOEXE" ./cmd/control-omairc-tui
