#!/bin/sh

. /usr/lib/live/config.sh
. /etc/os-release

## 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.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 " hyperv"
}

Config() {
    if ([ "$VERSION_CODENAME" = "bookworm" ] || [ "$VERSION_CODENAME" = "trixie" ] || [ "$VERSION_CODENAME" = "sid" ]) || ([ "$ID" = "ubuntu" ] && cat /proc/filesystems | grep aufs >/dev/null 2>&1); then
        if is_virtual "Hyper-V"; then
            echo "Running on Hyper-V. Configuring X11 for compatibility..."
            if [ ! -d /etc/X11/xorg.conf.d ]; then
                mkdir -p /etc/X11/xorg.conf.d || error "Failed to create directory /etc/X11/xorg.conf.d."
            fi
            cat <<EOF >/etc/X11/xorg.conf.d/30-hyperv.conf
Section "Device"
    Identifier  "HYPER-V Framebuffer"
    Driver      "fbdev"
EndSection

EOF
            echo "X11 configuration for Hyper-V completed."
        elif [ -f /etc/X11/xorg.conf.d/30-hyperv.conf ]; then
            rm -f /etc/X11/xorg.conf.d/30-hyperv.conf || error "Failed to remove /etc/X11/xorg.conf.d/30-hyperv.conf."
            echo "Removed Hyper-V X11 configuration file."
        fi
    fi
}

Cmdline
Init
Config
