#!/bin/bash
# Modern Libc Environment for Older Linux Distributions
# Copyright (C) RoyR 2026
#
# Important Note on Kernel Compatibility:
# While mlin provides a modern userspace environment (including a modern glibc),
# it operates on top of the host system's existing Linux kernel.
#
# Modern glibc versions and applications often rely on specific system calls
# or kernel features introduced in newer kernel versions. If the host system's
# kernel is too old, programs run via mlin may still encounter issues or fail
# to execute correctly, even with mlin's bundled libraries.
#
# mlin solves the userspace library problem, but it cannot upgrade the kernel.
# Ensure your older Linux distribution has a kernel version that is
# sufficiently recent to support the applications you intend to run.
# (e.g., a glibc from 2026 might need a kernel from ~2012-2018 at minimum,
# depending on the specific features used by the application).

# Exit on unset variables, exit on error, and fail on pipe errors.
set -euo pipefail

# Get the absolute path to the executed script, resolving all symlinks.
SCRIPT_FULL_PATH=$(readlink -f "${0}")

# Get the directory containing the actual mlin wrapper script.
SCRIPT_DIR=$(dirname "${SCRIPT_FULL_PATH}")

# Determine the mlin installation root directory.
# This assumes the 'mlin' wrapper script is located at MLIN_ROOT/bin/mlin.
MLIN_ROOT=$(dirname "${SCRIPT_DIR}")

# Get the name of the program the user is trying to run (e.g., "my_program").
PROG_NAME=$(basename "${0}")

# If the script is executed directly as "mlin" (not via a symlink to another program),
# tell the user how to use it.
if [ $(basename "${0}") = "mlin" ]; then
    echo "Error: The 'mlin' wrapper script should be symlinked to the name of the program you want to run." >&2
    echo "Example: ln -s '${SCRIPT_FULL_PATH}' /usr/local/bin/program_name" >&2
    echo "Then, execute 'program_name' directly." >&2
    exit 1
fi

# Point SSL certificates to the mlin installation's certs directory.
# This ensures applications can find trusted certificates.
export SSL_CERT_DIR="${MLIN_ROOT}/mlin/etc/ssl/certs"

# Add mlin's bin directory to the PATH for the wrapped program.
# This allows the wrapped program to find any executable binary
# dependencies installed under mlin.
export PATH="${MLIN_ROOT}/bin:${PATH}"

# Set any environment variables needed by programs running in mlin here
# These exports would only affect programs specifically looking for these variables.
export LYNX_CFG="${MLIN_ROOT}/mlin/etc/lynx/lynx.cfg"
export LYNX_LSS="${MLIN_ROOT}/mlin/etc/lynx/lynx.lss"
export GLYNX_CFG="${MLIN_ROOT}/mlin/etc/glynx/lynx.cfg"
export GLYNX_LSS="${MLIN_ROOT}/mlin/etc/glynx/lynx.lss"

# Use the dynamic linker to execute executable with proper libraries
exec "${MLIN_ROOT}/mlin/lib/ld-linux-x86-64.so.2" \
     --inhibit-cache --library-path "${MLIN_ROOT}/mlin/lib/" \
     "${MLIN_ROOT}/mlin/bin/${PROG_NAME}" "$@"

# If 'exec' fails, the script will exit here (though it shouldn't if successful).
echo "Error: Failed to execute '${MLIN_ROOT}/mlin/bin/${PROG_NAME}'" >&2
exit 1

