#!/bin/bash

IS_TEXT="/usr/libexec/oem-install"
IS_GRAPHICAL="/usr/libexec/oem-install.py"
IS_UNIT=oem-install.service

# systemd targets
GRAPHICAL_TARGET="graphical.target"
CURRENT_DEFAULT_TARGET="$(systemctl get-default)"

WINDOWMANAGER_SCRIPT="/usr/libexec/initial-setup/firstboot-windowmanager"

START_GUI_COMMAND="/usr/bin/xinit ${WINDOWMANAGER_SCRIPT} ${IS_GRAPHICAL} --no-stdout-log -- /usr/bin/Xorg :9 -ac -nolisten tcp"

# check if graphical Initial Setup is installed
if [ -f ${IS_GRAPHICAL} ]; then
    # don't run the GUI on text-only systems (default.target != graphical.target),
    # users are not expecting a graphical interface do start in such case
    # and there might not even be any displays connected
    if [ "$CURRENT_DEFAULT_TARGET" == "$GRAPHICAL_TARGET" ]; then
        echo "Starting oem-install GUI" | systemd-cat -t initial-setup -p 6
        # Make look and feel the same in all DEs and avoid graphical artefacts with some themes
        export GTK_THEME=Adwaita:light
        ${START_GUI_COMMAND}
    else
        echo "OEM install GUI is installed, but default.target != graphical.target" | systemd-cat -t oem-install -p 5
        echo "Starting oem-istall" | systemd-cat -t oem-install -p 6
        ${IS_TEXT}
    fi
else
    echo "Starting oem-install" | systemd-cat -t initial-setup -p 6
    ${IS_TEXT}
fi

