#!/bin/sh
reso=$(xrandr | grep -m1 "*+" | awk '{ print $1 }' | cut -c -4)

if [ -z $(grep xhost /home/oem/.config/autostart/oem-setup.desktop | awk '{print $1}') ]; then
    sudo -u oem xhost +
fi

textcra="<font color=red size=5>Achtung!</font><p><font size=3>Bitte führen sie die Ersteinrichtung vollständig und ohne Unterbrechung durch.<br>Der Rechner ist erst nach einem Neustart am Ende der Einrichtung voll funktionsfähig.<br>Bitte stellen Sie sicher, dass der Rechner über genug Energie verfügt.<br>Im Zweifelsfall schließen sie ihn ans Stromnetz an. Eine LAN Verbindung ist von Vorteil.&nbsp;&nbsp;&nbsp;&nbsp;</font></p><br><font color=red size=5>Attention!</font><p><font size=3>Please carry out the initial setup completely and without interruption.<br>The computer is only fully functional after a restart at the end of the setup.<br>Please make sure that the computer has enough power.<br>If in doubt, connect it to the mains. A LAN connection is an advantage.<br><br></font></p>"

export breite=450

if [ "$reso" -ge 3840 ]; then
    export QT_AUTO_SCREEN_SET_FACTOR=0
    export QT_SCALE_FACTOR=2
    export GDK_DPI_SCALE=2
    export QT_FONT_DPI=192
    export breite=850

cat <<'EOF' > $target/etc/skel/.config/kdeglobals
[KScreen]
ScaleFactor=2
ScreenScaleFactors=eDP-1=2;HDMI-1=2;DP-1=2;HDMI-2=2;DP-2=2;HDMI-3=2;DP-3=2;HDMI-4=2;DP-4=2;
EOF

cat <<'EOF' > $target/etc/skel/.config/kcmfonts
[General]
forceFontDPI=192
EOF

cat <<'EOF' > $target/etc/skel/.config/kcminputrc
[Mouse]
cursorSize=48
EOF

cat <<'EOF' >> /etc/sddm.conf.d/kde_settings.conf

[X11]
EnableHiDPI=true
ServerArguments=-dpi 192
EOF
elif [ "$reso" -ge 2400 ]; then

    export QT_AUTO_SCREEN_SET_FACTOR=0
    export QT_SCALE_FACTOR=1.5
    export GDK_DPI_SCALE=1.5
    export QT_FONT_DPI=144
    export breite=650

cat <<'EOF' > $target/etc/skel/.config/kdeglobals
[KScreen]
ScaleFactor=1.5
ScreenScaleFactors=eDP-1=1.5;HDMI-1=1.5;DP-1=1.5;HDMI-2=1.5;DP-2=1.5;HDMI-3=1.5;DP-3=1.5;HDMI-4=1.5;DP-4=1.5;
EOF

cat <<'EOF' > $target/etc/skel/.config/kcmfonts
[General]
forceFontDPI=144
EOF

cat <<'EOF' > $target/etc/skel/.config/kcminputrc
[Mouse]
cursorSize=36
EOF

cat <<'EOF' >> /etc/sddm.conf.d/kde_settings.conf

[X11]
EnableHiDPI=true
ServerArguments=-dpi 144
EOF
fi

cat <<'EOF' > /etc/skel/.config/kwalletrc
[Wallet]
Enabled=false

EOF

# Force Plasma themeing
export QT_PLATFORM_PLUGIN=kde
export KDE_FULL_SESSION=TRUE
export KDE_SESSION_VERSION=5
export QT_QPA_PLATFORMTHEME=Breeze

killall tuxedo-control-center

if [ -f /usr/bin/tomte ]; then
    while [ ! -f /tmp/tomte_finished ]
    do
	echo "" > /dev/null
	sleep 1
    done | zenity --width=$breite --progress --text "tomte installiert noch fixes. Die Installation geht gleich weiter.\ntomte is installing fixes. The installation will continue after that" --title "tomte arbeitet / tomte is running" --no-cancel --pulsate --auto-close
fi

kdialog --warningcontinuecancel "$textcra" --continue-label "Start" --cancel-label "Poweroff"
if [ $? -ge 1 ]; then
    poweroff
fi

update-pciids
systemctl stop packagekit.service
systemctl mask packagekit.service
killall DiscoverNotifier
systemctl stop tuxedo-tomte.timer
systemctl stop tuxedo-timte.service
systemctl stop firstshutdown.service
systemctl stop rmfirstshutdown.service

if [ -f /etc/systemd/system/multi-user.target.wants/firstshutdown.service ]; then
    rm -f /etc/systemd/system/multi-user.target.wants/firstshutdown.service
    rm -f /etc/systemd/system/firstshutdown.service
fi
if [ -f /etc/systemd/system/multi-user.target.wants/rmfirstshutdown.service ]; then
    rm -f /etc/systemd/system/multi-user.target.wants/rmfirstshutdown.service
    rm -f /etc/systemd/system/rmfirstshutdown.service
fi

find /home/*/.config/autostart -name cryptshutdown* -delete
find /home/*/.config/autostart -name set-click* -delete
find /etc/skel/.config/autostart -name cryptshutdown* -delete
find /etc/skel/.config/autostart -name set-click* -delete
find /usr/local/bin -name cryptshutdown.sh -delete
find /usr/local/bin -name firstshutdown.sh -delete
find /usr/local/bin -name rmfirstshutdown.sh -delete

/usr/bin/_tuxedo.calamares -d 8 --xdg-config

