#!/bin/sh

#. /lib/live/config.sh

## live-config(7) - System Configuration Components
## Copyright (C) 2016-2020 The Debian Live team
## Copyright (C) 2006-2015 Daniel Baumann <mail@daniel-baumann.ch>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.

#set -e

# The functions pkg_is_installed and component_was_executed are copied from
# /lib/live/config.sh for compatibility with older versions of Astra Linux,
# since this library is not available in Astra Linux 1.7/4.7.

pkg_is_installed() {
    local package="$1"
    if [ -z "$package" ]; then
        echo "ERROR: pkg_is_installed() needs a package as parameter" >&2
        exit 1
    fi
    if dpkg-query -f'${db:Status-Status}\n' -W $package 2>/dev/null | grep -q ^installed; then
        return 0
    fi
    return 1
}

component_was_executed() {
    local component="$1"
    if [ -z "$component" ]; then
        echo "ERROR: component_was_executed() needs a component as parameter" >&2
        exit 1
    fi
    if test -e /var/lib/live/config/$component; then
        return 0
    fi
    return 1
}

Cmdline() {
    # Reading kernel command line
    for _PARAMETER in ${LIVE_CONFIG_CMDLINE}; do
        case "${_PARAMETER}" in
        live-config.noautologin | noautologin)
            LIVE_CONFIG_NOAUTOLOGIN="true"
            ;;

        live-config.nox11autologin | nox11autologin)
            LIVE_CONFIG_NOX11AUTOLOGIN="true"
            ;;

        live-config.username=* | username=*)
            LIVE_USERNAME="${_PARAMETER#*username=}"
            ;;
        esac
    done
}

Init() {
    # Disables both console and graphical autologin.
    case "${LIVE_CONFIG_NOAUTOLOGIN}" in
    true)
        exit 0
        ;;
    esac

    # Disables graphical autologin, no matter what mechanism
    case "${LIVE_CONFIG_NOX11AUTOLOGIN}" in
    true)
        exit 0
        ;;
    esac

    # Checking if package is installed or already configured
    if ! pkg_is_installed "fly-dm" ||
        component_was_executed "fly-dm"; then
        exit 0
    fi

    echo -n " fly-dm"
}

Config() {
    # autologin
    if [ -n "${LIVE_USERNAME}" ]; then
        LIVE_X_SESSION_MANAGER=$(realpath /usr/bin/x-session-manager)
        case "${LIVE_X_SESSION_MANAGER}" in
        /usr/bin/fly-wm)
            LIVE_SESSION="fly.desktop"
            ;;
        esac

        if [ -f /etc/X11/fly-dm/fly-dmrc ]; then
            sed -i 's/#\?AutoLoginEnable=.*/AutoLoginEnable=true/' /etc/X11/fly-dm/fly-dmrc
            sed -i "s/#\?AutoLoginUser=.*/AutoLoginUser=${LIVE_USERNAME}/" /etc/X11/fly-dm/fly-dmrc
            sed -i 's/#\?AutoLoginDelay=.*/AutoLoginDelay=0/' /etc/X11/fly-dm/fly-dmrc
        fi
        if [ -f /usr/share/fly-wm/theme/default.themerc ]; then
            sed -i 's/ScreenSaverDelay=.*/ScreenSaverDelay=0/' /usr/share/fly-wm/theme/default.themerc
        fi
        if [ -f /usr/bin/fly-first-start.sh ]; then
            sed -i '3i exit 0' /usr/bin/fly-first-start.sh
        fi
        if [ -f /usr/share/fly-wm/sessrc ]; then
            sed -i 's/UseConfirmDialog=.*/UseConfirmDialog=false/' /usr/share/fly-wm/sessrc
        fi
    fi

    # Avoid xinit
    touch /var/lib/live/config/xinit

    # Creating state file
    touch /var/lib/live/config/fly-dm
}

Cmdline
Init
Config
