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

# 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


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=full-greeter
else
  MODE=full-shell
fi

if [ -x "$(command -v device-info)" ]; then
    export GRID_UNIT_PX=$(device-info get GridUnit)
    export QTWEBKIT_DPR=$(device-info get WebkitDpr)
    export NATIVE_ORIENTATION=$(device-info get PrimaryOrientation)
    export 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}
fi

# Tell unity-mir to raise SIGSTOP after we start, which we use to tell
# systemd when we are done starting up.
export UNITY_MIR_EMITS_SIGSTOP=1

# Hack for virtual machines that fails to probe modeset
if ([ -x "$(command -v hostnamectl)" ] && [ "$(hostnamectl status | grep 'Chassis: vm')" ]) \
   || grep -wq hypervisor /proc/cpuinfo; then
    export MIR_MESA_KMS_DISABLE_MODESET_PROBE=1
fi

# Set some envs
export QT_QPA_PLATFORM=wayland
export QT_IM_MODULE=maliit
export MALIIT_FORCE_DBUS_CONNECTION=1
export UITK_ICON_THEME=suru
dbus-update-activation-environment --systemd UITK_ICON_THEME=suru
dbus-update-activation-environment --systemd MALIIT_FORCE_DBUS_CONNECTION=1
dbus-update-activation-environment --systemd QT_IM_MODULE=maliit
dbus-update-activation-environment --systemd GTK_IM_MODULE=maliit
dbus-update-activation-environment --systemd QT_QPA_PLATFORM=wayland
dbus-update-activation-environment --systemd SDL_VIDEODRIVER=wayland
dbus-update-activation-environment --systemd QT_WAYLAND_DISABLE_WINDOWDECORATION=1
dbus-update-activation-environment --systemd QT_ACCESSIBILITY=1
dbus-update-activation-environment --systemd QT_AUTO_SCREEN_SCALE_FACTOR=0
dbus-update-activation-environment --systemd GTK_CSD=0

# 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
  x11_display_file=$(mktemp)

  export MIR_SERVER_ENABLE_X11=1
  export MIR_SERVER_X11_DISPLAYFD=5
  ${BINARY:-lomiri} --mode=$MODE "$@" 5>${x11_display_file}&
  child=$!

  if inotifywait -qq --timeout 5 --event close_write "${x11_display_file}" && [ -s "${x11_display_file}" ]
  then
    export DISPLAY=:$(cat "${x11_display_file}")
    dbus-update-activation-environment --systemd DISPLAY=$DISPLAY
    rm "${x11_display_file}"
    echo "got DISP $DISPLAY"
  else
    echo "ERROR: Failed to get X11 display"
    rm "${x11_display_file}"
  fi

else

  ${BINARY:-lomiri} --mode=$MODE "$@" &
  child=$!

fi


# Wait for SIGSTOP in child
while [ "$(ps --no-headers -o state $child)" != "T" ]; do
  sleep 0.1
done

kill -CONT $child
exit 0
