#!/bin/sh -ex
# Copyright 2020 Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later

# This script runs as soon as the rootfs generated by
# "postmarketos install --ondev" boots up.

# Find the target partition (on same storage medium as installer, either
# internal or external)
# ---
# - p1: boot partition
# - p2: reserved space (target partition)
# - p3: install partition (mounted as /)
part_install="$(df -T / | awk '/^\/dev/ {print $1}')"
part_target="$(echo "$part_install" | sed 's/3$/2/')"
part_boot="$(echo "$part_install" | sed 's/3$/1/')"

# Sanity check
if [ "$(realpath /dev/disk/by-label/pmOS_install)" != "$part_install" ]; then
	if [ -n "$ONDEV_SKIP_LABEL_CHECK" ]; then
		echo "WARNING: not booting from pmOS_install, but" \
			"ONDEV_SKIP_LABEL_CHECK is set."
	else
		echo "ERROR: ondev-boot should only be started from the" \
			"pmOS_install partition!"
		exit 1
	fi
fi

. /etc/deviceinfo

# Find internal storage
part_target_internal=""
if [ -z "$deviceinfo_dev_internal_storage" ]; then
	echo "NOTE: deviceinfo_dev_internal_storage not defined, not" \
		"offering to install from external to internal."
elif [ "$(echo "$part_install" | sed -E 's/p?3$//')" \
		= "$deviceinfo_dev_internal_storage" ]; then
	echo "NOTE: installer runs from internal storage, not offering to" \
		"install from external to internal."
elif ! [ -b "$deviceinfo_dev_internal_storage" ]; then
	echo "WARNING: internal storage medium" \
		"('$deviceinfo_dev_internal_storage') not found, not" \
		"offering to install from external to internal."
	echo "NOTE: when testing with qemu, run with --second-storage to" \
		"simulate the internal storage."
elif [ "$deviceinfo_dev_internal_storage_repartition" != "true" ]; then
	echo "NOTE: not offering to install from external to internal" \
		"storage: deviceinfo_dev_internal_storage_repartition is not" \
		"set to true. This is a safety feature, don't change it" \
		"unless you know what you are doing! Read the deviceinfo" \
		"reference for more information:" \
		"https://postmarketos.org/deviceinfo"
else
	echo "NOTE: internal storage medium found, and installer is running" \
		"from external storage. Will offer to install from external" \
		"to internal."

	# The mobile module expects the path to the root partition (p2). We
	# don't know at this point if the partition will end just in "2" (like
	# /dev/vda2 in QEMU) or in "p2" (like /dev/mmcblk0p2). Therefore just
	# hardcode a path here and let ondev-prepare-internal-storage create
	# this symlink once it created the partition table and figured out the
	# correct path to the second partition.
	part_target_internal="/tmp/ondev-internal-storage"
fi

# Calamares module "unpackfs" needs loop
modprobe loop || true

# Configure tinydm to start i3 as root
sed -i "s/AUTOLOGIN_UID=10000/AUTOLOGIN_UID=0/" /etc/conf.d/tinydm
tinydm-set-session -s /usr/share/xsessions/i3.desktop

# Configure i3 to start calamares
mkdir -p /root/.config/i3
cat << EOF > /root/.config/i3/config
new_window none
workspace_layout tabbed
exec xrdb -merge ~/.Xresources
exec xset -dpms
exec xset s off
exec unclutter-xfixes --fork --timeout 1
exec calamares -D8
EOF

# Set environment variables
cat << EOF > /root/.profile
# Use mobile keyboard
export QT_IM_MODULE="qtvirtualkeyboard"
export QT_VIRTUALKEYBOARD_STYLE=Plasma

# Make path to boot partition available, so it can be used in
# shellprocess.conf to let foreign distros overwrite the boot partition after
# successful installation. (For postmarketOS, we'll just keep the boot
# partition from the installer.)
export ONDEV_BOOT_PARTITION="$part_boot"
EOF

# Guess DPI based on screen height (FIXME: postmarketos#15)
cat << EOF > /root/.Xresources
Xft.dpi: $(expr "$deviceinfo_screen_height" "*" 100 / 720)
EOF

# mobile.conf: set targetDeviceRoot{,Internal}
sed -i "s#^targetDeviceRoot:.*#targetDeviceRoot: \"$part_target\"#g" \
	/etc/calamares/modules/mobile.conf
sed -i "s#^targetDeviceRootInternal:.*#targetDeviceRootInternal: \"$part_target_internal\"#g" \
	/etc/calamares/modules/mobile.conf

# Add debug user
if [ -e "/no-debug-user" ]; then
	set +x
	echo "NOTE: not creating debug user, because /no-debug-user exists"
	set -x
else
	set +x
	echo "Creating debug user"
	echo "  username: 'user'"
	echo "  password: 'y'"
	echo
	echo "  Use this for login via serial or SSH over USB (172.16.42.1)"
	echo "  and debugging the installer. Calamares log is written to"
	echo "  '/root/.local/state/tinydm.log'."
	echo
	echo "  This debug user will not be created if /no-debug-user"
	echo "  exists in the installation OS. Like this:"
	echo "  'pmbootstrap install --ondev --cp anyfile:/no-debug-user'"
	echo
	set -x
	yes | adduser user -G wheel || true
fi

ondev-boot-mount
rc-service tinydm start

sleep 1
tail -F /root/.local/state/tinydm.log
