#!/bin/sh
# Derselbe native NW.js-Bytecode-Lader wie in der GUI, mit unsichtbarem Fenster.
set -eu
compiler_root=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
if [ -x "$compiler_root/eibstudio" ]; then
    compiler_runtime="$compiler_root/eibstudio"
elif [ -x "$compiler_root/../../MacOS/nwjs" ]; then
    compiler_runtime="$compiler_root/../../MacOS/nwjs"
else
    printf '%s\n' 'EibStudio executable not found beside the compiler launcher.' >&2
    exit 2
fi
compiler_profile=$(mktemp -d "${TMPDIR:-/tmp}/eibstudio-compiler.XXXXXXXX")
trap 'rm -rf -- "$compiler_profile"' EXIT
if [ "$#" -eq 1 ] && [ "$1" = '--help' ]; then
    set -- --compiler-help
fi
compiler_exit=0
"$compiler_runtime" "--nwapp=$compiler_root" --compiler --disable-gpu --disable-background-networking --no-proxy-server --log-level=3 "--user-data-dir=$compiler_profile" "--compiler-result=$compiler_profile" "--compiler-cwd=$PWD" "$@" || compiler_exit=$?
if [ "$compiler_exit" -ne 0 ]; then
    exit "$compiler_exit"
fi
if [ ! -f "$compiler_profile/exitcode.txt" ]; then
    printf '%s\n' 'EibStudio terminated without a compiler result.' >&2
    exit 2
fi
compiler_exit=$(cat "$compiler_profile/exitcode.txt")
cat "$compiler_profile/stdout.txt"
cat "$compiler_profile/stderr.txt" >&2
exit "$compiler_exit"
