#!/bin/bash
# usage: run-build.sh <base|head> <x64|arm64> <label> [reuse]
# Runs the real desktop artifact build for Linux in that side's checkout and records the full log + exit code.
S=/tmp/scratch
side=$1; arch=$2; label=$3; reuse=$4
case $side in base) wt=$HOME/wt/base;; head) wt=$HOME/wt/head;; esac
out=$S/$side-$arch-$label; mkdir -p $out
cd $wt
[ "$arch" = arm64 ] && . $S/${CROSS_ENV:-cross-env.sh}
export T3CODE_DESKTOP_KEEP_STAGE=true
if [ "$reuse" = reuse ]; then export T3CODE_DESKTOP_REUSE_RESOURCE_MONITOR=true T3CODE_DESKTOP_REUSE_LINUX_CAPTURE_HELPERS=true; fi
{
  echo "\$ git rev-parse HEAD"; git rev-parse HEAD
  echo "\$ uname -m"; uname -m
  [ "$arch" = arm64 ] && echo "\$ . ${CROSS_ENV:-cross-env.sh}   # aarch64 cross toolchain, see the uploaded file"
  [ "$reuse" = reuse ] && echo "\$ export T3CODE_DESKTOP_REUSE_RESOURCE_MONITOR=true T3CODE_DESKTOP_REUSE_LINUX_CAPTURE_HELPERS=true"
  echo "\$ node scripts/build-desktop-artifact.ts --platform linux --target AppImage --arch $arch --skip-build --keep-stage --output-dir out"
  node scripts/build-desktop-artifact.ts --platform linux --target AppImage --arch $arch --skip-build --output-dir $out/out 2>&1
  code=$?
  echo "\$ echo \$?"; echo $code
} > $out/build.log 2>&1
grep -v "^\s*$" $out/build.log | tail -${TAILN:-25}
