#!/bin/bash

# From: https://github.com/OSInside/kiwi/blob/v9.23.20/kiwi/config/functions.sh
# Or: /usr/lib/python3.6/site-packages/kiwi/config/functions.sh (python3-kiwi package)
function baseUpdateSysConfig {
    # /.../
    # Update the contents of a sysconfig variable
    # ----
    local FILE=$1
    local VAR
    local VAL
    if [ -f "${FILE}" ];then
        VAR=$2
        VAL=$3
        eval sed -i "s'@^\($VAR=\).*\$@\1\\\"$VAL\\\"@'" "${FILE}"
    else
        echo "warning: config file $FILE not found"
    fi
}

checkRoot() {
    if [[ $UID != 0 ]]; then
        echo "This option requires sudo:"
        echo "sudo $0 $*"
        exit 1
    fi
}

prepare() {
    checkRoot
    baseUpdateSysConfig /etc/sysconfig/displaymanager DISPLAYMANAGER_AUTOLOGIN linux

    USER_PATH=$(dbus-send --print-reply=literal --system --dest=org.freedesktop.Accounts /org/freedesktop/Accounts org.freedesktop.Accounts.FindUserByName string:linux)
    dbus-send --print-reply --system --dest=org.freedesktop.Accounts $USER_PATH org.freedesktop.Accounts.User.SetSessionType string:'wayland'
    dbus-send --print-reply --system --dest=org.freedesktop.Accounts $USER_PATH org.freedesktop.Accounts.User.SetSession string:'gnome-kiosk-script-wayland'

    mkdir -p /home/linux/.local/bin
    cat > /home/linux/.local/bin/gnome-kiosk-script <<EOF
#!/bin/sh

# Sleep for a year
sleep 8760h
EOF

    mkdir -p /etc/xdg/autostart
    cat > /etc/xdg/autostart/kamarada-firstboot.desktop <<EOF
[Desktop Entry]
Type=Application
Name=Kamarada FirstBoot
Exec=/usr/bin/kamarada-firstboot
EOF

    echo "enabled=False" > /home/linux/.config/user-dirs.conf
    chown linux:users /home/linux/.config/user-dirs.conf

    systemctl disable kamarada-firstboot
}

run() {
    mkdir -p ~/.config
    touch ~/.config/kamarada-firstboot
    while [ -e ~/.config/kamarada-firstboot ]
    do
        python3 /usr/share/kamarada-firstboot/kamarada-firstboot.py

        # Read text file
        # https://stackoverflow.com/a/10771857/1657502
        RESULT=$(<~/.config/kamarada-firstboot)

        # Split string
        # https://stackoverflow.com/a/1478245/1657502
        set -- junk $RESULT
        shift
        SELECTED_LANGUAGE=$1
        SELECTED_ACTION=$2

        case "$SELECTED_ACTION" in
            Try)
                rm ~/.config/kamarada-firstboot
                sudo $0 --try $SELECTED_LANGUAGE
                break
                ;;

            Shutdown)
                sudo shutdown -h now
                break
                ;;

            Reboot)
                sudo reboot
                break
                ;;
        esac
    done
}

try() {
    checkRoot

    USER_PATH=$(dbus-send --print-reply=literal --system --dest=org.freedesktop.Accounts /org/freedesktop/Accounts org.freedesktop.Accounts.FindUserByName string:linux)
    dbus-send --print-reply --system --dest=org.freedesktop.Accounts $USER_PATH org.freedesktop.Accounts.User.SetSessionType string:'wayland'
    dbus-send --print-reply --system --dest=org.freedesktop.Accounts $USER_PATH org.freedesktop.Accounts.User.SetSession string:'gnome-wayland'

    rm /home/linux/.local/bin/gnome-kiosk-script
    rm /etc/xdg/autostart/kamarada-firstboot.desktop
    rm /home/linux/.config/user-dirs.conf

    SELECTED_LANGUAGE=$1
    /usr/sbin/kamarada-setup $SELECTED_LANGUAGE

    systemctl restart display-manager
}

case "$1" in
    --prepare)
        prepare
        ;;
    --try)
        try $2
        ;;
    *)
        run
        ;;
esac

