#!/bin/sh -ef

# Copyright (C) 2021 UBports Foundation
# Patched for mir 2.x by Azkali / Quintus port

# Ensure the shell always gets unthrottled touch events, so that applications
# who want full speed low-latency input can get it (LP: #1497105) and so that
# apps can use QML touch compression safely (the QML touch compression
# algorithm does not support nesting well - LP: #1486341, LP: #1556763 - so
# must be fed by the raw input event stream from Unity8).
export QML_NO_TOUCH_COMPRESSION=1

# mir 2.x removed mirclient and the prompt-file option, so the original
# exports below tripped boost::program_options with "unrecognised option".
# export MIR_SERVER_ENABLE_MIRCLIENT=1
# export MIR_SERVER_PROMPT_FILE=1
unset MIR_SERVER_ENABLE_MIRCLIENT
unset MIR_SERVER_PROMPT_FILE

# Strip mir1-era host-socket vars that lightdm/upstream still set.
# They map to --host-socket / --name in mir 2 and trip "unrecognised option".
unset MIR_SERVER_HOST_SOCKET
unset MIR_SERVER_NAME

# Unlikely under systemd, but just in case.
if [ -z "$XDG_RUNTIME_DIR" ]; then
    XDG_RUNTIME_DIR=/run/user/$(id -u)
    export XDG_RUNTIME_DIR
fi

MIR_SERVER_FILE="${XDG_RUNTIME_DIR}/mir_socket"

rm -f "$MIR_SERVER_FILE"
rm -f "${MIR_SERVER_FILE}_trusted"

# Point the nested mir 2.x (inside lomiri) at LSC's wayland socket
# so it nests as a wayland client rather than spawning its own android2
# stack and fighting LSC for /dev/hwcomposer.
export MIR_SERVER_PLATFORM_DISPLAY_LIBS=mir:wayland
export MIR_SERVER_PLATFORM_RENDERING_LIBS=mir:egl-generic
export MIR_SERVER_WAYLAND_HOST=/run/wayland-syscomp
export MIR_SERVER_PLATFORM_PATH=/usr/lib/aarch64-linux-gnu/mir/server-platform-nested
# Symlink LSC's wayland socket into the per-user runtime dir so the
# wayland-host platform finds it via libwayland's standard lookup.
ln -sf /run/wayland-syscomp "$XDG_RUNTIME_DIR/wayland-syscomp" 2>/dev/null
ln -sf /run/wayland-syscomp.lock "$XDG_RUNTIME_DIR/wayland-syscomp.lock" 2>/dev/null

# TODO: maybe handle some kind of collision?
export WAYLAND_DISPLAY=wayland-0

# Propegate our socket path for launching applications
dbus-update-activation-environment --systemd MIR_SOCKET="${MIR_SERVER_FILE}"
dbus-update-activation-environment --systemd WAYLAND_DISPLAY
dbus-update-activation-environment --systemd MALIIT_SERVER_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/maliit-server"

# Ensure that Lomiri uses the correct QPA.
# export QTMIR_SKIP_SESSION_AUTH=1
export QT_QPA_PLATFORM=mirserver

# Allow Lomiri to assume user's logind Display session.
export LOMIRI_AS_SYSTEMD_UNIT=1

export MIR_SERVER_ENABLE_X11=1
export MIR_SERVER_ADD_WAYLAND_EXTENSIONS=zwp_input_method_v1:zwp_input_panel_v1:zwp_text_input_manager_v2:zwp_text_input_manager_v3
export MIR_SERVER_XWAYLAND_PATH=/usr/libexec/Xwayland.lomiri
export MALIIT_SERVER_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/maliit-server"

# Lomiri cannot work with software rendering
unset LIBGL_ALWAYS_SOFTWARE
if [ "$MESA_LOADER_DRIVER_OVERRIDE" = "swrast" ]; then
    unset MESA_LOADER_DRIVER_OVERRIDE
fi

# Start maliit in background once wayland-0 is ready.
# This subshell survives the exec and starts maliit asynchronously.
(
    n=0
    while [ $n -lt 30 ]; do
        [ -e "$XDG_RUNTIME_DIR/wayland-0" ] && break
        n=$((n + 1))
        sleep 1
    done
    systemctl --user restart maliit-server.service
) &

exec ${LOMIRI_BINARY:-lomiri} "$@"
