#!/usr/bin/env bash

ERROR_CHECK="0"
AG="alt-gaming"

print_ok () { printf "\E[35m%s OK: $@ %s\e[0m\n" ;}
print_check () { printf "\E[36m%s Check: $@ %s\e[0m\n" ;}
print_warning () { printf "\E[33m%s Warning: $@ %s\e[0m\n" ;}
print_error () { printf "\E[31m%s Error: $@ %s\e[0m\n" && ERROR_CHECK="1" ;}

installed () { rpm -q "$1" &>/dev/null ;}

print_package () {
    if installed "$1"
    then echo -e "\n Package \"$1\" installed."
    else echo -e "\n Package \"$1\" not installed."
    fi
}

# verdict <package> <status> <ok message> <fail message>
# The setting is checked first, the package second: a good setting needs no
# package, a bad one is an error with the package and only a hint without it.
verdict () {
    local pkg="$1" status="$2" ok_msg="$3" fail_msg="$4"
    if [[ "$status" == 0 ]] ; then
        print_ok "$ok_msg"
    elif installed "$pkg" ; then
        print_error "$fail_msg"
    else
        print_warning "$fail_msg Install \"$pkg\" to fix it."
    fi
}

kernel_ge () {  # kernel_ge MAJOR MINOR
    local kmaj kmin
    IFS=. read -r kmaj kmin _ <<< "$(uname -r)"
    (( kmaj > $1 || (kmaj == $1 && kmin >= $2) ))
}

print_package $AG-esync
print_check "esync (ulimit -Hn=$(ulimit -Hn))"
[[ $(ulimit -Hn) -ge 524288 ]] 2>/dev/null
verdict $AG-esync $? "esync support is enabled." "esync support doesn't work."

print_package $AG-ntsync
if kernel_ge 6 14 ; then
    print_check "ntsync (/dev/ntsync is exist)"
    [[ -e /dev/ntsync ]]
    verdict $AG-ntsync $? "ntsync support is enabled." "ntsync support doesn't work."
else
    print_warning "ntsync supported only for kernel 6.14+. Skip it."
fi

print_package $AG-mm-count
print_check "vm.max_map_count=$(cat /proc/sys/vm/max_map_count)"
[[ $(cat /proc/sys/vm/max_map_count) -ge 2147483642 ]]
verdict $AG-mm-count $? "Reduce operating system mmap restrictions is enabled" \
    "Reduce operating system mmap restrictions doesn't work."

print_package $AG-clearcpuid514
print_check "clearcpuid=514 ($(grep "LINUX_DEFAULT" /etc/sysconfig/grub2 2>/dev/null))"
! grep -i -q "umip" /proc/cpuinfo
verdict $AG-clearcpuid514 $? "UMIP is disabled (clearcpuid=514 is working, or the flag is not supported)." \
    "UMIP is enabled (clearcpuid=514 doesn't work)."

print_package $AG-tcp-mtu-probing
print_check "net.ipv4.tcp_mtu_probing ($(cat /proc/sys/net/ipv4/tcp_mtu_probing))"
[[ $(cat /proc/sys/net/ipv4/tcp_mtu_probing) -ge 1 ]]
verdict $AG-tcp-mtu-probing $? "net.ipv4.tcp_mtu_probing is enabled" "net.ipv4.tcp_mtu_probing=1 doesn't work."

print_package $AG-alive-timeout
print_check "GNOME check alive timeout"
if [[ "${DESKTOP_SESSION}" =~ gnome ]] ; then
    GNOME_AT=$(gsettings get org.gnome.mutter check-alive-timeout | awk '{print $2}')
    [[ $GNOME_AT -ge 15000 ]] 2>/dev/null
    verdict $AG-alive-timeout $? "check-alive-timeout >= 15000" "check-alive-timeout=$GNOME_AT is less than 15000."
else
    print_ok "Desktop environment is not GNOME. Skip it."
fi

print_package $AG-swappiness10
SWAPPINESS=$(cat /proc/sys/vm/swappiness)
print_check "swappiness=$SWAPPINESS"
if [[ $SWAPPINESS -lt 10 ]] ; then
    print_warning "swappiness=$SWAPPINESS differs from recommended 10, but it is fine."
else
    [[ $SWAPPINESS -eq 10 ]]
    verdict $AG-swappiness10 $? "swappiness=10 in used" "swappiness=10 doesn't work."
fi

echo

if [[ "$ERROR_CHECK" == "1" ]] ; then
    print_warning "Not all tests were successful!\n It is possible that you did not restart your computer after installing the \"alt-gaming\" package...\n"
    exit 1
fi
