#!/bin/sh

# SPDX-FileCopyrightText: 2019-2021 Aleix Pol <apol@kde.org>
# SPDX-FileCopyrightText: 2019-2021 Bhushan Shah <bshah@kde.org>
# SPDX-FileCopyrightText: 2019-2020 Jonah Brüchert <jbb@kaidan.im>
# SPDX-License-Identifier: GPL-2.0-or-later

[ -f /etc/profile ] && . /etc/profile

export QT_QPA_PLATFORM=wayland
export QT_QPA_PLATFORMTHEME=KDE
export QT_WAYLAND_DISABLE_WINDOWDECORATION=1
export EGL_PLATFORM=wayland

export QT_QUICK_CONTROLS_STYLE=org.kde.breeze
export QT_ENABLE_GLYPH_CACHE_WORKAROUND=1
export QT_QUICK_CONTROLS_MOBILE=true
export PLASMA_INTEGRATION_USE_PORTAL=1
export PLASMA_PLATFORM=phone:handset

export PLASMA_INTEGRATION_FOCUS_ON_RELEASE=1

export MOZ_ENABLE_WAYLAND=1

export ELECTRON_OZONE_PLATFORM_HINT=wayland

# if coredumpd knows about the dumps, make sure drkonqi catches them
if [[ $(cat /proc/sys/kernel/core_pattern) =~ /systemd-coredump ]]
then
	export KDE_COREDUMP_NOTIFY=1
fi

# TODO port away from PLASMA_DEFAULT_SHELL into QT_QPA_PLATFORM=offscreen plasma-apply-lookandfeel --apply org.kde.plasma.phone
export PLASMA_DEFAULT_SHELL=org.kde.plasma.phoneshell

if [ ! -d "/tmp/.X11-unix" ]; then
    mkdir /tmp/.X11-unix
    chmod +t /tmp/.X11-unix
fi

kwriteconfig5 --file ~/.config/kwinrc --group Windows --key Placement Maximizing
kwriteconfig5 --file ~/.config/kwinrc --group Windows --key BorderlessMaximizedWindows true
kwriteconfig5 --file ~/.config/kwinrc --group org.kde.kdecoration2 --key NoPlugin true

kwriteconfig5 --file ~/.config/plasmamobilerc --group General --key DesktopView false

kwriteconfig5 --file ~/.config/kdeglobals --group KDE --key LookAndFeelPackage org.kde.plasma.phone

sed -i 's/plugin=org.kde.plasma.notifications/plugin=ru.astralinux.plasma.notifications/g' ~/.config/plasma-org.kde.plasma.phoneshell-appletsrc || true

NEED_RESTART=true
DESKTOP_VIEW=false

if [ -e /var/cache/astra-mobile/start_setup_wizard ]; then
    if [ -e /usr/share/kscreen/generic-config ]; then
        export QT_SCALE_FACTOR=$(cat /usr/share/kscreen/generic-config | grep "scale" | cut -d':' -f2 | tr ' ' \\0 | tr ',' \\0)
    else
        export QT_SCALE_FACTOR=1.8
    fi

    kwin_wayland --xwayland --no-lockscreen --inputmethod maliit-keyboard /usr/lib/astra-mobile/astra-mobile-wizard
else
    while [ "$NEED_RESTART" = "true" ]
    do
      kwriteconfig5 --file ~/.config/plasmamobilerc --group General --key KWinPid --delete
      kwriteconfig5 --file ~/.config/plasmamobilerc --group General --key NeedRestart false

      DESKTOP_VIEW=$(kreadconfig5 --file ~/.config/plasmamobilerc --group General --key DesktopView)

      if [ "$DESKTOP_VIEW" = "true" ]; then
        export PLASMA_DEFAULT_SHELL=org.kde.plasma.desktop
      else
        export PLASMA_DEFAULT_SHELL=org.kde.plasma.phoneshell
      fi

      kiosk_apps=$(kreadconfig5 --file /etc/mobile-kiosk/mobile-kiosk.conf --group `whoami` --key Applications)
      if [ -n "$kiosk_apps" ]; then
          kiosk_hide_panels=$(kreadconfig5 --file /etc/mobile-kiosk/mobile-kiosk.conf --group `whoami` --key SingleMode)
          if [ "$kiosk_hide_panels" = "true" ]; then
              export PLASMA_DEFAULT_SHELL=ru.astralinux.kiosk
          fi
      fi

      startplasma-wayland --xwayland --lockscreen --inputmethod maliit-keyboard --exit-with-session=/usr/lib/x86_64-linux-gnu/libexec/startplasma-waylandsession
      NEED_RESTART=$(kreadconfig5 --file ~/.config/plasmamobilerc --group General --key NeedRestart)
    done
fi

rm -r /tmp/.X11-unix

kwriteconfig5 --file ~/.config/kwinrc --group Windows --key Placement --delete
kwriteconfig5 --file ~/.config/kwinrc --group Windows --key BorderlessMaximizedWindows --delete
kwriteconfig5 --file ~/.config/kwinrc --group org.kde.kdecoration2 --key NoPlugin --delete

kwriteconfig5 --file ~/.config/kdeglobals --group KDE --key LookAndFeelPackage --delete
