#!/usr/bin/env bash
# Build libmp3lame.dll for the Windows release — the recipe CI uses (SEC5-5, DEV5-4).
#
# This is the "script used to build the DLL" that THIRD-PARTY-NOTICES.txt's LAME source offer
# names. CI runs it on the windows-latest runner (Git Bash + the runner's MSYS2), and attaches it
# to each tagged release as lame-3.100-build.sh, beside lame-3.100-source.tar.gz: the exact,
# unmodified LAME 3.100 tree it compiled (vendored in the mp3lame-sys crate, sha-checked by cargo
# against crates/mb-core/Cargo.lock).
#
#   scripts/ci-build-lame.sh            # on the Windows runner, from the repo root
#
# Steps: cargo fetch the mp3lame-sys crate (an optional dependency of crates/mb-core); pack its
# lame-3.100 folder as lame-src/lame-3.100-source.tar.gz; copy it to lame-build/; in MSYS2's
# MinGW-w64 shell: ./configure --enable-shared --disable-static --disable-frontend
# --disable-gtktest && make; strip the DLL; put it at src-tauri/dist-dll/libmp3lame.dll.
#
# If that fails: a tag build (GITHUB_REF refs/tags/v*) stops with an error. Any other build warns
# and ships the committed dist-extras/libmp3lame.dll instead, so a PR still gets an installer.
# Either way LAME_DLL=built|prebuilt goes to $GITHUB_ENV (BUILDINFO.txt and the step summary).
set -uo pipefail
cd "${GITHUB_WORKSPACE:-$(dirname "$0")/..}"
ROOT=$PWD
is_tag() { [[ "${GITHUB_REF:-}" == refs/tags/v* ]]; }
say_env() { [[ -z "${GITHUB_ENV:-}" ]] || echo "$1" >> "$GITHUB_ENV"; }
summary() { [[ -z "${GITHUB_STEP_SUMMARY:-}" ]] || echo "$1" >> "$GITHUB_STEP_SUMMARY"; }

mkdir -p src-tauri/dist-dll lame-src
(cd crates/mb-core && cargo fetch --locked) || echo "cargo fetch of the LAME source failed"
# $CARGO_HOME is a Windows path on the runner; cygpath makes it globbable.
CH=${CARGO_HOME:-$HOME/.cargo}
command -v cygpath >/dev/null && CH=$(cygpath -u "$CH")
SRC=$(ls -d "$CH"/registry/src/*/mp3lame-sys-*/lame-3.100 2>/dev/null | sort -V | tail -1)

built=0
if [[ -n "$SRC" ]]; then
  # The source as compiled, for the LGPL offer: packed before the build touches anything.
  tar -czf lame-src/lame-3.100-source.tar.gz -C "$(dirname "$SRC")" lame-3.100 \
    && echo "packed $(basename "$SRC") from $(basename "$(dirname "$SRC")")"
  rm -rf lame-build && cp -r "$SRC" lame-build
  WIN_DIR=$(cd lame-build && pwd -W)   # the folder as MSYS2's own shell spells it
  if /c/msys64/usr/bin/bash -lc "export PATH=/mingw64/bin:\$PATH; pacman -S --noconfirm --needed mingw-w64-x86_64-gcc make >/dev/null; cd '$WIN_DIR' && ./configure --enable-shared --disable-static --disable-frontend --disable-gtktest >configure.log 2>&1 && make -j4 >make.log 2>&1" \
     && cp lame-build/libmp3lame/.libs/libmp3lame-0.dll src-tauri/dist-dll/libmp3lame.dll \
     && /c/msys64/mingw64/bin/strip src-tauri/dist-dll/libmp3lame.dll; then
    built=1
  fi
fi
cd "$ROOT"

if (( built )); then
  echo "libmp3lame.dll built from source"
  say_env "LAME_DLL=built"
  summary "- LAME DLL: built from source"
else
  if is_tag; then
    echo "::error::libmp3lame.dll was not built on this runner (source missing or MSYS2 build failed; see lame-build/*.log). A release never ships the prebuilt fallback."
    exit 1
  fi
  echo "::warning::libmp3lame.dll was not built on this runner (source missing or MSYS2 build failed; see lame-build/*.log); shipping dist-extras/libmp3lame.dll instead (not allowed on a tag build)"
  cp dist-extras/libmp3lame.dll src-tauri/dist-dll/libmp3lame.dll
  say_env "LAME_DLL=prebuilt"
  summary "- LAME DLL: **prebuilt fallback** (dist-extras)"
fi
if is_tag && [[ ! -s lame-src/lame-3.100-source.tar.gz ]]; then
  echo "::error::the LAME source tarball wasn't made; a release must carry it (LGPL source offer)"
  exit 1
fi
# tauri.conf.json bundles the LGPL text beside the DLL (resources)
cp dist-extras/libmp3lame-COPYING.txt src-tauri/dist-dll/libmp3lame-COPYING.txt
ls -la src-tauri/dist-dll lame-src
sha256sum src-tauri/dist-dll/libmp3lame.dll
