#!/bin/bash
# -*- Mode: sh; indent-tabs-mode: nil; tab-width: 2 -*-

# allow overriding auto-detected or calculated environment variable values
source /etc/default/lomiri-desktop-session 2>/dev/null

# 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=${DEFAULT_QML_NO_TOUCH_COMPRESSION:-1}

if [[ $XDG_SESSION_DESKTOP == ubuntu-touch ]]; then
  export MIR_SERVER_PROMPT_FILE=1

  # Hard code socket path because our snappy apparmor profile
  # only lets us put the socket in one place.  And consumers expect it there.
  # (XDG_RUNTIME_DIR isn't typical under snappy)
  export MIR_SERVER_FILE=/run/user/"$(id -u)"/mir_socket

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

  # On Ubuntu Touch, we currently use a trick where we auto-login the
  # user without a LightDM greeter at all.  In this case, we want to
  # start with the lockscreen visible.  Once we switch to using a
  # proper greeter for the first login, we can remove this code path.
  MODE=${DEFAULT_MODE:-full-greeter}
else
  MODE=${DEFAULT_MODE:-full-shell}
fi

if [[ -x $(command -v device-info) ]]; then
    export GRID_UNIT_PX=${DEFAULT_GRID_UNIT_PX:-$(device-info get GridUnit)}
    export NATIVE_ORIENTATION=${DEFAULT_NATIVE_ORIENTATION:-$(device-info get PrimaryOrientation)}
else
    export GRID_UNIT_PX=${DEFAULT_GRID_UNIT_PX:-8}
    export NATIVE_ORIENTATION=${DEFAULT_NATIVE_ORIENTATION:-Landscape}
fi
GSETTINGS_GRID_UNIT_PX=$( gsettings get com.lomiri.touch.system global-scaling-factor | LC_ALL=C awk '{printf("%.f\n", ENVIRON["GRID_UNIT_PX"]*$1)}' )
if (( GSETTINGS_GRID_UNIT_PX >= 4 )); then
    GRID_UNIT_PX=$GSETTINGS_GRID_UNIT_PX
fi
export QT_WAYLAND_FORCE_DPI=${DEFAULT_QT_WAYLAND_FORCE_DPI:-$(( 12 * GRID_UNIT_PX ))}
dbus-update-activation-environment --systemd QT_WAYLAND_FORCE_DPI="${QT_WAYLAND_FORCE_DPI}"
dbus-update-activation-environment --systemd GRID_UNIT_PX="${GRID_UNIT_PX}"

# Hack for virtual machines that fails to probe modeset
if systemd-detect-virt --vm --quiet; then
    export MIR_MESA_KMS_DISABLE_MODESET_PROBE=${DEFAULT_MIR_MESA_KMS_DISABLE_MODESET_PROBE:-1}
    export LOMIRI_RUNNING_IN_VM=1
    dbus-update-activation-environment --systemd LOMIRI_RUNNING_IN_VM=1
elif [[ -n $DEFAULT_MIR_MESA_KMS_DISABLE_MODESET_PROBE ]]; then
    export MIR_MESA_KMS_DISABLE_MODESET_PROBE=$DEFAULT_MIR_MESA_KMS_DISABLE_MODESET_PROBE
fi

# Set some envs
export QT_QPA_PLATFORM=${DEFAULT_QT_QPA_PLATFORM:-wayland}
export QT_IM_MODULE=${DEFAULT_QT_IM_MODULE:-maliit}
export MALIIT_FORCE_DBUS_CONNECTION=${DEFAULT_MALIIT_FORCE_DBUS_CONNECTION:-1}
export UITK_ICON_THEME=${DEFAULT_UITK_ICON_THEME:-suru}
export ALARM_BACKEND=mkcal
export LOMIRI_ORGANIZER_BACKEND=mkcal
export LOMIRI_CONTACT_BACKEND=org.nemomobile.contacts.sqlite
dbus-update-activation-environment --systemd UITK_ICON_THEME="${UITK_ICON_THEME}"
dbus-update-activation-environment --systemd ALARM_BACKEND=mkcal
dbus-update-activation-environment --systemd LOMIRI_ORGANIZER_BACKEND=mkcal
dbus-update-activation-environment --systemd LOMIRI_CONTACT_BACKEND=org.nemomobile.contacts.sqlite
dbus-update-activation-environment --systemd MALIIT_FORCE_DBUS_CONNECTION="${MALIIT_FORCE_DBUS_CONNECTION}"
dbus-update-activation-environment --systemd QT_IM_MODULE="${QT_IM_MODULE}"
dbus-update-activation-environment --systemd GTK_IM_MODULE="${DEFAULT_GTK_IM_MODULE:-maliit}"
dbus-update-activation-environment --systemd QT_QPA_PLATFORM="${QT_QPA_PLATFORM}"
dbus-update-activation-environment --systemd SDL_VIDEODRIVER="${DEFAULT_SDL_VIDEODRIVER:-wayland}"
dbus-update-activation-environment --systemd QT_WAYLAND_DISABLE_WINDOWDECORATION="${DEFAULT_QT_WAYLAND_DISABLE_WINDOWDECORATION:-1}"
dbus-update-activation-environment --systemd QT_ACCESSIBILITY="${DEFAULT_QT_ACCESSIBILITY:-1}"
dbus-update-activation-environment --systemd QT_AUTO_SCREEN_SCALE_FACTOR="${DEFAULT_QT_AUTO_SCREEN_SCALE_FACTOR:-0}"
dbus-update-activation-environment --systemd GTK_CSD="${DEFAULT_GTK_CSD:-0}"

export LOMIRI_AS_SYSTEMD_UNIT=1

ENABLE_UBUNTU_ACCOUNTSSERVICE="0"
USER_LANG=$(gdbus call --system --dest org.freedesktop.Accounts --object-path /org/freedesktop/Accounts/User"$(id -u)" --method org.freedesktop.DBus.Properties.Get org.freedesktop.Accounts.User Language | cut -d\' -f2)

if [[ -n $USER_LANG ]]; then
    export LANGUAGE="$USER_LANG"
    dbus-update-activation-environment --systemd LANGUAGE="$USER_LANG"
fi

if [[ $ENABLE_UBUNTU_ACCOUNTSSERVICE == 0 ]]; then
    USER_LOCALE=$(gdbus call --system --dest org.freedesktop.Accounts --object-path /org/freedesktop/Accounts/User"$(id -u)" --method org.freedesktop.DBus.Properties.Get com.lomiri.shell.AccountsService FormatsLocale | cut -d\' -f2)

    if [[ -n $USER_LOCALE ]]; then
        export LANG="$USER_LOCALE"
        dbus-update-activation-environment --systemd LANG="$USER_LOCALE"
    fi
fi

# We need to be sure we dont use mir-on-mir
unset MIR_SERVER_HOST_SOCKET

# start with clean displays
unset DISPLAY

# Claim the user wldisplay
export WAYLAND_DISPLAY=wayland-0
dbus-update-activation-environment --systemd WAYLAND_DISPLAY="${WAYLAND_DISPLAY}"

if command -v Xwayland 2>/dev/null; then
  export MIR_SERVER_ENABLE_X11=${DEFAULT_MIR_SERVER_ENABLE_X11:-1}
  export MIR_SERVER_XWAYLAND_PATH=${DEFAULT_MIR_SERVER_XWAYLAND_PATH:-/usr/libexec/Xwayland.lomiri}
fi

# ensure XDG user directories exist, run in the C locale since Lomiri and Apps
# do not universally support localized directories
LC_ALL=C xdg-user-dirs-update

exec ${LOMIRI_BINARY:-lomiri} --mode="$MODE" "$@"

