# Local source build: invoked by tools/build-workstation with an explicit checkout.
pkgname=fds-tools
_fds_source=${FDS_SOURCE_DIR:?Run make workstation from the FDS checkout}
pkgver=$("$_fds_source/tools/version")
pkgrel=1
pkgdesc='FDS/OS workstation cartridge builder and twelve-bay ARM emulator'
arch=('x86_64' 'aarch64')
license=('MIT')
depends=('glibc' 'gcc-libs' 'xz')
makedepends=('rust' 'python' 'git')
optdepends=(
  'bubblewrap: cartridge creation and inspection in a private namespace'
  'erofs-utils: create and inspect cartridge payload filesystems'
  'qemu-system-aarch64: run FDS in the emulator'
  'qemu-img: writable emulator DATA overlays'
)
options=('!debug' '!strip')

build() {
  cd "$_fds_source"
  ./tools/build-workstation-binaries
  # Includes the notices of locked dependencies; this is an intentionally
  # complete workspace inventory rather than a claim they all link into tools.
  if [[ -f "$srcdir/RUST-NOTICES.txt" ]]; then rm "$srcdir/RUST-NOTICES.txt"; fi
  python3 tools/rust-notices "$srcdir/RUST-NOTICES.txt"
}

check() {
  local tool
  for tool in fds-cartridge fds-emulator; do
    [[ $("$_fds_source/out/workstation/$tool" --version) == "$tool $pkgver" ]]
    "$_fds_source/out/workstation/$tool" --help >/dev/null
  done
}

package() {
  local tool
  for tool in fds-cartridge fds-emulator; do
    install -Dm755 "$_fds_source/out/workstation/$tool" "$pkgdir/usr/bin/$tool"
  done
  install -Dm644 "$_fds_source/LICENSE" "$pkgdir/usr/share/licenses/fds-tools/LICENSE"
  install -Dm644 "$srcdir/RUST-NOTICES.txt" "$pkgdir/usr/share/licenses/fds-tools/RUST-NOTICES.txt"
  install -Dm644 "$_fds_source/README.md" "$pkgdir/usr/share/doc/fds-tools/README.md"
  cp -a "$_fds_source/docs" "$pkgdir/usr/share/doc/fds-tools/"
  # Ship source recipes, excluding any locally compiled example output.
  local example
  for example in hello report; do
    install -Dm644 "$_fds_source/examples/software/$example/software.toml" "$pkgdir/usr/share/doc/fds-tools/examples/software/$example/software.toml"
    cp -a "$_fds_source/examples/software/$example/void" "$pkgdir/usr/share/doc/fds-tools/examples/software/$example/"
  done
  install -Dm644 "$_fds_source/examples/software/cartridge.toml" "$pkgdir/usr/share/doc/fds-tools/examples/software/cartridge.toml"
}
