#!/usr/bin/env bash
timestamp=$(date +"%Y-%m-%d_%H-%Mh")
runtimefilename="runtime_${timestamp}.txt"
current_now=$(cat /sys/class/power_supply/BAT0/current_now)
voltage_now=$(cat /sys/class/power_supply/BAT0/voltage_now)
mapfile -t charge_control_start_threshold < <(cat /sys/class/power_supply/BAT*/charge_control_start_threshold | tr ' ' '\n')
mapfile -t charge_control_end_threshold < <(cat /sys/class/power_supply/BAT*/charge_control_end_threshold | tr ' ' '\n')
mapfile -t charge_control_start_available_thresholds < <(cat /sys/class/power_supply/BAT*/charge_control_start_available_thresholds | tr ' ' '\n')
mapfile -t charge_control_end_available_thresholds < <(cat /sys/class/power_supply/BAT*/charge_control_end_available_thresholds | tr ' ' '\n')
# shellcheck disable=SC2046
mapfile -t energy_full_design < <(upower -i $(upower -e | grep 'BAT') | grep -E 'energy-full-design' | awk '{print $2 " " $3}')
# shellcheck disable=SC2046
mapfile -t energy_full < <(upower -i $(upower -e | grep 'BAT') | grep -E 'energy-full' | grep -v "energy-full-design" | awk '{print $2 " " $3}')

if [ "$(id -u)" -ne 0 ]; then
    if [ "$LANG" = "de_DE.UTF-8" ]; then
        printf "runtime.sh muss mit root Rechten ausgefuehrt werden!"
        exec sudo --preserve-env="XDG_SESSION_TYPE,XDG_CURRENT_DESKTOP" su -c "sh $0"
    else
        printf "runtime.sh must be executed with root privileges!"
        exec sudo --preserve-env="XDG_SESSION_TYPE,XDG_CURRENT_DESKTOP" su -c "sh $0"
    fi
fi


clear
if [ "$LANG" = "de_DE.UTF-8" ]; then
    printf "%s\n" "#############################################################"
    printf "%s\n" "Bitte fuehren Sie JETZT folgende Schritte aus:"
    printf "%s\n" "- ALLE externen Geraete (USB, Ethernet, HDMI, etc.) abstecken"
    printf "%s\n" "- Netzteil abstecken"
    printf "%s\n" "- Tastaturbeleuchtung abschalten"
    printf "%s\n" "- Bildschirmhelligkeit auf ein Minimum reduzieren"
    printf "%s\n" "#############################################################"
else
    printf "%s\n" "#############################################################"
    printf "%s\n" "Please follow these steps NOW:"
    printf "%s\n" "- disconnect ALL external devices (USB, Ethernet, HDMI, etc.)"
    printf "%s\n" "- unplug charger" 
    printf "%s\n" "- deactivate keyboard backlight"
    printf "%s\n" "- set display brightness to minimum"
    printf "%s\n" "#############################################################"
fi

sleep 5
clear

if [ "$LANG" = "de_DE.UTF-8" ]; then
    printf "%s\n" "10 Sekunden bis zum Start der Messung!"
else
    printf "%s\n" "10 seconds till the recording starts!"
fi

if [ "$current_now" -eq 0 ]; then
    if [ "$LANG" = "de_DE.UTF-8" ]; then
        printf "%s\n%s\n" "Angeschlossenes Ladegerät erkannt!" "Skript wird beendet"
    else
        printf "%s\n%s\n" "Connected charger detected!" "Script will be terminated"
    fi
    exit 1;
fi

printf "%s\n" "rfkill list before block" >> "$runtimefilename"
rfkill list >> "$runtimefilename"
printf "%s\n" "Blocking Wi-Fi and Bluetooth via rfkill command..."
rfkill block all
printf "%s\n" "rfkill list after block" >> "$runtimefilename"
rfkill list >> "$runtimefilename"

for backlight in /sys/class/backlight/*; do
    if [ -d "$backlight" ]; then
        echo 0 | tee "$backlight/brightness" > /dev/null
    fi
done

for kbd in /sys/class/leds/*kbd_backlight; do
    if [ -d "$kbd" ]; then
        echo 0 | tee "$kbd/brightness" > /dev/null
    fi
done

if [ -f /sys/devices/platform/tuxedo_keyboard/charging_profile/charging_profile ]; then
    {
        printf "%s\n\n" "charging_profile"
        cat /sys/devices/platform/tuxedo_keyboard/charging_profile/charging_profile
        printf "\n"
    } >> "$runtimefilename"
else
    printf "%s\n\n\n" "Modell unterstuetzt kein charging_profile" >> "$runtimefilename"
fi

if [ -f /sys/devices/platform/tuxedo_keyboard/charging_priority/charging_prio ]; then
    {
        printf "%s\n\n" "charging_profile"
        cat /sys/devices/platform/tuxedo_keyboard/charging_priority/charging_prio
        printf "\n"
    } >> "$runtimefilename"
else
    printf "%s\n\n\n" "Modell unterstuetzt kein charging_prio" >> "$runtimefilename"
fi

for charge_type in /sys/class/power_supply/BAT*/charge_type; do
    if [ -f "$charge_type" ]; then
        {
            printf "%s\n" "charge_type"
            cat "$charge_type"

            printf "\n%s\n" "charge_control_start_threshold"
            printf "%s%%\n" "${charge_control_start_threshold[@]}"

            printf "\n%s\n" "charge_control_end_threshold"
            printf "%s%%\n" "${charge_control_end_threshold[@]}"

            printf "\n%s\n" "charge_control_start_available_thresholds"
            printf "%s%%\n" "${charge_control_start_available_thresholds[@]}"

            printf "\n%s\n" "charge_control_end_available_thresholds"
            printf "%s%%\n" "${charge_control_end_available_thresholds[@]}"
        } >> "$runtimefilename"
    else
        printf "Modell unterstuetzt kein Flexicharger" >> "$runtimefilename"
    fi
done

for raw_cycle_count in /sys/class/power_supply/BAT*/raw_cycle_count; do
    if [ -f "raw_cycle_count" ]; then
        {
            printf "%s\n\n" "raw_cycle_count"
            cat /sys/class/power_supply/BAT*/raw_cycle_count
            raw_cycle_count=$(cat /sys/class/power_supply/BAT*/raw_cycle_count)
            
            printf "\n\n\n%s\n\n" "raw_xif1"
            cat /sys/class/power_supply/BAT*/raw_xif1
            raw_xif1=$(cat /sys/class/power_supply/BAT*/raw_xif1)

            printf "\n\n\n%s\n\n" "raw_xif2"
            cat /sys/class/power_supply/BAT*/raw_xif2
            raw_xif2=$(cat /sys/class/power_supply/BAT*/raw_xif2)
            printf "\n\n\n"

            printf "%s\n" "Cycles:  $raw_cycle_count"
            printf "%s\n" "Health:  $((raw_xif2 * 100 / raw_xif1))%%"
        } >> "$runtimefilename"
    else
        printf "%s\n" "Kein NB02 Geraet" >> "$runtimefilename"
    fi
done

sleep 10

{
    printf "%s\n" "/sys/class/power_supply/BAT0/capacity"
    cat /sys/class/power_supply/BAT0/capacity
    printf "\n%s\n" "/sys/class/power_supply/BAT0/charge_full"
    cat /sys/class/power_supply/BAT0/charge_full
    printf "\n%s\n" "cat /sys/class/power_supply/BAT0/charge_now"
    cat /sys/class/power_supply/BAT0/charge_now
    printf "\n%s\n" "current battery drain"
    echo "$(( current_now * voltage_now / 1000000 / 1000 )) mW"
    printf "\n"
    date
    printf "\n%s\n%s\n" "##########" "dmidecode | grep 'Product Name'"
    dmidecode | grep 'Product Name'
    printf "\n%s\n%s\n" "##########" "uname -a"
    uname -a
    printf "\n%s\n%s\n" "##########" "lsb_release -a"
    lsb_release -a
    printf "\n%s\n%s\n" "##########" "lspci"
    lspci
    printf "\n%s\n%s\n" "##########" "lsusb"
    lsusb
    printf "\n%s\n%s\n" "##########" "lsblk"
    lsblk -dn -o MODEL | grep .
    printf "\n%s\n%s\n" "##########" "dpkg -l | grep nvidia"
    dpkg -l | grep nvidia
    # shellcheck disable=SC2034
    # shellcheck disable=SC2043
    for primeselect in /usr/bin/prime-select; do
        if [ -f "primeselect" ]; then
            printf "\n%s\n%s\n" "##########" "prime-select query"
            prime-select query
            printf "\n%s\n" "##########"
        fi
    done
} >> "$runtimefilename"

if hash xterm 2>/dev/null; then
    exec xterm -geometry 100x30 -e tail -f "$runtimefilename" &
fi

while true; do
    {
        date
        printf "\n%s\n" "/sys/class/power_supply/BAT0/capacity"
        printf "%s%%\n" "$(cat /sys/class/power_supply/BAT0/capacity)"
        printf "\n%s\n" "current battery drain"
        echo "$(( current_now * voltage_now / 1000000 / 1000 )) mW"
        printf "\n%s\n" "energy-full-design"
        # shellcheck disable=SC2128
        printf "%s\n" "$energy_full_design"
        printf "\n%s\n" "energy-full"
        # shellcheck disable=SC2128
        printf "%s\n" "$energy_full"
        printf "\n%s\n" "/sys/class/power_supply/BAT0/charge_now"
        cat /sys/class/power_supply/BAT0/charge_now
        printf "\n%s\n" "#####################" 
    } >> "$runtimefilename"
    xte 'mousermove 1 1'
    sleep 60
done
