#!/bin/sh
# TypeToPass launcher (Ubuntu / Debian). A product of Omprem Infotech Private Limited.
# Runs the bundled Python + Qt. Qt 6.9 needs glibc 2.28: Ubuntu 20.04 and newer use their own glibc;
# older systems (Ubuntu 18.04) start the app with the newer glibc and FreeType that ship inside this package.
D=/opt/typetopass
[ -d "$D/app" ] || D="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
PYV=$(ls "$D/python/bin" | grep -E '^python3\.[0-9]+$' | head -n1)
SITE=$(ls -d "$D"/python/lib/python3.*/site-packages 2>/dev/null | head -n1)
export PYTHONPATH="$D/app"
export PYTHONHOME="$D/python"
export PYTHONNOUSERSITE=1 PYTHONDONTWRITEBYTECODE=1 PYTHONUTF8=1
export QT_PLUGIN_PATH="$SITE/PySide6/Qt/plugins"
export LD_LIBRARY_PATH="$D/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
# Wayland desktops (Ubuntu 22.04+): use XWayland, which supports every feature the app uses
[ -z "$QT_QPA_PLATFORM" ] && [ -n "$WAYLAND_DISPLAY" ] && [ -n "$DISPLAY" ] && export QT_QPA_PLATFORM=xcb

glibc=$(getconf GNU_LIBC_VERSION 2>/dev/null | awk '{print $2}')
need=2.28
if [ -n "$glibc" ] && [ "$(printf '%s\n%s\n' "$need" "$glibc" | sort -V | head -n1)" != "$need" ] \
   && [ -x "$D/glibc/ld-linux-x86-64.so.2" ]; then
    exec "$D/glibc/ld-linux-x86-64.so.2" --library-path "$D/glibc:$D/compat-lib:$D/lib:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu" \
        "$D/python/bin/$PYV" -s -m typing_tutor "$@"
fi
exec "$D/python/bin/$PYV" -s -m typing_tutor "$@"
