self="$(readlink -f "$0" 2>/dev/null || echo "$0")"
if [ "$1" = __build ]; then
root="$2"
git -C "$root" worktree prune 2>/dev/null
wt="$(mktemp -d "${TMPDIR:-/tmp}/yog-cicd.XXXXXX")" || exit 1
echo "=== $(date '+%F %T') build+install main $(git -C "$root" rev-parse --short main 2>/dev/null) ==="
status=1
if git -C "$root" worktree add --quiet --detach "$wt" main; then
if CARGO_TARGET_DIR="$root/target" make -C "$wt" install; then status=0; fi
git -C "$root" worktree remove --force "$wt" 2>/dev/null
else
echo "bl-install-main: 'git worktree add main' failed; nothing built." >&2
fi
rm -rf "$wt" 2>/dev/null
echo "=== exit=$status ==="
exit "$status"
fi
if [ "$1" = protocol ]; then
printf '{"protocol":[1],"ops":["close"]}\n'
exit 0
fi
[ -t 0 ] || cat >/dev/null 2>&1
root="$(CDPATH= cd -- "$(dirname -- "$self")/.." && pwd)" || exit 0
mkdir -p "$root/target" 2>/dev/null || exit 0
log="$root/target/cicd-install.log"
if command -v setsid >/dev/null 2>&1; then
setsid "$self" __build "$root" </dev/null >>"$log" 2>&1 &
else
nohup "$self" __build "$root" </dev/null >>"$log" 2>&1 &
fi
echo "bl-install-main: compiling + installing main in background -> $log" >&2
exit 0