#!/bin/sh
# configure.win — build TinyCC on Windows via Rtools (MSYS2/mingw)

THISDIR=$(cd "$(dirname "$0")" && pwd)
CONFIG_LOG="$THISDIR/config.log"
MAKE=$("${R_HOME}/bin/R" CMD config MAKE)
CC=$("${R_HOME}/bin/R" CMD config CC | awk '{print $1}')
AR=$("${R_HOME}/bin/R" CMD config AR)
TINYCC_SRC="$THISDIR/src/tinycc"
TINYCC_DEST="$THISDIR/inst/tinycc"

export MSYSTEM=UCRT64
RTINYCC_DEBUG_BUILD=${RTINYCC_DEBUG_BUILD:-0}

: > "$CONFIG_LOG"

log() {
    printf '%s\n' "$1" | tee -a "$CONFIG_LOG"
}

run_logged() {
    log "RUN: $*"
    "$@" >> "$CONFIG_LOG" 2>&1
}

cleanup_source() {
    if [ -d "$TINYCC_SRC" ]; then
        log "Removing temporary source tree: $TINYCC_SRC"
        rm -rf "$TINYCC_SRC" >> "$CONFIG_LOG" 2>&1 || true
    fi
}

trap cleanup_source EXIT

log "Starting Windows configure for Rtinycc"
log "Package source directory: $THISDIR"
log "TinyCC source directory: $TINYCC_SRC"
log "TinyCC install directory: $TINYCC_DEST"
log "MSYSTEM=$MSYSTEM"
log "RTINYCC_DEBUG_BUILD=$RTINYCC_DEBUG_BUILD"

log "Unpacking vendored TinyCC source"
run_logged "${R_HOME}/bin/Rscript" "$THISDIR/tools/vendortinycc.R" unpack || exit 1

if [ ! -d "$TINYCC_SRC" ]; then
    log "ERROR: TinyCC source directory not found after unpack"
    exit 1
fi

if [ -d "$TINYCC_DEST" ]; then
    log "Removing existing TinyCC installation"
    rm -rf "$TINYCC_DEST" >> "$CONFIG_LOG" 2>&1 || exit 1
fi
run_logged mkdir -p "$TINYCC_DEST/lib" "$TINYCC_DEST/include" "$TINYCC_DEST/libtcc" || exit 1

cd "$TINYCC_SRC" || exit 1
log "Building TinyCC in $PWD"

if [ "$RTINYCC_DEBUG_BUILD" = "1" ]; then
    log "Configuring TinyCC with debug symbols"
    run_logged ./configure --prefix="$TINYCC_DEST" --cc="$CC" --ar="$AR" --debug || exit 1
    run_logged "$MAKE" CFLAGS='-O0 -g3' libtcc.dll libtcc.a tcc.exe libtcc1.a || exit 1
else
    run_logged ./configure --prefix="$TINYCC_DEST" --cc="$CC" --ar="$AR" || exit 1
    run_logged "$MAKE" libtcc.dll libtcc.a tcc.exe libtcc1.a || exit 1
fi

if [ -f "tcc.exe" ]; then
    run_logged cp -f "tcc.exe" "$TINYCC_DEST/" || exit 1
else
    log "ERROR: tcc.exe not found after build"
    exit 1
fi

run_logged cp -f libtcc.h "$TINYCC_DEST/libtcc/" || true
run_logged cp -f include/*.h "$TINYCC_DEST/include/" || true
run_logged cp -f tcclib.h "$TINYCC_DEST/include/" || true
run_logged mkdir -p "$TINYCC_DEST/include/winapi" || exit 1
run_logged cp -rf win32/include/* "$TINYCC_DEST/include/" || true
run_logged cp -f libtcc.dll "$TINYCC_DEST/lib/" || true
run_logged cp -f libtcc.a "$TINYCC_DEST/lib/" || true
run_logged cp -f libtcc.dll "$TINYCC_DEST/" || true
run_logged cp -f win32/lib/*.def "$TINYCC_DEST/lib/" || true

LIBTCC1_SRC=$(find . -name "libtcc1.a" 2>/dev/null | head -n 1)
if [ -n "$LIBTCC1_SRC" ]; then
    run_logged mkdir -p "$TINYCC_DEST/lib/tcc" || exit 1
    run_logged cp -f "$LIBTCC1_SRC" "$TINYCC_DEST/lib/tcc/" || true
else
    log "WARNING: libtcc1.a not found during build"
fi

R_DLL=$(find "${R_HOME}/bin" -name "R.dll" 2>/dev/null | head -n 1)
if [ -n "$R_DLL" ]; then
    log "Generating R.def from $R_DLL"
    run_logged ./tcc.exe -impdef "$R_DLL" -o "$TINYCC_DEST/lib/R.def" || \
        log "WARNING: tcc -impdef failed for R.dll"
else
    log "WARNING: R.dll not found; skipping R.def generation"
fi

UCRT_DLL=$(find /c/Windows/System32 -maxdepth 1 -iname "ucrtbase.dll" 2>/dev/null | head -n 1)
if [ -n "$UCRT_DLL" ]; then
    log "Generating ucrtbase.def from $UCRT_DLL"
    run_logged ./tcc.exe -impdef "$UCRT_DLL" -o "$TINYCC_DEST/lib/ucrtbase.def" || \
        log "WARNING: tcc -impdef failed for ucrtbase.def"
    log "Generating msvcrt.def compatibility shim from $UCRT_DLL"
    run_logged ./tcc.exe -impdef "$UCRT_DLL" -o "$TINYCC_DEST/lib/msvcrt.def" || \
        log "WARNING: tcc -impdef failed for msvcrt.def"
else
    log "WARNING: ucrtbase.dll not found; keeping original CRT import defs"
fi

log "TinyCC built and installed successfully"
