#!/bin/sh
# camel's entry point in this install. It starts camel.real, and before it does it answers one
# question the program itself cannot: is the C++ standard library this install finds new enough
# to load it?
#
# Nothing inside the program can answer it. The GLIBCXX versions a binary requires are ELF
# version requirements and the dynamic loader resolves them before `main`, so a too-old
# libstdc++.so.6 ends the process with one `version `GLIBCXX_3.4.x' not found` line per
# requirement, before a single line of Camel is printed.
#
# This script runs before that process exists. It asks the same loader the kernel would use
# (`--list` maps what an exec maps, and names the file it picked), and when the answer is a
# version that is too old it says which version is needed, which one is there, and where to put
# a newer one. Any other loader complaint is not its business: such a run execs the binary and
# the loader reports it exactly as it did before this script existed.
#
# Written by scripts/cxx-runtime-guard.mjs (scripts/collect-out.js), explained with the readings
# in docs/technical/58_install_cxx_runtime.md. Edit the writer, not this file.
set -u

self=$0
case $self in
*/*) ;;
*)
    found=$(command -v "$self" 2>/dev/null || :)
    if [ -z "$found" ]; then
        printf 'camel: cannot find itself on PATH, so it cannot find the install it belongs to.\n' >&2
        exit 1
    fi
    self=$found
    ;;
esac

# A symbolic link on PATH names the install its target lives in, not the directory of the link.
linked=$(readlink -f "$self" 2>/dev/null) && self=$linked
bindir=$(CDPATH= cd "$(dirname "$self")" 2>/dev/null && pwd -P) || {
    printf 'camel: cannot enter the directory this launcher sits in (%s).\n' "$(dirname "$self")" >&2
    exit 1
}
binary=$bindir/camel.real
if [ ! -x "$binary" ]; then
    printf 'camel: this launcher starts %s, and that file is missing or not executable; the install is incomplete -- install it again.\n' "$binary" >&2
    exit 1
fi

# The highest GLIBCXX_3.4.N in a blob of text (a library's dynamic strings, or the loader's
# complaint). awk and not `sort -V`: the install is read on whatever the distribution ships.
highest_glibcxx() {
    tr -cs 'A-Za-z0-9_.' '\n' |
        grep '^GLIBCXX_3\.4\.[0-9][0-9]*$' |
        awk -F. 'BEGIN { top = -1 } { if ($3 + 0 > top) top = $3 + 0 } END { if (top >= 0) printf "GLIBCXX_3.4.%d\n", top }'
}

interpreter=/lib/ld-linux-aarch64.so.1
if [ -z "$interpreter" ] || [ ! -x "$interpreter" ]; then
    interpreter=$(ls -1 /lib*/ld-linux*.so.2 2>/dev/null | head -n 1 || :)
fi
if [ -n "$interpreter" ] && [ -x "$interpreter" ]; then
    report=$("$interpreter" --list "$binary" 2>&1 >/dev/null || :)
    case $report in
    *GLIBCXX_*"not found"*)
        need=$(printf '%s\n' "$report" | highest_glibcxx)
        provider=$(printf '%s\n' "$report" | awk '/GLIBCXX_/ && /not found/ { sub(/^[^:]*: /, ""); sub(/: version .*$/, ""); print; exit }')
        if [ -z "$provider" ]; then
            provider=$bindir/../libs/libstdc++.so.6
        else
            resolved=$(readlink -f "$provider" 2>/dev/null) && provider=$resolved
        fi
        have=$(highest_glibcxx < "$provider" 2>/dev/null || :)
        if [ -z "$have" ]; then
            have="has no GLIBCXX_3.4 version at all"
        else
            have="has at most $have"
        fi
        libsdir=$bindir/../libs
        if real_libsdir=$(CDPATH= cd "$libsdir" 2>/dev/null && pwd -P); then
            libsdir=$real_libsdir
        elif root=$(CDPATH= cd "$bindir/.." 2>/dev/null && pwd -P); then
            libsdir=$root/libs
        fi
        printf 'camel: cannot start: this install needs the C++ runtime %s, and %s %s.\n' "$need" "$provider" "$have" >&2
        printf 'camel: put a libstdc++.so.6 that has %s (the one the toolchain that built this install ships) into %s, and run this again.\n' "$need" "$libsdir" >&2
        exit 1
        ;;
    esac
fi

exec "$binary" "$@"
