#!/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
        live-config.module-mode=* | module-mode=*)
            LIVE_MODULE_MODE="${_PARAMETER#*module-mode=}"
            echo "Module mode: ${LIVE_MODULE_MODE}"
            ;;
        live-config.debug | debug)
            LIVE_CONFIG_DEBUG=true
            echo "Debug mode enabled"
            ;;
        esac
    done
}

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

    echo -n " config-module-mode"
}

Config() {
    LIVE="/run/initramfs/memory"
    BUNDLES="$LIVE/bundles"
    CHANGES="$LIVE/changes"
    if [ -d "$CHANGES/changes" ] && [ -d "$CHANGES/workdir" ]; then
        CHANGES="$CHANGES/changes"
    fi

    # Configuring the system to operate in 'merged' mode
    if [ "$LIVE_MODULE_MODE" = "merged" ]; then
        echo "Configuring the system to operate in 'merged' mode."

        if [ -x /usr/sbin/minios-update-users ]; then
            echo "Updating users with minios-update-users"
            minios-update-users "$BUNDLES" "$CHANGES" >/var/log/minios/minios-update-users.output.log 2>&1
        fi

        if [ -x /usr/sbin/inios-update-cache ]; then
            echo "Updating cache with minios-update-cache"
            minios-update-cache "$BUNDLES" >/var/log/minios/minios-update-cache.output.log 2>&1 &
        fi

        if [ -x /usr/sbin/minios-update-dpkg ]; then
            echo "Updating dpkg with minios-update-dpkg"
            minios-update-dpkg "$BUNDLES" "$CHANGES" >/var/log/minios/minios-update-dpkg.output.log 2>&1 &
        fi
    fi
}

Cmdline
Init
Config
