# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: Copyright 2025 SUSE LLC
# SPDX-FileCopyrightText: Copyright 2025 Richard Brown
# SPDX-FileCopyrightText: Copyright 2024 Raymond Yip
# SPDX-FileCopyrightText: Copyright 2025 Tobias Görgens

proceedInstall() {
    d --info --ok-label="Install Now" --no-wrap --width=300 --height=300 --icon=distributor-logo-symbolic --title="" --text="<big>Welcome to ${TIK_OS_NAME}</big>\n\nPlease press <b>Install Now</b> to continue"
}

displayACWarningMsg() {
    d --warning --no-wrap --title="AC Power Recommended" --text="Runnning on battery power detected\n\nIt is recommended to connect the system to AC power during the install"
}

checkLaptop() {
    chassis=$(cat /sys/class/dmi/id/chassis_type)
    # Test for respectively Handheld, Notebook, Laptop, and Portable
    # If chassis variable matches 8 9 10 or 11 function continues else it proceeds to test AC power and Battery
    [[ "$chassis" =~ ^(8|9|10|11)$ ]] || return
    # Tested machine is confirmed mobile
    givePowerRecommendation=false
    # Only check for AC and Battery power connections with upower
    updevices=$(/usr/bin/upower -e | grep -E 'AC|BAT')
    for pdev in $updevices; do
        # Get detailed info for each AC and BAT device in upower
        upinfo=$(/usr/bin/upower -i "$pdev" | grep -E 'online|state')
        # Check for discharging state or AC power offline which is equal to no state
        if [[ "$upinfo" =~ (discharging|no) ]]; then
            # Give power recommendation only once, so set this to true
            givePowerRecommendation=true
        fi
    done
    if [ "$givePowerRecommendation" = true ]; then
        log "[welcome] AC Power disconnected and Battery is not charging"
        displayACWarningMsg
    fi
}

verify_efi() {
    # Verify that the system was booted with EFI, exit with error if not
    if [ ! -d /sys/firmware/efi ]; then
        # System was not booted with EFI
        local error_msg="${TIK_OS_NAME} requires UEFI mode, which is not found on your system.\nPlease check your BIOS settings to see if UEFI can be enabled."
        error "${error_msg}"
    fi
}

proceedInstall
verify_efi
checkLaptop
