#!/bin/sh

. /usr/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>
## Copyright (C) 2025 MiniOS <https://minios.dev>
##
## 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

Cmdline() {
    for _PARAMETER in ${LIVE_CONFIG_CMDLINE}; do
        case "${_PARAMETER}" in
        # User/group settings
        live-config.user-default-groups=* | user-default-groups=*)
            LIVE_USER_DEFAULT_GROUPS="${_PARAMETER#*user-default-groups=}"
            ;;
        live-config.user-fullname=* | user-fullname=*)
            LIVE_USER_FULLNAME="${_PARAMETER#*user-fullname=}"
            ;;
        live-config.username=* | username=*)
            LIVE_USERNAME="${_PARAMETER#*username=}"
            ;;
        live-config.user-password=* | user-password=*)
            LIVE_USER_PASSWORD="${_PARAMETER#*user-password=}"
            ;;
        live-config.user-password-crypted=* | user-password-crypted=*)
            LIVE_USER_PASSWORD_CRYPTED="${_PARAMETER#*user-password-crypted=}"
            ;;
        live-config.debug | debug)
            LIVE_CONFIG_DEBUG=true
            ;;
        esac
    done
}

Init() {
    # Setting debug mode if enabled
    if [ "${LIVE_CONFIG_DEBUG}" = true ]; then
        set -x
    fi

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

    echo -n " user-setup"
}

Config() {
    # Workaround for MiniOS Flux
    if [ "${LIVE_USERNAME}" = "root" ] && pkg_is_installed "fluxbox-slax"; then
        local LIVE_USERNAME="guest"
        local LIVE_USER_PASSWORD=""
        local LIVE_USER_PASSWORD_CRYPTED="*"
    fi

    # Checking if package is already configured differently
    if grep -q "^${LIVE_USERNAME}:" /etc/passwd; then
        exit 0
    fi

    # Adjust formatting of groups
    if [ -n "${LIVE_USER_DEFAULT_GROUPS}" ]; then
        LIVE_USER_DEFAULT_GROUPS="$(echo ${LIVE_USER_DEFAULT_GROUPS} | sed -e 's|,| |g')"
    fi

    # Make sure user is not in sudo group if sudo is disabled
    case "${LIVE_CONFIG_NOROOT}" in
    true)
        LIVE_USER_DEFAULT_GROUPS="$(echo ${LIVE_USER_DEFAULT_GROUPS} | sed -e 's|sudo||')"
        ;;
    esac

    # Determine user password input method
    if [ -n "${LIVE_USER_PASSWORD}" ]; then
        USER_PASS_TYPE="plain"
    elif [ -n "${LIVE_USER_PASSWORD_CRYPTED}" ]; then
        USER_PASS_TYPE="crypted"
    fi

    # Build debconf selections
    cat >/tmp/live-config.cfg <<EOF
user-setup passwd/make-user boolean true
user-setup passwd/root-password string
user-setup passwd/root-password-again string
user-setup passwd/root-password-crypted string *
EOF

    # User password selections
    case "${USER_PASS_TYPE}" in
    plain)
        cat >>/tmp/live-config.cfg <<EOF
user-setup passwd/user-password string ${LIVE_USER_PASSWORD}
user-setup passwd/user-password-again string ${LIVE_USER_PASSWORD}
user-setup passwd/user-password-crypted string
EOF
        ;;
    crypted)
        cat >>/tmp/live-config.cfg <<EOF
user-setup passwd/user-password string
user-setup passwd/user-password-again string
user-setup passwd/user-password-crypted string ${LIVE_USER_PASSWORD_CRYPTED}
EOF
        ;;
    esac

    # Common user settings
    cat >>/tmp/live-config.cfg <<EOF
user-setup passwd/user-default-groups string ${LIVE_USER_DEFAULT_GROUPS}
user-setup passwd/user-fullname string ${LIVE_USER_FULLNAME}
user-setup passwd/username string ${LIVE_USERNAME}
user-setup passwd/user-uid string 1000
EOF

    debconf-set-selections </tmp/live-config.cfg
    rm -f /tmp/live-config.cfg

    /usr/lib/user-setup/user-setup-apply 2>&1 |
        grep -v "Shadow passwords are now on" || :

    cat /etc/shadow | grep -q "^${LIVE_USERNAME}:" || {
        echo "Failed to create user ${LIVE_USERNAME}."
    }

    # Setting up user environment
    if [ -d "/home/${LIVE_USERNAME}" ]; then
        for DIR in Desktop Documents Downloads Music Pictures Public Templates Videos; do
            mkdir -p "/home/${LIVE_USERNAME}/$DIR" || echo "Failed to create /home/${LIVE_USERNAME}/$DIR"
        done
        chown ${LIVE_USERNAME}:${LIVE_USERNAME} /home/${LIVE_USERNAME} || echo "Failed to set ownership for /home/${LIVE_USERNAME}"
        chown -R ${LIVE_USERNAME}:${LIVE_USERNAME} /home/${LIVE_USERNAME} || echo "Failed to set ownership recursively for /home/${LIVE_USERNAME}"
    fi

    # Creating state file
    touch /var/lib/live/config/user-setup
}

Cmdline
Init
Config
