#!/bin/bash

unsupported_php_versions=("ea-php5*" "ea-php7[0-1]")
php_versions_in_use=($(whmapi1 php_get_vhost_versions |grep 'version:' |grep -v 'version: 1' |uniq |awk '{print $2}'))
unsupported_versions=()
for version in "${php_versions_in_use[@]}"; do
    for pattern in "${unsupported_php_versions[@]}"; do
        if [[ "${version}" == ${pattern} ]]; then
            unsupported_versions+=("${version}")
        fi
    done
done

if [[ "${#unsupported_versions[@]}" -gt 0 ]]; then
    php_ok=false
else
    php_ok=true
fi

installed_packages=$(rpm -qa)
if echo "$installed_packages" | grep -q "mysql-community-server-5.7"; then
    db_ok=2
elif echo "$installed_packages" | grep -q "mysql-community-server-8.0"; then
    db_ok=true
elif echo "$installed_packages" | grep -Eq "MariaDB-server-10\.[3-6]"; then
    db_ok=true
elif echo "$installed_packages" | grep -Eq "MariaDB-server-10\.[0-2]"; then
    db_ok=2
else
    db_ok=false
fi

yum_repolist_enabled=$(yum -C repolist enabled 2>/dev/null || yum repolist enabled)
if echo $yum_repolist_enabled | grep -q "remi" | grep -v cpanel-remixicons; then
    remi_ok=false
else
    remi_ok=true
fi

jetbackup=$(jetbackup --version 2>/dev/null)
if echo "$jetbackup" | grep -qi "Jetbackup 4"; then
    jetbackup_ok=false
else
    jetbackup_ok=true
fi

# if centos, OK
if echo "$installed_packages" | grep -q "centos-release"; then
    os_ok=true
# if cloudlinux make sure we don't have mysql-governor in use
elif echo "$installed_packages" | grep -q "cloudlinux-release"; then
    if echo "$installed_packages" | grep -q "governor-mysql"; then
        os_ok=false
    else
        os_ok=true
    fi
else
    os_ok=false
fi

# check cpu model on dedis, currently amd dedis have issues with our qemu overlay methods
cpu_ok=true
#if ! [[ $(virt-what) ]]; then
#    if ! lscpu |grep -E '^Vendor ID:'|grep -q 'Intel'; then
#        cpu_ok=false
#    fi
#fi


if [[ "${php_ok}" == "true" ]] && [[ "${db_ok}" == "true" ]] && [[ "${remi_ok}" == "true" ]] && [[ "${jetbackup_ok}" == "true" ]] && [[ "${os_ok}" == "true" ]] && [[ "${cpu_ok}" == "true" ]]; then
    # moved file to /tmp for easier access across users, this is just to ensure we clean up the old file
    # then we moved it to /dev/shm so it cleans itself up during upgrades
    [[ -f /root/support/el8_upgrade_candidate ]] || rm -f /root/support/el8_upgrade_candidate
    [[ -f /tmp/el8_upgrade_candidate ]] || rm -f /tmp/el8_upgrade_candidate
    echo 1 > /dev/shm/el8_upgrade_candidate
elif [[ "${php_ok}" == "true" ]] && [[ "${db_ok}" == "2" ]] && [[ "${remi_ok}" == "true" ]] && [[ "${jetbackup_ok}" == "true" ]] && [[ "${os_ok}" == "true" ]] && [[ "${cpu_ok}" == "true" ]]; then
    # moved file to /tmp for easier access across users, this is just to ensure we clean up the old file
    # then we moved it to /dev/shm so it cleans itself up during upgrades
    [[ -f /root/support/el8_upgrade_candidate ]] || rm -f /root/support/el8_upgrade_candidate
    [[ -f /tmp/el8_upgrade_candidate ]] || rm -f /tmp/el8_upgrade_candidate
    echo 2 > /dev/shm/el8_upgrade_candidate
else
    # moved file to /tmp for easier access across users, this is just to ensure we clean up the old file
    # then we moved it to /dev/shm so it cleans itself up during upgrades
    [[ -f /root/support/el8_upgrade_candidate ]] || rm -f /root/support/el8_upgrade_candidate
    [[ -f /tmp/el8_upgrade_candidate ]] || rm -f /tmp/el8_upgrade_candidate
    [[ -f /dev/shm/el8_upgrade_candidate ]] || rm -f /dev/shm/el8_upgrade_candidate
fi
