#!/bin/sh
# -*- Mode: sh; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Copyright (C) 2011, 2013, 2016 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, version 3 of the License.

trap cleanup TERM EXIT

cleanup()
{
    trap - TERM EXIT
    if [ -n "$CMD_PID" ]; then
        kill "$CMD_PID"
    fi
    exit 0
}

set_greeter_var()
{
    export "$1=$2"
    dbus-update-activation-environment --systemd "$1=$2"
}

# Mir 2.x: lomiri runs as a nested mir 2.x server (qtmir's mirserver QPA)
# wrapping wayland clients. The outer LSC exposes its wayland socket at
# /run/wayland-syscomp. mir's wayland-host option wants the basename and
# resolves it via XDG_RUNTIME_DIR, so we set XDG_RUNTIME_DIR=/run for the
# child process and pass the bare socket name.
LSC_WAYLAND_SOCKET=wayland-syscomp

# Clean up any stale mir socket left in the user runtime dir from a
# previous mir1 session.
rm -f "$XDG_RUNTIME_DIR/mir_socket"

# Lightdm sets MIR_SERVER_HOST_SOCKET=/run/mir_socket and MIR_SERVER_NAME=
# greeter-0 - both mir1-era. Mir 2.x parses MIR_SERVER_* as CLI options;
# --host-socket and --name don't exist in mir 2.x and trip boost's
# 'unrecognised option' error. Strip them.
unset MIR_SERVER_HOST_SOCKET
unset MIR_SERVER_NAME

echo "DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS}" >"$XDG_RUNTIME_DIR/dbus-session"

TOUCH_WRAPPER=
if which lomiri-touch-session >/dev/null; then
    TOUCH_WRAPPER=lomiri-touch-session
fi

export XDG_SESSION_DESKTOP=lomiri
export DESKTOP_SESSION=$XDG_SESSION_DESKTOP

set_greeter_var QT_IM_MODULE Maliit
# Use the new qtmir QPA against LSC's wayland socket.
set_greeter_var QT_QPA_PLATFORM mirserver
# Dump the wayland protocol traffic between lomiri's nested mir 2.x and
# LSC so we can see why frames aren't reaching the panel.
set_greeter_var WAYLAND_DEBUG 1
# Pin the nested mir 2.x to the wayland display platform + egl-generic
# rendering. Without explicit pinning, the platform prober picks
# android2 (the only one specifically built for this hardware) and
# fights LSC for /dev/hwcomposer.
set_greeter_var MIR_SERVER_PLATFORM_DISPLAY_LIBS mir:wayland
set_greeter_var MIR_SERVER_PLATFORM_RENDERING_LIBS mir:egl-generic
set_greeter_var MIR_SERVER_ADD_WAYLAND_EXTENSIONS zwp_input_method_v1:zwp_input_panel_v1:zwp_text_input_manager_v2:zwp_text_input_manager_v3
set_greeter_var MIR_SERVER_WAYLAND_HOST "/run/$LSC_WAYLAND_SOCKET"
# Tag the wayland xdg_toplevel that the wayland-host creates so LSC's
# session matching has a stable identifier.
set_greeter_var MIR_SERVER_WAYLAND_SURFACE_APP_ID greeter-0
set_greeter_var MIR_SERVER_WAYLAND_SURFACE_TITLE "Lomiri greeter"
# Do NOT export WAYLAND_DISPLAY: libwayland's wl_display_add_socket reads
# WAYLAND_DISPLAY as the server-side socket name, which would make our
# nested mir 2.x try to publish its own server at $LSC_WAYLAND_SOCKET and
# collide with LSC. MIR_SERVER_WAYLAND_HOST alone routes the client side.
# Keep XDG_RUNTIME_DIR at the lightdm-user default (/run/user/108) so
# the nested mir 2.x server can publish its own wayland socket there.
# But link LSC's socket from /run into that runtime dir so mir's
# wayland-host lookup finds it.
ln -sf /run/$LSC_WAYLAND_SOCKET "$XDG_RUNTIME_DIR/$LSC_WAYLAND_SOCKET" 2>/dev/null
ln -sf /run/$LSC_WAYLAND_SOCKET.lock "$XDG_RUNTIME_DIR/$LSC_WAYLAND_SOCKET.lock" 2>/dev/null
LOMIRI_RUNTIME_DIR="$XDG_RUNTIME_DIR"

set_greeter_var PA_DISABLED 1
set_greeter_var MALIIT_SERVER_ADDRESS "unix:path=$XDG_RUNTIME_DIR/maliit-server"

if [ "$(id -u phablet 2>/dev/null)" = "32011" ]; then
    USER_LANG=$(gdbus call --system --dest org.freedesktop.Accounts --object-path /org/freedesktop/Accounts/User32011 --method org.freedesktop.DBus.Properties.Get org.freedesktop.Accounts.User Language | cut -d\' -f2)
    if [ -n "$USER_LANG" ]; then
        set_greeter_var LANGUAGE "$USER_LANG"
    fi
    USER_LOCALE=$(gdbus call --system --dest org.freedesktop.Accounts --object-path /org/freedesktop/Accounts/User32011 --method org.freedesktop.DBus.Properties.Get org.freedesktop.Accounts.User FormatsLocale | cut -d\' -f2)
    if [ -n "$USER_LOCALE" ]; then
        set_greeter_var LANG "$USER_LOCALE"
        set_greeter_var LC_ALL "$USER_LOCALE"
    fi
fi

if [ -n "$(command -v device-info)" ]; then
    GRID_UNIT_PX="$(device-info get GridUnit)"
fi

export GRID_UNIT_PX="${GRID_UNIT_PX_OVERRIDE:-${GRID_UNIT_PX:-18}}"

exec env XDG_RUNTIME_DIR="$LOMIRI_RUNTIME_DIR" $TOUCH_WRAPPER $@ &
CMD_PID=$!

# Wait for either the wayland socket (mir2) or a per-user mir socket
# (legacy mir1, in case anything still publishes one).
count=0
limit=60
while [ true ]; do
    count=$((count + 1))
    [ $count -gt $limit ] && break
    [ -e "/run/$LSC_WAYLAND_SOCKET" ] && break
    sleep 1s
done

systemctl --user restart maliit-server.service &
systemctl --user restart lomiri-indicators.target &

wait $CMD_PID
CMD_PID=

systemctl --user stop maliit-server.service
systemctl --user stop lomiri-indicators.target
