#!/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

BEXT="sb"

Cmdline() {
    # Reading kernel command line
    for _PARAMETER in ${LIVE_CONFIG_CMDLINE}; do
        case "${_PARAMETER}" in
        from=*)
            FROM="${_PARAMETER#*from=}"
            ;;
        toram)
            TORAM="true"
            ;;
        live-config.username=* | username=*)
            LIVE_USERNAME="${_PARAMETER#*username=}"
            ;;
        live-config.link-user-dirs | link-user-dirs)
            LIVE_LINK_USER_DIRS="true"
            ;;
        live-config.bind-user-dirs | bind-user-dirs)
            LIVE_BIND_USER_DIRS="true"
            ;;
        live-config.user-dirs-path | user-dirs-path)
            LIVE_USER_DIRS_PATH="${_PARAMETER#*user-dirs-path=}"
            ;;
        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-media"
}

Config() {
    case "${LIVE_USERNAME}" in
    root)
        HOME_DIR="/root"
        ;;
    *)
        HOME_DIR="/home/${LIVE_USERNAME}"
        ;;
    esac

    if [ -z "$FROM" ] || echo "$FROM" | grep 'http://' || [ -d "/run/initramfs/memory/iso" ]; then
        FROM="minios"
    elif [ -f /run/initramfs/var/log/from.log ]; then
        FROM=$(cat /run/initramfs/var/log/from.log)
    fi

    # search for bundles in the mounted directory
    if [ "$(find "/run/initramfs/memory/data/$FROM" -maxdepth 1 -name "*.$BEXT" 2>/dev/null)" != "" ]; then
        # we found at least one bundle/module here
        FROM=$(echo "/run/initramfs/memory/data/$FROM" | tr -s "/" | sed -r "s:/[^/]+/[.][.]/:/:g")
    elif [ "$(find "/run/initramfs/memory/iso/$FROM" -maxdepth 1 -name "*.$BEXT" 2>/dev/null)" != "" ]; then
        FROM=$(echo "/run/initramfs/memory/iso/$FROM" | tr -s "/" | sed -r "s:/[^/]+/[.][.]/:/:g")
    fi
    MEDIA="$FROM"

    DEVICE=$(get_device $MEDIA)
    DEVICE_NAME=$(get_device_name $MEDIA)
    FS_TYPE=$(get_filesystem_type $MEDIA)
    MEDIA_ROOT=$(get_mount_point $MEDIA)

    echo "Checking if the media is writable or if TORAM mode is enabled"
    if ! test -w "$MEDIA" || [ "$TORAM" = "true" ]; then
        WRITABLE="false"
        echo "The media is not writable or the system is running in TORAM mode."
    else
        WRITABLE="true"
        echo "The media is writable."
    fi

    if [ "${WRITABLE}" = "true" ]; then
        case "${FS_TYPE}" in
        vfat | ntfs | exfat)
            chmod 777 "${MEDIA_ROOT}"
            mkdir -p "/media/${DEVICE_NAME}"
            mount "${DEVICE}" "/media/${DEVICE_NAME}"
            MEDIA_ROOT="/media/${DEVICE_NAME}"
            ;;
        esac
    fi

    if [ "${WRITABLE}" = "true" ]; then
        case "${FS_TYPE}" in
        vfat | ntfs | exfat)
            LIVE_USER_DIRS_PATH="${LIVE_USER_DIRS_PATH:-/minios/userdata}"
            USER_DIRS_ABS_PATH="${MEDIA_ROOT}${LIVE_USER_DIRS_PATH}"
            for DIR in Desktop Documents Downloads Music Pictures Public Templates Videos; do
                MTD="${USER_DIRS_ABS_PATH}/${DIR}"
                TARGET="${HOME_DIR}/${DIR}"
                BACKUP="${TARGET}.backup"

                # If both options are set, prefer link-user-dirs
                if [ "${LIVE_BIND_USER_DIRS}" = "true" ] && [ "${LIVE_LINK_USER_DIRS}" = "true" ]; then
                    echo "Both bind-user-dirs and link-user-dirs are set. Using link-user-dirs."
                    LIVE_BIND_USER_DIRS="false"
                fi

                if [ "${LIVE_BIND_USER_DIRS}" = "true" ]; then
                    # Ensure media folder exists
                    mkdir -p "${MTD}"

                    # --- Prepare TARGET for bind-mount ---
                    # 1) If it's a symlink — remove it
                    if [ -L "${TARGET}" ]; then
                        rm "${TARGET}"
                    fi

                    # 2) If it's a directory with data — back it up
                    if [ -d "${TARGET}" ] && [ "$(ls -A "${TARGET}")" ]; then
                        mv "${TARGET}" "${BACKUP}"
                        echo "Moved existing data ${TARGET} → ${BACKUP}"
                    fi

                    # 3) (Re)create empty directory as mountpoint
                    mkdir -p "${TARGET}"

                    # 4) Bind-mount
                    mount --bind "${MTD}" "${TARGET}" ||
                        echo "Failed to bind-mount ${MTD} → ${TARGET}"

                elif [ "${LIVE_LINK_USER_DIRS}" = "true" ]; then
                    # Ensure media folder exists
                    mkdir -p "${MTD}"

                    # --- Prepare TARGET for symlink ---
                    # 1) If it's a symlink — remove it
                    if [ -L "${TARGET}" ]; then
                        rm "${TARGET}"
                    fi

                    # 2) If it's a directory with data — back it up
                    if [ -d "${TARGET}" ] && [ "$(ls -A "${TARGET}")" ]; then
                        mv "${TARGET}" "${BACKUP}"
                        echo "Moved existing data ${TARGET} → ${BACKUP}"
                    fi

                    # 3) If it's a directory without data — remove it
                    if [ -d "${TARGET}" ]; then
                        if [ -z "$(ls -A "${TARGET}")" ]; then
                            rmdir "${TARGET}" || echo "Failed to remove empty directory ${TARGET}"
                        else
                            echo "Directory ${TARGET} is not empty, skipping removal"
                        fi
                    fi

                    # 4) Create symlink
                    ln -s "${MTD}" "${TARGET}" ||
                        echo "Failed to create symlink ${TARGET} → ${MTD}"
                fi
            done
            ;;
        esac
    fi

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

Cmdline
Init
Config
