@echo off
rem Example App 9.9.9 -- self-contained bundle launcher.
rem
rem Generated by ariza for windows-x86_64. Everything this script needs is
rem inside the directory above it; nothing outside the bundle is read, and
rem nothing is installed.

setlocal enableextensions

rem %~dp0 is this script's directory with a trailing backslash, already
rem resolved, so the bundle root is one level up.
for %%I in ("%~dp0..") do set "BUNDLE_ROOT=%%~fI"

set "ARIZA_RAKU=%BUNDLE_ROOT%\rakudo\bin\raku.exe"
if not exist "%ARIZA_RAKU%" (
    echo Example App: this bundle is incomplete - no interpreter at "%ARIZA_RAKU%" 1>&2
    echo   Unpack the whole archive and run this script from inside it. 1>&2
    exit /b 1
)

rem The bundle is the only module repository -- the bundled runtime's own
rem vendor repository, which is what makes the shipped bytecode usable
rem from here rather than only where it was built.
set "RAKULIB=inst#%BUNDLE_ROOT%\rakudo\share\perl6\vendor"
set "PERL6LIB="
set "NOTCURSES_NATIVE_DATA_DIR=%BUNDLE_ROOT%\native"
rem Each set prepends, so lower-priority SQLCipher is applied first. The
rem final PATH starts with Notcurses, then SQLCipher, then the inherited PATH.
set "PATH=%BUNDLE_ROOT%\native\sqlcipher;%PATH%"
set "PATH=%BUNDLE_ROOT%\native\Notcurses-Native\binaries-notcurses-3.0.17-r11\lib;%PATH%"
set "DBIISH_SQLCIPHER_LIB=%BUNDLE_ROOT%\native\sqlcipher\sqlcipher.dll"

set "ARIZA_STATE_DIR=%LOCALAPPDATA%\exampleapp"
if not exist "%ARIZA_STATE_DIR%\.first-run" (
    echo Example App: first launch can take a few seconds. Later ones are instant. 1>&2
    if not exist "%ARIZA_STATE_DIR%" mkdir "%ARIZA_STATE_DIR%" 2>nul
    if exist "%ARIZA_STATE_DIR%" type nul >"%ARIZA_STATE_DIR%\.first-run" 2>nul
)

"%ARIZA_RAKU%" "%BUNDLE_ROOT%\rakudo\share\perl6\vendor\bin\exampleapp.raku" %*
exit /b %ERRORLEVEL%
