#!/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" ;}
fatal () { print_error "$@" && exit 1 ;}

check_installed () {
    if rpm -q "$1" &>/dev/null ; then
        echo -e "\n Package \"$1\" installed."
        return 0
    else
        echo -e "\n Package \"$1\" not installed. Skip it...\n"
        return 1
    fi
}

if check_installed $AG-esync ; then
    print_check "esync (ulimit -Hn=$(ulimit -Hn))"
    if ulimit -Hn | grep -q "524288"
    then print_ok "esync support is enabled."
    else print_error "esync support doesn\'t work."
    fi
fi

if check_installed $AG-ntsync ; then
    kernel_flavour=$(uname -r | awk -F. '{print $1"."$2}')
    if [[ ${kernel_flavour//.*/} -ge 6 ]] \
    && [[ ${kernel_flavour//*./} -ge 14 ]]
    then
        print_check "ntsync (/dev/ntsync is exist)"
        if [[ -e "/dev/ntsync" ]]
        then print_ok "ntsync support is enabled."
        else print_error "ntsync support doesn\'t work."
        fi
    else
        print_warning "ntsync supported only for kernel 6.14+. Skip it."
    fi
fi

if check_installed $AG-mm-count ; then
    print_check "vm.max_map_count=$(cat /proc/sys/vm/max_map_count)"
    if grep -q "2147483642" /proc/sys/vm/max_map_count
    then print_ok "Reduce operating system mmap restrictions is enabled"
    else print_error "Reduce operating system mmap restrictions doesn\'t work"
    fi
fi

if check_installed $AG-clearcpuid514 ; then
    print_check "clearcpuid=514 ($(grep "LINUX_DEFAULT" /etc/sysconfig/grub2))"
    if grep -i -q "umip" /proc/cpuinfo
    then print_error "UMIP is enabled (clearcpuid=514 doesn\'t work)."
    else print_ok "UMIP is disabled (clearcpuid=514 is working, or the flag is not supported)."
    fi
fi

if check_installed $AG-tcp-mtu-probing ; then
    print_check "net.ipv4.tcp_mtu_probing ($(cat /proc/sys/net/ipv4/tcp_mtu_probing))"
    if grep -q "1" /proc/sys/net/ipv4/tcp_mtu_probing
    then print_ok "net.ipv4.tcp_mtu_probing=1 in used"
    else print_error "net.ipv4.tcp_mtu_probing=1 doesn\'t work"
    fi
fi

if check_installed $AG-alive-timeout ; then
    print_check "GNOME check alive timeout"
    if [[ "${DESKTOP_SESSION}" =~ gnome ]] ; then
        GNOME_AT=$(gsettings get org.gnome.mutter check-alive-timeout | awk '{print $2}')
        if [[ $GNOME_AT -ge 15000 ]]
        then print_ok "check-alive-timout >= 15000"
        else print_error "$AG-alive-timeout doesn\'t work"
        fi
    else
        print_ok "Desktop environment is not GNOME. Skip it."
    fi
fi

if check_installed $AG-swappiness10 ; then
    print_check "change swappiness"
    if grep -q "10" /proc/sys/vm/swappiness
    then print_ok "swappiness=10 in used"
    else print_error "swappiness=10 doesn\'t work"
    fi
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"
fi
