#!/bin/bash

if [ -z "$DISPLAY" ] ; then
	# Set up the unique identifier at /etc/machine-id. D-Bus and pretty
	# much everything else requires this.
	# At the time of execution of this script, the root filesystem or
	# at least where /etc resides should be read-write.
	echo "Setting up machine-id ..."
	systemd-machine-id-setup
	# Quit Plymouth to transfer the screen to X11.
	echo "Asking Plymouth to quit ..."
	plymouth quit --wait
	# DRM drivers must be loaded in order to start the graphical
	# environment. modprobe@drm just loads the DRM framework. The rest
	# of the graphics stack must be loaded either manually or by udev.
	echo "Waiting for udev to complete device probing and setup ..."
	udevadm settle
	echo "Launching the graphical environment ..."
	exec xinit "$0"
fi

# To make dbus and others happy.
export HOME=/tmp
export XDG_RUNTIME_DIR=/run

eval $(dbus-launch)

# Setup the IME.
export GTK_IM_MODULE=fcitx
export QT_IM_MODULE=fcitx
export QT5_IM_MODULE=fcitx
export XMODIFIERS=@im=fcitx

# Fix the cursor by changing it to a pointer.
xsetroot -cursor_name left_ptr

# Try to figure out the DPI ...
dimensions=($(xrandr --current -q | grep -Po -e '\d+x\d+.*\*' | grep -Po '^\d+x\d+'))
# We just take the first.
dimension="${dimensions[0]}"
scr_x="${dimension%%x*}"
scr_y="${dimension##*x}"
# The OOBE wizard is designated to run at 1024x768.
# Use 768 as the base of the vertical resolution.
if [ "$scr_x" -gt 768 ] && [ "$scr_y" -gt 768 ] ; then
	# We expect a horizontal display.
	# Use the vertical resolution as ref.
	if [ "$scr_y" -gt "$scr_x" ] ; then
		scr_ref="$scr_x"
	else
		scr_ref="$scr_y"
	fi
	if [ "$(( scr_ref / 2304 ))" -gt 0 ] ; then
		# Webkit2gtk does not use Xft.dpi. It requires additional
		# environment variables to achieve scaling ...
		export GDK_SCALE=3
		dpi=288
	elif [ "$(( scr_ref / 1536 ))" -gt 0 ] ; then
		export GDK_SCALE=2
		dpi=192
	elif [ "$(( scr_ref / 1152 ))" -gt 0 ] ; then
		# It does not support fractional scaling.
		# Use GDK_DPI_SCALE to scale up texts.
		# GUI wizard should be started fullscreen.
		# Layout is trashed if DPI is scaled, since the window
		# dimension does not change.
		export GDK_DPI_SCALE=1.5
		dpi=144
	elif [ "$(( scr_ref / 960 ))" -gt 0 ] ; then
		export GDK_DPI_SCALE=1.25
		dpi=120
	else
		dpi=96
	fi
	echo "DPI is set to $dpi for $dimension"
	xrandr --dpi "$dpi"
fi

# Start the window manager.
kwin_x11 --no-kactivities --lock &
# FIXME: find an appropriate way to do this.
# Wait for KWin to fully start, otherwise the OOBE window won't be centered.
sleep 5

# IME
# FIXME: fcitx5 is not configred properly if LANG is not zh_CN.
env LANG=zh_CN.UTF-8 fcitx5 &

# Disable sleep/suspend
xset s off &

# Workaround for white-screen issues on some hardware
export WEBKIT_DISABLE_DMABUF_RENDERER=1
echo "Starting the GUI OOBE wizard ..." | logger
/usr/libexec/aosc-os-oobe/aosc-os-oobe-gui

source /etc/locale.conf
export LANG

# Clean up.
oma --no-check-dbus --no-progress --no-bell purge --yes aosc-os-oobe-gui
oma --no-check-dbus --no-progress --no-bell autoremove --yes

killall fcitx5 kwin_x11

# Apply the static hostname to the transient hostname.
HOSTNAME="$(hostnamectl hostname --static)"
export HOSTNAME
hostnamectl hostname --transient "$HOSTNAME"

# If the root filesystem is mounted read-only when starting up, systemd
# will setup a transient machine ID at either /run or /tmp, and the file
# will bind-mounted to /etc.
# Before we restart, this file must be committed to the disk, so the
# transient ID will persist.
# Again, /etc should be writable at the execution of this script.
systemd-machine-id-setup --commit

sync
reboot -f
