#!/bin/sh
# Example App 9.9.9 — self-contained bundle launcher.
#
# Generated by ariza for macos-arm64. Everything this script needs is
# inside the directory above it; nothing outside the bundle is read, and
# nothing is installed. Move the bundle anywhere, including a path with
# spaces, and this still works.

set -eu

ARIZA_DISPLAY='Example App'

# Resolve this script to its physical path, following symlinks.
#
# Not `readlink -f`: that is a GNU extension, absent from macOS before
# Monterey and from the BSDs, and a launcher that only resolves symlinks
# on Linux is a launcher that breaks the moment someone puts one in
# ~/bin. Plain `readlink` is POSIX-ish and everywhere.
ariza_resolve() {
    ariza_target=$1
    while [ -L "$ariza_target" ]; do
        ariza_dir=$(cd -P -- "$(dirname -- "$ariza_target")" && pwd)
        ariza_link=$(readlink -- "$ariza_target")
        case $ariza_link in
            /*) ariza_target=$ariza_link ;;
            *)  ariza_target=$ariza_dir/$ariza_link ;;
        esac
    done
    ariza_dir=$(cd -P -- "$(dirname -- "$ariza_target")" && pwd)
    printf '%s\n' "$ariza_dir/$(basename -- "$ariza_target")"
}

# $0 is a bare name when some shells find us on PATH; ask the shell where.
ariza_self=$0
case $ariza_self in
    */*) ;;
    *)   ariza_self=$(command -v -- "$ariza_self" 2>/dev/null || printf '%s\n' "$0") ;;
esac
ariza_self=$(ariza_resolve "$ariza_self")

BUNDLE_ROOT=$(cd -P -- "$(dirname -- "$ariza_self")/.." && pwd)

ARIZA_RAKU="$BUNDLE_ROOT/rakudo/bin/raku"
if [ ! -x "$ARIZA_RAKU" ]; then
    printf '%s: this bundle is incomplete — no interpreter at %s\n' \
        "$ARIZA_DISPLAY" "$ARIZA_RAKU" >&2
    printf '  Unpack the whole archive and run this script from inside it.\n' >&2
    exit 1
fi

# The bundle is the only module repository. A RAKULIB or PERL6LIB left in
# the user's environment would otherwise put their own Raku 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 here: Rakudo records a precompiled module's
# dependencies relative to the repository only when it has a name for it,
# and a path of our own choosing would not have one.
RAKULIB="inst#$BUNDLE_ROOT/rakudo/share/perl6/vendor"
export RAKULIB
unset PERL6LIB || :

# Notcurses::Native looks for its prebuilt libraries here, and sets
# TERMINFO_DIRS from the same location. Never NOTCURSES_NATIVE_LIB_DIR,
# which skips that and leaves the terminal without terminfo.
NOTCURSES_NATIVE_DATA_DIR="$BUNDLE_ROOT/native"
export NOTCURSES_NATIVE_DATA_DIR

# A first launch compiles nothing (the bundle ships warm bytecode) but
# does have to page a few hundred megabytes off disk, so say so once.
ARIZA_STATE_DIR="${XDG_STATE_HOME:-${HOME:-/tmp}/.local/state}/exampleapp"
if [ ! -e "$ARIZA_STATE_DIR/.first-run" ]; then
    printf '%s: first launch can take a few seconds. Later ones are instant.\n' \
        "$ARIZA_DISPLAY" >&2
    if mkdir -p "$ARIZA_STATE_DIR" 2>/dev/null; then
        : >"$ARIZA_STATE_DIR/.first-run" 2>/dev/null || :
    fi
fi

# A warning, never a refusal: TERM is frequently wrong in environments
# where the app still runs fine, and a launcher that second-guesses the
# user's terminal is worse than one that mentions it.
case "${TERM:-}" in
    ''|dumb|unknown)
        printf '%s: TERM is "%s" — this is a full-screen terminal application\n' \
            "$ARIZA_DISPLAY" "${TERM:-unset}" >&2
        printf '  and may not display correctly. Try TERM=xterm-256color.\n' >&2
        ;;
esac

exec "$ARIZA_RAKU" "$BUNDLE_ROOT/rakudo/share/perl6/vendor/bin/exampleapp.raku" "$@"
