# {{ app_display }} {{ app_version }} — self-contained bundle launcher.
#
# Generated by ariza for {{ platform }}. Everything this script needs is
# inside the directory above it; nothing outside the bundle is read, and
# nothing is installed.

$ErrorActionPreference = 'Stop'

$ArizaDisplay = '{{ app_display }}'
{% if updates_enabled %}$ArizaRunner = Join-Path $PSScriptRoot '{{ app_exec }}.exe'
if (-not (Test-Path -LiteralPath $ArizaRunner)) {
    [Console]::Error.WriteLine("${ArizaDisplay}: this update-enabled bundle is incomplete - no native runner at $ArizaRunner")
    exit 1
}
& $ArizaRunner @args
exit $LASTEXITCODE
{% else %}
{% endif %}# $PSScriptRoot is already the resolved directory of this script, so
# there is no symlink chasing to do — the POSIX twin's job here is done
# for us by PowerShell.
$BundleRoot = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path

$ArizaRaku = Join-Path $BundleRoot 'rakudo\bin\raku.exe'
if (-not (Test-Path -LiteralPath $ArizaRaku)) {
    [Console]::Error.WriteLine("${ArizaDisplay}: this bundle is incomplete - no interpreter at $ArizaRaku")
    [Console]::Error.WriteLine('  Unpack the whole archive and run this script from inside it.')
    exit 1
}

# The bundle is the only module repository; anything the user has set
# would put modules compiled against a different Rakudo ahead of ours.
# It is the bundled runtime's own vendor repository, which is what makes
# the shipped bytecode usable from here rather than only where it was
# built.
$env:RAKULIB = 'inst#' + (Join-Path $BundleRoot '{{ site_win }}')
Remove-Item Env:\PERL6LIB -ErrorAction SilentlyContinue

# Notcurses::Native finds its prebuilt libraries here and sets
# TERMINFO_DIRS from the same place.
$env:NOTCURSES_NATIVE_DATA_DIR = Join-Path $BundleRoot 'native'
{%- if sqlcipher %}

# Each assignment prepends, so lower-priority SQLCipher is applied first.
# The final PATH starts with Notcurses, then SQLCipher, then the inherited PATH.
$SqlcipherDir = Join-Path $BundleRoot '{{ sqlcipher_dir_win }}'
$env:PATH = $SqlcipherDir + ';' + $env:PATH
{%- endif %}
{%- if notcurses %}
$NotcursesDir = Join-Path $BundleRoot '{{ notcurses_dir_win }}'
$env:PATH = $NotcursesDir + ';' + $env:PATH
{%- endif %}
{%- if sqlcipher %}

# DBIish pre-loads SQLCipher by absolute path when told where it is.
$env:DBIISH_SQLCIPHER_LIB = Join-Path $BundleRoot '{{ sqlcipher_lib_win }}'
{%- endif %}

$ArizaStateRoot = $env:LOCALAPPDATA
if (-not $ArizaStateRoot) {
    $ArizaStateRoot = $env:TEMP
}
$ArizaStateDir = Join-Path $ArizaStateRoot '{{ app_exec }}'
if (-not (Test-Path -LiteralPath (Join-Path $ArizaStateDir '.first-run'))) {
    [Console]::Error.WriteLine("${ArizaDisplay}: first launch can take a few seconds. Later ones are instant.")
    try {
        New-Item -ItemType Directory -Force -Path $ArizaStateDir | Out-Null
        New-Item -ItemType File -Force -Path (Join-Path $ArizaStateDir '.first-run') | Out-Null
    } catch { }
}

& $ArizaRaku (Join-Path $BundleRoot '{{ target_win }}') @args
exit $LASTEXITCODE
