# Moneymoor 0.2.0 — self-contained bundle launcher.
#
# Generated by ariza for windows-x86_64. Everything this script needs is
# inside the directory above it; nothing outside the bundle is read, and
# nothing is installed.

$ErrorActionPreference = 'Stop'

$ArizaDisplay = 'Moneymoor'

# $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.
$env:RAKULIB = 'inst#' + (Join-Path $BundleRoot 'site')
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'

# Windows resolves a DLL by name from PATH, and DBIish pre-loads it by
# absolute path when told where it is.
$SqlcipherDir = Join-Path $BundleRoot 'native\sqlcipher'
$env:PATH = $SqlcipherDir + ';' + $env:PATH
$env:DBIISH_SQLCIPHER_LIB = Join-Path $BundleRoot 'native\sqlcipher\sqlcipher.dll'

$ArizaStateDir = Join-Path ($env:LOCALAPPDATA ? $env:LOCALAPPDATA : $env:TEMP) 'moneymoor'
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 'site\bin\moneymoor.raku') @args
exit $LASTEXITCODE
