#!/usr/bin/env bash
# Launch one command with the standalone MAKO Renderer Vulkan layer enabled.
set -euo pipefail

usage() {
    cat <<'EOF'
Usage: mako-launch [--] COMMAND [ARG ...]

Activates MAKO Renderer for one command through its supported SDR presentation
boundary, prevents the known LSFG-VK 1.x and 2.x frame-generation layers from
joining the same Vulkan process, and uses a deterministic MAKO implicit-layer
directory for reliable swapchain ownership.

Set environment variables before mako-launch to pass them to the game:
  MAKO_PROFILE=my-profile mako-launch COMMAND [ARG ...]

Standalone launch compatibility settings are read from:
  ~/.config/mako-render/launcher.conf

MAKO_LAUNCH_CONFIG=/path/to/launcher.conf selects another settings file.

Advanced troubleshooting only:
  MAKO_ALLOW_COMPETING_LAYERS=1 mako-launch COMMAND [ARG ...]

Allowing multiple frame-generation layers in one process is unsupported and
may cause startup, synchronization, presentation, or image-quality failures.
EOF
}

if (($# == 0)); then
    echo "mako-launch requires a command to run." >&2
    usage >&2
    exit 2
fi

case "$1" in
    -h|--help)
        usage
        exit 0
        ;;
    --)
        shift
        if (($# == 0)); then
            echo "mako-launch requires a command after --." >&2
            usage >&2
            exit 2
        fi
        ;;
esac

# Launcher-only compatibility settings must be established before the child
# starts, but they are not Renderer profile values and must never be placed in
# conf.toml. Parse the UI-owned fixed key/value format without sourcing or
# evaluating user-controlled text. An invalid file fails closed to defaults.
mako_launch_config_path="${MAKO_LAUNCH_CONFIG:-}"
if [[ -z "$mako_launch_config_path" ]]; then
    if [[ -n "${XDG_CONFIG_HOME:-}" ]]; then
        mako_launch_config_path="$XDG_CONFIG_HOME/mako-render/launcher.conf"
    elif [[ -n "${HOME:-}" ]]; then
        mako_launch_config_path="$HOME/.config/mako-render/launcher.conf"
    else
        mako_launch_config_path="/etc/mako-render/launcher.conf"
    fi
fi
unset MAKO_LAUNCH_CONFIG

mako_launch_version_seen=0
mako_launch_enable_zink=0
mako_launch_force_alsa_audio=0
mako_launch_enable_zink_seen=0
mako_launch_force_alsa_audio_seen=0
mako_launch_config_valid=1
if [[ -f "$mako_launch_config_path" ]]; then
    while IFS= read -r mako_launch_line || [[ -n "$mako_launch_line" ]]; do
        mako_launch_line="${mako_launch_line#"${mako_launch_line%%[!$' \t\r\n']*}"}"
        mako_launch_line="${mako_launch_line%"${mako_launch_line##*[!$' \t\r\n']}"}"
        if [[ -z "$mako_launch_line" || "$mako_launch_line" == \#* ]]; then
            continue
        fi
        if [[ "$mako_launch_line" != *=* ]]; then
            mako_launch_config_valid=0
            break
        fi
        mako_launch_key="${mako_launch_line%%=*}"
        mako_launch_value="${mako_launch_line#*=}"
        if [[ "$mako_launch_value" == *=* ]]; then
            mako_launch_config_valid=0
            break
        fi
        mako_launch_key="${mako_launch_key#"${mako_launch_key%%[!$' \t\r\n']*}"}"
        mako_launch_key="${mako_launch_key%"${mako_launch_key##*[!$' \t\r\n']}"}"
        mako_launch_value="${mako_launch_value#"${mako_launch_value%%[!$' \t\r\n']*}"}"
        mako_launch_value="${mako_launch_value%"${mako_launch_value##*[!$' \t\r\n']}"}"
        case "$mako_launch_key" in
            version)
                if [[ "$mako_launch_version_seen" != 0 || "$mako_launch_value" != 1 ]]; then
                    mako_launch_config_valid=0
                    break
                fi
                mako_launch_version_seen=1
                ;;
            enable_zink)
                if [[ "$mako_launch_enable_zink_seen" != 0 ||
                        "$mako_launch_value" != 0 && "$mako_launch_value" != 1 ]]; then
                    mako_launch_config_valid=0
                    break
                fi
                mako_launch_enable_zink="$mako_launch_value"
                mako_launch_enable_zink_seen=1
                ;;
            force_alsa_audio)
                if [[ "$mako_launch_force_alsa_audio_seen" != 0 ||
                        "$mako_launch_value" != 0 && "$mako_launch_value" != 1 ]]; then
                    mako_launch_config_valid=0
                    break
                fi
                mako_launch_force_alsa_audio="$mako_launch_value"
                mako_launch_force_alsa_audio_seen=1
                ;;
            *)
                mako_launch_config_valid=0
                break
                ;;
        esac
    done < "$mako_launch_config_path"

    if [[ "$mako_launch_version_seen" != 1 ]]; then
        mako_launch_config_valid=0
    fi
    if [[ "$mako_launch_config_valid" != 1 ]]; then
        echo "mako-launch: ignoring invalid launcher configuration: $mako_launch_config_path" >&2
        mako_launch_enable_zink=0
        mako_launch_force_alsa_audio=0
    fi
fi
unset mako_launch_config_path mako_launch_line mako_launch_key mako_launch_value
unset mako_launch_version_seen mako_launch_config_valid
unset mako_launch_enable_zink_seen mako_launch_force_alsa_audio_seen

if [[ "$mako_launch_enable_zink" == 1 ]]; then
    export __GLX_VENDOR_LIBRARY_NAME=mesa
    export MESA_LOADER_DRIVER_OVERRIDE=zink
    export GALLIUM_DRIVER=zink
fi
if [[ "$mako_launch_force_alsa_audio" == 1 ]]; then
    export SDL_AUDIODRIVER=alsa
    case ";${WINEDLLOVERRIDES:-};" in
        *";winepulse.drv=d;"*) ;;
        *) export WINEDLLOVERRIDES="${WINEDLLOVERRIDES:+$WINEDLLOVERRIDES;}winepulse.drv=d" ;;
    esac
    case ";${WINEDLLOVERRIDES:-};" in
        *";winealsa.drv=b;"*) ;;
        *) export WINEDLLOVERRIDES="${WINEDLLOVERRIDES:+$WINEDLLOVERRIDES;}winealsa.drv=b" ;;
    esac
fi
unset mako_launch_enable_zink mako_launch_force_alsa_audio

allow_competing_layers="${MAKO_ALLOW_COMPETING_LAYERS:-0}"
case "$allow_competing_layers" in
    0)
        # These are the disable_environment gates declared by the two public
        # LSFG-VK manifest generations. The Vulkan loader must see them before
        # it discovers and assembles the process's layer chain.
        export DISABLE_LSFG=1
        export DISABLE_LSFGVK=1
        ;;
    1)
        # Explicitly remove inherited guards for an advanced comparison run.
        # This is intentionally opt-in and unsupported for ordinary gameplay.
        unset DISABLE_LSFG DISABLE_LSFGVK
        ;;
    *)
        echo "MAKO_ALLOW_COMPETING_LAYERS must be 0 or 1." >&2
        exit 2
        ;;
esac

# Do not expose the launcher's private policy switch to the renderer or game.
unset MAKO_ALLOW_COMPETING_LAYERS

# Resolve the installed private manifest directory from this launcher. This is
# the deterministic SDR boundary proven by MAKO v2: Steam's Vulkan Fossilize,
# overlay, and system-wide presentation layers cannot bypass MAKO's device or
# swapchain hooks. Gamescope and Steam's Game Mode UI remain outside this
# application layer chain. Older packages fall back to their public manifest.
mako_launcher_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
mako_install_prefix="$(cd -- "$mako_launcher_dir/.." && pwd -P)"
mako_data_home="${XDG_DATA_HOME:-$HOME/.local/share}"
mako_implicit_layer_path="$mako_install_prefix/share/mako-render/vulkan/implicit_layer.d"
if [[ ! -d "$mako_implicit_layer_path" && -d "$mako_data_home/mako-render/vulkan/implicit_layer.d" ]]; then
    mako_implicit_layer_path="$mako_data_home/mako-render/vulkan/implicit_layer.d"
elif [[ ! -d "$mako_implicit_layer_path" ]]; then
    mako_implicit_layer_path="$mako_install_prefix/share/vulkan/implicit_layer.d"
fi
export VK_IMPLICIT_LAYER_PATH="$mako_implicit_layer_path"
unset VK_ADD_IMPLICIT_LAYER_PATH
unset mako_launcher_dir mako_install_prefix mako_data_home mako_implicit_layer_path

# Gamescope WSI implements its own FIFO policy above MAKO, while MAKO expands
# one application present into a generated/original sequence below it. Keeping
# both policies active serializes the two lower presents and can collapse a
# 60 FPS game to roughly 13 FPS. Suppress WSI inside this deterministic chain;
# the Gamescope compositor itself remains active.
export DISABLE_GAMESCOPE_WSI=1
unset ENABLE_GAMESCOPE_WSI

# The current supported path is SDR. Gamescope WSI membership is fixed before
# Vulkan starts, so its experimental HDR bridge cannot be enabled later by
# live compositor feedback. Keep the launch contract explicit on both sides.
export MAKO_DISABLE_HDR_EXPOSURE=1
unset DXVK_HDR
export ENABLE_MAKO=1

exec "$@"
