#! /bin/bash

clear
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

BLOCK_WIDTH=62
BG_BLUE="\e[1;37;44m"
BG_RED="\e[1;37;41m"
BG_GREEN="\e[1;37;42m"
BG_YELLOW='\033[1;33m'
NC='\033[0m' # No Color
RESET="\e[0m"

#SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WGET_ARGS=""

#
# 
# Return: null
cleanup() {
    echo -ne "\e[0m"
    echo -ne "\e[r"
    tput cnorm
    tput cup $(tput lines) 0
    #echo -e "\nInstaller completed."
    #echo -e "\n"
}
trap cleanup EXIT INT TERM

#
# 
# Return: null
function set_license_brainycp() {
	ip_get=$(wget -t 4 --waitretry=3 --connect-timeout=10 --read-timeout=10 -qO- http://core.brainycp.com/ipchecker.php)
	linux_date=$(date '+%m.%d.%y')
	time_record=$(date '+%Y-%m-%d %H:%I:%S')
	kernel_info=$(uname -a)
	
	if [ -f /etc/redhat-release ]; then
		get_osname=$(cat /etc/redhat-release | awk '{print tolower($1)}')
		os_relase=$(cat /etc/redhat-release | grep -Eo '[0-9]' | awk '{print $1}' | head -n1)
		virtall="$(virt-what)"
	else
		get_osname=$(cat /etc/os-release | grep -E '^NAME' | cut -d '=' -f2 | sed 's/\"//g' | xargs)
		os_relase=$(cat /etc/os-release | grep -E '^VERSION_ID' | cut -d '=' -f2 | sed 's/\"//g')
		virtall=''
	fi
	virttypez="$(dmidecode -s system-product-name 2>/dev/stdout| awk '{print $1}')"
	virttypexen="$(dmidecode | grep -i domU 2>/dev/stdout)"
	virttypemic="$(dmidecode | egrep -i 'manufacturer|product' 2>/dev/stdout)"

	if [[ $virtall = *"xen"* ]]; then
			virttype="xen"
		elif [[ $virtall = "lxc" ]];then
			virttype="xlc"
		elif [[ $virttypemic = "Product Name: HVM domU" ]];then
			virttype="microsoftvirtpc"
		elif [[ $virttypexen = "Product Name: HVM domU" ]];then
			virttype="xen"
		elif [[ $virttypez = "/dev/mem:" ]];then
			virttype="openvz"
		elif [[ $virttypez = "KVM" ]];then
			virttype="kvm"
		elif [[ $virttypez = "VMware" ]];then
			virttype="wmware"
		elif [[ $virttypez = "VirtualBox" ]];then
			virttype="virtualbox"
		elif [[ $virttypez = "Bochs" ]];then
			virttype="Bochs"
		else 
			virttype="baremetal"
	fi
	
	#for var in 'http://panelname.shn-host.ru/check.php'
	for var in 'http://panelname.shn-host.ru/check.php' 'http://panelbr1.shn-host.ru/check.php' 'http://panelbr1u.s-host.net/check.php' 'http://panelbr2.shn-host.ru/check.php'
	do
		lic_mega_salt='kgj475yLH&I#@FJK!@J%Y#(*U&XDVJUS)(**U&)$*(U%()IJKDOVJOIU!@&*^$&*@HCG@YT&*DCSI@$$*()UODIVJMNKLJIkjhlkgjoi34uy5hiosdnjkljh8Y#*(HEU$^()*U@()&*($^Y(U*HDFjoh23i94ui5y9u8hgjkodfjh438yYGR&*9TYFBKL!&@Y'
		hash_license=$(echo "$ip_get$linux_date$lic_mega_salt$ip_get$linux_date" | openssl dgst -sha512 | awk '{print $2}')
		curl -X POST --connect-timeout 15 -d "ip_user=$ip_get&operation=install_softvare_license&date=$linux_date&paramin=$hash_license&time_record=$time_record&virttype=$virttype&get_osname=$get_osname&get_osver=$os_relase&kernel_info=$kernel_info" $var &>/dev/null
	done
}

#
# Terminates the program if it is not run with root privileges
# Return: null
assert_run_as_root() {
    if [[ $(id -u) -ne 0 ]]; then
        echo "This the script must be run as root"
        exit 2
    fi
}

# Reads a variable value from /etc/os-release
# Return: release name
get_os_release_version() {
    local -r var="${1}"
    local val
    if ! val="$(cat /etc/os-release 2>/dev/null | grep -i "^${var}" | cut -f 2 -d "=" | xargs)"; then
        echo "Error: ${var} is not found in /etc/os-release" >&2
        exit 1
    fi
    echo "${val}"
}

# Cheking OS version
# Return: null
check_os_version() {
    local -r os_type="${1}"
    if [[ "${os_type}" != "centos" ]] && [[ "${os_type}" != "ubuntu" ]] && [[ "${os_type}" != "almalinux" ]] && [[ "${os_type}" != "rocky" ]]
    then
        echo "Your system '${os_type}' is not supported."
        exit 1
    fi
}

# Cheking Systemd resolved
# Return: null
check_systemd_resolved() {
    SERVICE="systemd-resolved"

    # Check if the service is currently running
    if systemctl is-active --quiet "$SERVICE"; then
	echo "Active $SERVICE detected. Disabling it for DNS stability..."

	# Backup current DNS entries from the original file to maintain connectivity
	# Extract all 'nameserver' lines, excluding the local stub 127.0.0.53
	CURRENT_DNS=$(grep "^nameserver" /etc/resolv.conf | grep -v "127.0.0.53")

	# Stop and completely disable the service
        systemctl stop "$SERVICE"
        systemctl disable "$SERVICE" >/dev/null 2>&1

        # Remove the old symbolic link (it becomes broken after the service stops)
        rm -f /etc/resolv.conf

	# Create a new static resolv.conf file
	echo "nameserver 8.8.8.8" > /etc/resolv.conf
	echo "nameserver 1.1.1.1" >> /etc/resolv.conf
	if [ -n "$CURRENT_DNS" ]; then
		echo "$CURRENT_DNS" >> /etc/resolv.conf
        fi

	echo -e "Service stopped. DNS switched to static mode (8.8.8.8 prioritized)."
    else
	echo -e "Service $SERVICE is already inactive or not installed. Skipping."
    fi
}

#
# 
# Return: null
setup_screen_full() {
    clear
    tput civis
    local os_name="Unknown"
    local os_ver=""
    if [ -f /etc/os-release ]; then
        os_name=$(grep -E '^NAME=' /etc/os-release | cut -d'=' -f2 | sed 's/"//g' | awk '{print $1}')
        os_ver=$(grep -E '^VERSION_ID=' /etc/os-release | cut -d'=' -f2 | sed 's/"//g')
    fi
    local os_full_info="${os_name} ${os_ver}"
    local term_width=$(tput cols)
    local term_lines=$(tput lines)
    local header_height=5
    local block_width=50
    local padding=$(( (term_width - block_width) / 2 ))
    [ $padding -lt 2 ] && padding=2
    echo -ne "\e[$((header_height + 1));${term_lines}r"
    tput cup 0 0
    for i in $(seq 0 $((header_height - 1))); do
        printf "\e[1;37;44m%${term_width}s\e[0m" " "
        [ $i -lt $((header_height - 1)) ] && echo ""
    done
    tput cup 0 1
    printf "\e[1;37;44m%s\e[0m" "$os_full_info"
    tput cup 1 $padding
    printf "\e[1;37;44m%-50s\e[0m" "BRAINYCP INSTALLER - VERSION 4.0"
    tput cup 2 $padding
    printf "\e[1;37;44mStatus: %-42s\e[0m" "${current_status:-Initializing...}"
    tput cup 3 $padding
    printf "\e[1;37;44mLog file: %-42s\e[0m" "install.log"
    tput cup $header_height 0
    echo -ne "\e[0m"
}

#
# 
# Return: null
reset_screen_full() {
    echo -ne "\e[0m"
    echo -ne "\e[r"
    tput cnorm
    clear
    echo "Installation completed successfully!"
}

#
# 
# Return: null
setup_screen() {
    clear
    echo -ne "\e[6;r"
    tput cup 0 0
    # \e[1;37;44m - (1),  (37),  (44)
    # %-62s -     62
    printf "\e[1;37;44m%62s\e[0m\n" " "
    printf "\e[1;37;44m  %-60s\e[0m\n" "BRAINYCP INSTALLER - VERSION 4.0"
    printf "\e[1;37;44m  %-60s\e[0m\n" "Status: Installing dependencies..."
    printf "\e[1;37;44m  %-60s\e[0m\n" "Log file: _core_loader.log"
    printf "\e[1;37;44m%62s\e[0m\n" " "

    echo -ne "\e[0m"
    tput cup 5 0
}

#
# 
# Return: null
reset_screen() {
    echo -ne "\e[0m"
    echo -ne "\e[r"
    tput cnorm
    tput cup $(tput lines) 0
    #clear
    #echo "Installation completed!"
}

#
# 
# Return: null
set_status() {
    local term_width=$(tput cols)
    local block_width=50
    local padding=$(( (term_width - block_width) / 2 ))

    tput sc
    tput cup 2 $padding
    #printf "${BG_BLUE}Status: %-$((BLOCK_WIDTH-8))s${RESET}" "$1"
    printf "${BG_BLUE}Status: %-42s${RESET}" "$1"
    tput rc
}

#
# 
# Return: null
set_error() {
    local os_name="Unknown"
    local os_ver=""
    if [ -f /etc/os-release ]; then
        os_name=$(grep -E '^NAME=' /etc/os-release | cut -d'=' -f2 | sed 's/"//g' | awk '{print $1}')
        os_ver=$(grep -E '^VERSION_ID=' /etc/os-release | cut -d'=' -f2 | sed 's/"//g')
    fi
    local os_full_info="${os_name} ${os_ver}"

    local term_width=$(tput cols)
    local header_height=5
    local block_width=50
    local padding=$(( (term_width - block_width) / 2 ))
    [ $padding -lt 2 ] && padding=2
    local RED_BG="\e[1;37;41m"
    local RESET="\e[0m"

    tput sc
    tput cup 0 0
    for i in $(seq 0 $((header_height - 1))); do
        printf "${RED_BG}%${term_width}s${RESET}" " "
        [ $i -lt $((header_height - 1)) ] && echo ""
    done
    tput cup 0 1
    printf "${RED_BG}%s${RESET}" "$os_full_info"
    tput cup 1 $padding
    printf "${RED_BG}%-50s${RESET}" "BRAINYCP INSTALLER - CRITICAL ERROR"
    tput cup 2 $padding
    printf "${RED_BG}Error: %-43s${RESET}" "$1"
    tput cup 3 $padding
    printf "${RED_BG}Check logs: install.log for details${RESET}"
    tput rc
    echo -e "\n\e[1;31m[CRITICAL ERROR] $1\e[0m"
    tput cnorm
}

#
# 
# Return: null
welcom_screen() {
    local term_width=$(tput cols)
    tput sc
    tput cup 0 0
    for i in {0..4}; do
        printf "${BG_GREEN}%${term_width}s${RESET}\n" " "
    done

    local text="WELCOME TO BRAINYCP"
    local text_pad=$(( (term_width - ${#text}) / 2 ))
    tput cup 2 $text_pad
    printf "${BG_GREEN}%s${RESET}" "$text"

    tput rc
    echo -e "\n\e[1;32m[SUCCESS] Installation finished successfully!\e[0m"
}

#
# 
# Return: true/fslse
download_file() {
    local -r DEST_PATH="${1}"
    local -r LOG_FILE="${2}"
    local SUB_DIR="${3}"
    local FILE_NAME=$(basename "${DEST_PATH}")
    local DEST_DIR=$(dirname "${DEST_PATH}")
    local URLS_CORE=("http://core.brainycp.com/" "http://repubra.netxi.in/centos/core_tools/" "http://176.117.78.72/centos/core_tools/")
    local _SUCCESS="no"
    local -r DEBUG="no" # yes/no

    rm -f "${DEST_PATH}" &>/dev/null
    mkdir -p "${DEST_DIR}" &>/dev/null
    if [[ "${DEBUG}" == "yes" ]];then
    echo -e "\n${BG_YELLOW}[DEBUG]${NC} DEST_DIR: ${DEST_DIR}"
    fi

    for BASE_URL in "${URLS_CORE[@]}"; do
        local CLEAN_BASE=$(echo "${BASE_URL}" | sed 's#/*$##')
        local CLEAN_SUB=$(echo "${SUB_DIR}" | sed 's#^/*##;s#/*$##')

        if [[ -z "${CLEAN_SUB}" ]]; then
            FULL_URL="${CLEAN_BASE}/${FILE_NAME}"
        else
            FULL_URL="${CLEAN_BASE}/${CLEAN_SUB}/${FILE_NAME}"
        fi

	if [[ "${DEBUG}" == "yes" ]];then
	    echo -e "\n${BG_YELLOW}[DEBUG]${NC} wget: -q -T 10 -t 1 ${FULL_URL} -O ${DEST_PATH} -a ${LOG_FILE}"
	fi
        wget -T 10 -t 1 "${FULL_URL}" -O "${DEST_PATH}" -a "${LOG_FILE}"
        if [[ $? -eq 0 ]] && [[ -s "${DEST_PATH}" ]]; then
            _SUCCESS="yes"
	    if [[ "${DEBUG}" == "yes" ]];then
		echo -e "\n${BG_YELLOW}[DEBUG]${NC} SUCCESS: ${_SUCCESS}"
	    fi
            break
        fi
        rm -f "${DEST_PATH}" &>/dev/null
    done

    if [[ "${_SUCCESS}" == "yes" ]]; then
        return 0
    else
        return 1
    fi
}

run_remote_script() {
    local -r SCRIPT_NAME="${1}"
    local -r SUB_DIR="${2}"
    local -r URLS_CORE=(
        "http://core.brainycp.com/" 
        "http://repubra.netxi.in/centos/core_tools/" 
        "http://176.117.78.72/centos/core_tools/"
    )

    local _RESULT=""
    local _SUCCESS="no"

    for BASE_URL in "${URLS_CORE[@]}"; do
        local CLEAN_BASE=$(echo "${BASE_URL}" | sed 's#/*$##')
        local CLEAN_SUB=$(echo "${SUB_DIR}" | sed 's#^/*##;s#/*$##')

        if [[ -z "${CLEAN_SUB}" ]]; then
            FULL_URL="${CLEAN_BASE}/${SCRIPT_NAME}"
        else
            FULL_URL="${CLEAN_BASE}/${CLEAN_SUB}/${SCRIPT_NAME}"
        fi

	#echo -e "\n${BG_YELLOW}[DEBUG]${NC} wget: wget: -qO- ${FULL_URL}" >&2
	_RESULT=$(wget -q -T 5 -t 1 -O- "${FULL_URL}")
	#echo -e "\n${BG_YELLOW}[DEBUG]${NC} _RESULT: ${_RESULT}"
        if [[ $? -eq 0 ]] && [[ -n "${_RESULT}" ]]; then
            echo "${_RESULT}"
            _SUCCESS="yes"
            break
        fi
    done

    if [[ "${_SUCCESS}" == "yes" ]]; then
        return 0
    else
        return 1
    fi
}

validate_remote_url() {
    local -r FILE_NAME="${1}"
    local -r SUB_DIR="${2}"
    local -r URLS_CORE=(
        "http://core.brainycp.com/"
        "http://repubra.netxi.in/centos/core_tools/"
        "http://176.117.78.72/centos/core_tools/"
    )
    local _SUCCESS="false"

    for BASE_URL in "${URLS_CORE[@]}"; do
        local CLEAN_BASE=$(echo "${BASE_URL}" | sed 's#/*$##')
        local CLEAN_SUB=$(echo "${SUB_DIR}" | sed 's#^/*##;s#/*$##')

        if [[ -z "${CLEAN_SUB}" ]]; then
            FULL_URL="${CLEAN_BASE}/${FILE_NAME}"
        else
            FULL_URL="${CLEAN_BASE}/${CLEAN_SUB}/${FILE_NAME}"
        fi

        # 200 или 301/302 redirect
	#echo -e "\n${BG_YELLOW}[DEBUG]${NC} wget: -q -S --spider --timeout=5 --tries=1 ${FULL_URL}" >&2
        if wget -q -S --spider --timeout=5 --tries=1 "${FULL_URL}" 2>&1 | grep -qE 'HTTP/.* (200 OK|301|302)'; then
            _SUCCESS="true"
	    #echo -e "\n${BG_YELLOW}[DEBUG]${NC} _SUCCESS: ${_SUCCESS}" >&2
            break
        fi
    done

    echo "${_SUCCESS}"
}

function EXIT_SUCCESS(){
    echo "Press any key to continue."
    read -s -n 1
    reset_screen
    exit 0
}

function EXIT_ERROR(){
  echo ""
  echo "Press any key to continue."
  read -s -n 1
  reset_screen
  exit 1
}

function ECHO_OK(){
  echo -e "\033[1;32m [OK] \033[0m";tput sgr0
}
function ECHO_ERROR(){
  echo -e "\033[1;31m [ERROR] \033[0m";tput sgr0
}
function ECHO_DONE(){
  echo -e "\033[1;32m [DONE] \033[0m";tput sgr0
}

function init_quota() {
    cat << 'EOF' > /lib/systemd/system/brainy-quota.service
[Unit]
Description=BrainyCP Automated Disk Quota Initialization
DefaultDependencies=no
After=local-fs.target
#Before=brainyphp8-fpm.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/sbin/quotacheck -avugm -f
ExecStart=/usr/sbin/quotaon -vug /

# Логгер
ExecStopPost=/bin/bash -c 'if [ "$SERVICE_RESULT" != "success" ]; then \
    mkdir -p /var/log/brainy/; \
    echo "==================================================================" >> /var/log/brainy/quota_crash.log; \
    echo "[$(date \"+%Y-%m-%d %H:%M:%S\")] Критический сбой инициализации квот у ядра " >> /var/log/brainy/quota_crash.log; \
    echo "Статус (SERVICE_RESULT): $SERVICE_RESULT" >> /var/log/brainy/quota_crash.log; \
    echo "Код процесса (EXIT_CODE): $EXIT_CODE" >> /var/log/brainy/quota_crash.log; \
    echo "Системный статус (EXIT_STATUS): $EXIT_STATUS" >> /var/log/brainy/quota_crash.log; \
    echo "Текущий статус ядра:" >> /var/log/brainy/quota_crash.log; \
    /usr/sbin/quotaon -pa >> /var/log/brainy/quota_crash.log 2>&1; \
    echo "==================================================================" >> /var/log/brainy/quota_crash.log; \
else \
    # Запуск успешен
    rm -f /var/log/brainy/quota_crash.log; \
fi'

[Install]
WantedBy=multi-user.target
EOF

  systemctl daemon-reload  &>/dev/null
  systemctl enable brainy-quota.service &>/dev/null
}

config_fstab_quota() {
    local fstab_path="/etc/fstab"

    /bin/cp -p "$fstab_path" "${fstab_path}.bak"

    # Check
    if grep -q "usrquota" "$fstab_path" && grep -q "grpquota" "$fstab_path"; then
        return 0
    fi

    awk '
    $2 == "/" && $3 ~ /^ext/ && $4 ~ /defaults/ {
        sub(/defaults/, "defaults,usrquota,grpquota", $4)
    }
    { print }
    ' "$fstab_path" > "${fstab_path}.tmp"

    # Перезаписываем оригинальный файл
    mv "${fstab_path}.tmp" "$fstab_path"
    chmod 0644 "$fstab_path"

    echo -en "Check fstab... "
    if ! mount -fa 2>&1 | grep -q .; then
        # reload systemd
        systemctl daemon-reload  &>/dev/null
        mount -o remount /
        echo "[check pass]"
    else
        cp -p "${fstab_path}.bak" "$fstab_path"
        systemctl daemon-reload &>/dev/null
        echo "[ERROR]: Restore fstab"
        return 1
    fi

    echo -en "Check remaunt... "
    systemctl daemon-reload  &>/dev/null
    mount -o remount /

    # Проверяем, приняло ли ядро AlmaLinux новые флаги монтирования в памяти
    if mount | grep ' / ' | grep -q "quota"; then
        echo "[pass]"
    else
        echo "[warning]"
    fi
}

#
if [[ -f /usr/share/zoneinfo/Europe/Kiev && ! -f /usr/share/zoneinfo/Europe/Kyiv ]]; then
    ln -s /usr/share/zoneinfo/Europe/Kiev /usr/share/zoneinfo/Europe/Kyiv
elif [[ -f /usr/share/zoneinfo/Europe/Kyiv && ! -f /usr/share/zoneinfo/Europe/Kiev ]]; then
    ln -s /usr/share/zoneinfo/Europe/Kyiv /usr/share/zoneinfo/Europe/Kiev
fi


###################################################
###############################################
#
#    main
###
os_type="$(get_os_release_version 'ID=')"
if [[ "${os_type}" == "ubuntu" ]] || [[ "${os_type}" == "debian" ]];then
setup_screen_full
fi
if [[ "${os_type}" == "almalinux" ]];then
setup_screen_full
fi

echo -e "Please Wait... "
d=$(dirname $0)
assert_run_as_root
#os_type="$(get_os_release_version 'ID=')"
os_version="$(get_os_release_version 'VERSION_ID=')"
check_systemd_resolved
timedatectl set-timezone Europe/Kyiv

# Check resolf.conf
[ "$(head -n 1 /etc/resolv.conf)" == "nameserver 8.8.8.8" ] || sed -i '1i nameserver 8.8.8.8' /etc/resolv.conf

if ! [ -d /var/brainycp/ ]; then
	echo -en "Loading auxiliary packages... "
	if [[ "${os_type}" == "ubuntu" ]] || [[ "${os_type}" == "debian" ]];then
	    apt-get update  &>/dev/null
	    apt-get -y install apt &>/dev/null
	    apt-get -y install virt-what dmidecode &>_prepare.log
	    apt-get -y install packagekit &>_prepare.log
	else
	    yum -y install virt-what dmidecode &>_prepare.log
	fi
	if [ $? -eq 0 ]; then
            echo -en "\033[1;32m [OK] \033[0m\n";tput sgr0
	else
            echo -e "\033[1;31m [ERROR] \033[0m\n";tput sgr0
            cat _prepare.log
            echo ''
            exit 1
	fi

	echo -en "Load metadata... "
	set_license_brainycp "${os_type}" "${os_version}"
	echo "DONE"
fi

case "${os_type}" in
"ubuntu")
	echo 'Acquire::http::Timeout "60";' | tee /etc/apt/apt.conf.d/99timeout &> /dev/null
	echo 'Acquire::ftp::Timeout "60";' | tee -a /etc/apt/apt.conf.d/99timeout &> /dev/null
	echo 'Acquire::Retries "5";' | tee -a /etc/apt/apt.conf.d/99timeout &> /dev/null

	echo 'Acquire::http::No-Cache "true";' | tee /etc/apt/apt.conf.d/20auto-upgrades &> /dev/null
	echo 'Acquire::http::Pipeline-Depth "0";' | tee -a /etc/apt/apt.conf.d/20auto-upgrades &> /dev/null
	echo 'Acquire::BrokenProxy "true";' | tee -a /etc/apt/apt.conf.d/20auto-upgrades &> /dev/null

	# Initializing parameters for the package manager
	#echo 'Acquire::http::Timeout "5";' | tee /etc/apt/apt.conf.d/99timeout &> /dev/null
	#echo 'Acquire::Retries "2";' | tee -a /etc/apt/apt.conf.d/99timeout &> /dev/null
	
	#echo 'Acquire::http::No-Cache "true";' | tee /etc/apt/apt.conf.d/20auto-upgrades &> /dev/null
	#echo 'Acquire::http::Pipeline-Depth "0";' | tee -a /etc/apt/apt.conf.d/20auto-upgrades &> /dev/null
	#echo 'Acquire::BrokenProxy "true";' | tee -a /etc/apt/apt.conf.d/20auto-upgrades &> /dev/null

	# Get KEY_REPO
	KEY_REPO="/usr/share/keyrings/brainy-repo.gpg"
	cat <<'EOF' | gpg --dearmor --batch --yes -o "$KEY_REPO"
-----BEGIN PGP PUBLIC KEY BLOCK-----

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
IYP9g+NFwpv8c81qRKxh1Fw/M7THp0d+rA==
=+Ona
-----END PGP PUBLIC KEY BLOCK-----
EOF

	if [ $? -eq 0 ]; then
		echo ""
	else
	        echo -e "ERROR: Key installation failed"
		EXIT_ERROR
	fi

	echo -en "Updating information about available repositories... "
	#apt-get update  &>/dev/null
	apt-get update
	if [ $? -eq 0 ]; then
	    ECHO_OK
	else
	    ECHO_ERROR
	    EXIT_ERROR
	fi
	systemctl stop unattended-upgrades &>/dev/null
	systemctl disable unattended-upgrades &>/dev/null
	systemctl mask unattended-upgrades &>/dev/null

	systemctl stop packagekit &>/dev/null
	systemctl disable packagekit &>/dev/null
	systemctl mask packagekit &>/dev/null

	if [ "${os_version}" == "20.04" ];then
                echo 'System 20.04'
                echo -e "Updating of a system. It can take several minutes..."
                sleep 1
		#DEBIAN_FRONTEND=noninteractive UCF_FORCE_CONFFOLD=1 apt-get -y upgrade -o Dpkg::Options::="--force-confold" -o Dpkg::Use-Pty=0 -o APT::Color="0" -qq | cat
		DEBIAN_FRONTEND=noninteractive UCF_FORCE_CONFFOLD=1 apt-get -y upgrade -o Dpkg::Options::="--force-confold" -o Dpkg::Use-Pty=0 -o APT::Color="0"
		if [ $? -eq 0 ]; then
		    ECHO_OK
		else
		    ECHO_ERROR
		    EXIT_ERROR
		fi

		set_status "Downloading install script... "
		echo -en "Loading of the installer... "
		if download_file "_installUbuntu20_8.sh" "_core_loader.log"; then
			ECHO_OK
			set_status "Downloading install script... [ok]"
		else
			ECHO_ERROR
			set_error "Download failed: $SRC_FILE"
			echo "There was an error in loading process of the installer."
			cat _core_loader.log
			ECHO_ERROR
			EXIT_ERROR
		fi

		sleep 3
		sed -i '\#mirror+file:/etc/apt/#d' /etc/apt/sources.list
		. ${d}/_installUbuntu20_8.sh

		touch ${d}/install_ubuntu_20v8.txt
		brainy-core -s auto &>brainy_core.log

		apt-get -y install zip &>/dev/null
		chmod 0750 /usr/local/brainycp/ssh/*.php
		chmod 0750 /usr/local/brainycp/ssh/*.sh
		welcom_screen
		echo -e "Installation complete"
		EXIT_SUCCESS

        elif [[ "${os_version}" == "22.04" ]] || [[ "${os_version}" == "22.10" ]];then
                echo 'System 22.04'
                echo -e "Updating of a system. It can take several minutes..."
                sleep 1
		#DEBIAN_FRONTEND=noninteractive UCF_FORCE_CONFFOLD=1 COLUMNS=40 apt-get -y upgrade -qq -o Dpkg::Progress-Fancy="1" -o APT::Color="0"
		#DEBIAN_FRONTEND=noninteractive UCF_FORCE_CONFFOLD=1 apt-get -y upgrade -o Dpkg::Options::="--force-confold" -o Dpkg::Use-Pty=0 -o APT::Color="0" -qq | cat
		DEBIAN_FRONTEND=noninteractive UCF_FORCE_CONFFOLD=1 apt-get -y upgrade -o Dpkg::Options::="--force-confold" -o Dpkg::Use-Pty=0 -o APT::Color="0"
		if [ $? -eq 0 ]; then
		    ECHO_OK
		else
		    ECHO_ERROR
		    EXIT_ERROR
		fi

		set_status "Downloading install script... "
		echo -en "Loading of the installer... "
		if download_file "_installUbuntu22_8.sh" "_core_loader.log"; then
		    ECHO_OK
		    set_status "Downloading install script... [ok]"
		else
		    ECHO_ERROR
		    set_error "Download failed: $SRC_FILE"
		    echo "There was an error in loading process of the installer."
		    cat _core_loader.log
		    EXIT_ERROR
		fi

#		chmod 0700 ${d}/_installUbuntu_22
#		${d}/_installUbuntu_22 "${1}"

		# Run child script
		sleep 3
		sed -i '\#mirror+file:/etc/apt/#d' /etc/apt/sources.list

		. ${d}/_installUbuntu22_8.sh
#		if [ $? -ne 0 ];then
#		    /bin/rm -f ${d}/_installUbuntu_22 &>/dev/null
#		    echo "There was an error at installation. Installation process was stopped."
#		    exit 1
#		fi

#		/bin/rm -f ${d}/_installUbuntu_22 &>/dev/null
#		touch ${d}/install_ubuntu_22.txt
		touch ${d}/install_ubuntu_22v8.txt

		brainy-core -s auto &>brainy_core.log
		apt-get -y install zip &>/dev/null
		chmod 0750 /usr/local/brainycp/ssh/*.php
		chmod 0750 /usr/local/brainycp/ssh/*.sh
		welcom_screen
		echo -e "Installation complete"
		EXIT_SUCCESS

        elif [[ "${os_version}" == "24.04" ]];then
                echo 'System 24.04'
                echo -e "Updating of a system. It can take several minutes..."
                sleep 1
		#DEBIAN_FRONTEND=noninteractive UCF_FORCE_CONFFOLD=1 apt-get -y upgrade -o Dpkg::Options::="--force-confold" -o Dpkg::Use-Pty=0 -o APT::Color="0" -qq | cat
		DEBIAN_FRONTEND=noninteractive UCF_FORCE_CONFFOLD=1 apt-get -y upgrade -o Dpkg::Options::="--force-confold" -o Dpkg::Use-Pty=0 -o APT::Color="0"
		if [ $? -eq 0 ]; then
		    ECHO_OK
		else
		    ECHO_ERROR
		    EXIT_ERROR
		fi

		set_status "Downloading install script... "
		echo -en "Loading of the installer... "
		if download_file "_installUbuntu24_8.sh" "_core_loader.log"; then
		    ECHO_OK
		    set_status "Downloading install script... [ok]"
		else
		    ECHO_ERROR
		    set_error "Download failed: $SRC_FILE"
		    echo "There was an error in loading process of the installer."
		    cat _core_loader.log
		    EXIT_ERROR
		fi

#		chmod 0700 ${d}/_installUbuntu_24
#		${d}/_installUbuntu_24 "${1}"

		# Run child script
		sleep 3
		sed -i '\#mirror+file:/etc/apt/#d' /etc/apt/sources.list
		. ${d}/_installUbuntu24_8.sh

		brainy-core -s auto &>brainy_core.log
		apt-get -y install zip &>/dev/null
		chmod 0750 /usr/local/brainycp/ssh/*.php
		chmod 0750 /usr/local/brainycp/ssh/*.sh
		welcom_screen
		echo -e "Installation complete"
		EXIT_SUCCESS
	else
		echo "Error: your os version Ubuntu ${os_version} is not supported!"
		EXIT_ERROR
        fi
        exit 1
        ;;
"debian")
        if [ "${os_version}" == "10" ];then
                echo 'System Debian 10'
                echo "Please wait... "
                apt-get --allow-releaseinfo-change update &>/dev/null
                apt-get update &>/dev/null
                apt-get install -y --force-yes wget &>/dev/null
                echo -en "Detected Download install script... "
                wget -t 4 --waitretry=3 --connect-timeout=10 --read-timeout=10 http://core.brainycp.ru/_installDebian10.sh  &>/dev/null
                if [ $? -eq 0 ]; then
                        echo -en "\033[1;32m [OK] \033[0m\n";tput sgr0
                else
                        echo -e "\033[1;31m [ERROR] \033[0m\n";tput sgr0
                        exit 1
                fi
                ##/usr/bin/bash ${d}/_installDebian10.sh "$1"
		. ${d}/_installDebian10.sh
        else
                echo "Error: your os version Debian ${os_version} is not supported!"
        fi

        exit 0
        ;;
"centos")
        if [[ "${os_version}" == "7" ]] || [[ "${os_version}" == "8" ]];then
                echo ''
                echo -e "Your version of the CentOS ${os_version} system is not supported!"
                echo -e "Please use AlmaLinux 8 or 9"
                exit 1
        fi

        echo "Error: Your version of the CentOS ${os_version} system is not supported!"
        exit 1
        ;;
"almalinux")
        if [[ "${os_version}" == "10.2" ]];then
                echo ""
                echo -en "Updating of scripts for AlmaLinux ${os_version}... "
                #rpm -e --nodeps centos-repos  &>/dev/null
                #rpm -e --nodeps centos-linux-repos  &>/dev/null
		rpm -Uv --replacepkgs http://core.brainycp.com/repo/repo-brainy-v8b-10.0-1.brainy.el10.noarch.rpm  &>/dev/null
                if [ $? -eq 0 ]; then
                        echo -en "\033[1;32m [OK] \033[0m\n";tput sgr0
                else
                        echo -e "\033[1;31m [ERROR] \033[0m\n";tput sgr0
                        exit 1
                fi
		yum clean all &>/dev/null

                echo -en "Updating of tools for the virtulny environment... "
		yum -y install virt-what dmidecode &>/dev/null
		yum -y install openssh-server &>/dev/null
                if [ $? -eq 0 ]; then
                        echo -en "\033[1;32m [OK] \033[0m\n";tput sgr0
                else
                        echo -e "\033[1;31m [ERROR] \033[0m\n";tput sgr0
                        exit 1
                fi
        elif [[ "${os_version}" == "10.0" || "${os_version}" == "10.1" ]];then
                echo ""
                echo -e "THE INSTALLER OF BRAINYCP ISSUED THE REPORT"
                echo -e "============================================="
                echo -e "Attention! Your version of AlmaLinux ${os_version} became outdated!"
                echo -e "Please, update to the last release by means of the commands shown below:"
                echo -e ""
                echo -e "   1) yum clean all && yum -y update"
                echo -e "   2) reboot"
                echo -e ""
                echo -e "After updating your system, run the installation script again.\n"
                exit 1
        elif [[ "${os_version}" == "9.8" ]] || [[ "${os_version}" == "9.9" ]];then
                echo "AlmaLinux 9 not supported!"
                exit 1
        elif [[ "${os_version}" == "9.0" ]] || [[ "${os_version}" == "9.1" ]] || [[ "${os_version}" == "9.2" ]] || [[ "${os_version}" == "9.3" ]] || [[ "${os_version}" == "9.4" ]] || [[ "${os_version}" == "9.5" ]] || [[ "${os_version}" == "9.6" ]] || [[ "${os_version}" == "9.7" ]];then
                echo "AlmaLinux 9 not supported!"
                exit 1
        elif [[ "${os_version}" == "8.0" ]] || [[ "${os_version}" == "8.1" ]] || [[ "${os_version}" == "8.2" ]] || [[ "${os_version}" == "8.6" ]] || [[ "${os_version}" == "8.7" ]];then
                echo "AlmaLinux 8 not supported!"
                exit 1
        elif [[ "${os_version}" == "8.8" ]] || [[ "${os_version}" == "8.9" ]] || [[ "${os_version}" == "8.10" ]];then
                echo "AlmaLinux 8 not supported!"
                exit 1
#        else
#                echo ''
#                echo -e "Your version ${os_version} is not supported!"
#                exit 1
        fi
        ;;
"rocky")
        if [[ "${os_version}" == "9.0" ]] || [[ "${os_version}" == "9.1" ]] || [[ "${os_version}" == "9.2" ]] || [[ "${os_version}" == "9.3" ]] || [[ "${os_version}" == "9.4" ]] || [[ "${os_version}" == "9.5" ]];then
                echo ''
                echo -en "Update repo scripts for Rocky Linux 9... "
		rpm -Uv --replacepkgs http://core.brainycp.com/repo/alma-brainy-repo-v3-9.0-2.el9.noarch.rpm  &>/dev/null
                if [ $? -eq 0 ]; then
                        echo -en "\033[1;32m [OK] \033[0m\n";tput sgr0
                else
                        echo -e "\033[1;31m [ERROR] \033[0m\n";tput sgr0
                        exit 1
                fi
		yum clean all &>/dev/null

                echo -en "Update virtual tools... "
		yum -y install virt-what dmidecode &>/dev/null
                if [ $? -eq 0 ]; then
                        echo -en "\033[1;32m [OK] \033[0m\n";tput sgr0
                else
                        echo -e "\033[1;31m [ERROR] \033[0m\n";tput sgr0
                        exit 1
                fi
        else
                echo ''
                echo -e "Your version Rocky Linux ${os_version} is not supported!"
                exit 1
        fi
        ;;
esac

## FIXME: test
k=`uname -r`
if [[ "${k}" == *".vz7."* ]]; then
    echo "Detected your kernel = 3.10.xx.vz7 Please, install panel for OPENVZ Ver.3 as shown below."
    echo -e "\n\033[1;37myum clean all && yum install -y wget && wget http://core.brainycp.ru/openvz3.sh && bash ./openvz3.sh\033[0m\n";tput sgr0
    vopenvz="yes"
    exit
fi

get_osname_org=`cat /etc/redhat-release | awk '{print $1}'`
v8=`cat /etc/redhat-release | grep -oE '[0-9.]'| sed ':a;N;$!ba;s/\n//g' | cut -c 1`
v10=`cat /etc/redhat-release | grep -oE '[0-9.]'| sed ':a;N;$!ba;s/\n//g' | cut -f1 -d '.'`
srvname=`hostname`
srvip=`hostname -I`
default_lang="ru"

dip=`ip a s | grep inet | grep dynamic | xargs`
TOTALFILE="/proc/meminfo"
if [ -f $TOTALFILE ]; then
    memtotal=`cat /proc/meminfo | grep MemTotal: | xargs | cut -f2 -d' '`
    swaptotal=`cat /proc/meminfo | grep SwapTotal: | xargs | cut -f2 -d' '`
fi
if [[ "$v8" == "8" ]];then
  echo "";
  rpm -Uv --replacepkgs http://brainyrepo1.brainycp.com/centos/8_brainy_v3/x86_64/brainy-config-0.1b-1.brainy.el8.x86_64.rpm &>/dev/null
  export LANG=koi8-r
elif [[ "$v8" == "9" ]];then
  echo "";
  rpm -Uv --replacepkgs http://brainyrepo1.brainycp.com/centos/8_brainy_v3/x86_64/brainy-config-0.1b-1.brainy.el8.x86_64.rpm &>/dev/null
  export LANG=koi8-r
else
  echo "";
  rpm -Uv --replacepkgs http://brainyrepo1.brainycp.com/centos/7/x86_64/brainy-config-0.1b-1.brainy.el7.x86_64.rpm &>/dev/null
  export LANG=koi8-r
fi

#/usr/bin/brnconfig
#if [ $? -eq 0 ];then
#  echo "Abort installation."
#  exit 0
#fi

# Run process
#clear
#echo "Continued installation..."

osRhl="no"
osDebian="no"
osUbuntu="no"
vopenvz="no"

# Check that we are root ... so non-root users stop here
if [ "$EUID" -ne 0 ]
  then echo "Please run as root. Abort."
  exit
fi

#
# Check kernel version
#
k=`uname -r`
if [ "${k:0:2}" == "2." ]; then
    echo "Detected your kernel < 3.xx. Please, install panel for OPENVZ Ver.3 as shown below."
    echo -e "\033[1;37myum clean all && yum install -y wget && wget http://core.brainycp.ru/openvz3.sh && bash ./openvz3.sh\033[0m\n";tput sgr0
    vopenvz="yes"
    exit
fi

if [ "${k:0:20}" == "3.10.0-862.11.6.vz7." ]; then
    echo "Detected your kernel = 3.10.xx.vz7 Please, install panel for OPENVZ Ver.3 as shown below."
    echo -e "\033[1;37myum clean all && yum install -y wget && wget http://core.brainycp.ru/openvz3.sh && bash ./openvz3.sh\033[0m\n";tput sgr0
    vopenvz="yes"
    exit
fi

if [ "${k:0:21}" == "3.10.0-1062.12.1.vz7." ]; then
    echo "Detected your kernel = 3.10.xx.vz7 Please, install panel for OPENVZ Ver.3 as shown below."
    echo -e "\n\033[1;37myum clean all && yum install -y wget && wget http://core.brainycp.ru/openvz3.sh && bash ./openvz3.sh\033[0m\n";tput sgr0
    vopenvz="yes"
    exit
fi

if [ "${k:0:12}" == "5.4.55-1-pve" ]; then
    echo "Detected your kernel = 5.4.xx.vz7 Please, install panel for LXC Ver.3 as shown below."
    echo -e "\033[1;37myum clean all && yum install -y wget && wget http://core.brainycp.ru/openvz3.sh && bash ./openvz3.sh\033[0m\n";tput sgr0
    vopenvz="yes"
    exit
fi

##Checking vz
if [[ "${k}" == *".vz7."* ]]; then
    echo "Detected your kernel = 3.10.xx.vz7 Please, install panel for OPENVZ Ver.3 as shown below."
    echo -e "\n\033[1;37myum clean all && yum install -y wget && wget http://core.brainycp.ru/openvz3.sh && bash ./openvz3.sh\033[0m\n";tput sgr0
    vopenvz="yes"
    exit
fi


#
# VIRT TYPE
#
yum -y install virt-what dmidecode &>/dev/null

function get_virt_lxc() {
    virtall="$(virt-what)"
    virttypez="$(dmidecode -s system-product-name 2>/dev/stdout| awk '{print $1}')"
    virttypexen="$(dmidecode | grep -i domU 2>/dev/stdout)"
    virttypemic="$(dmidecode | egrep -i 'manufacturer|product' 2>/dev/stdout)"

    if [[ $virtall = "lxc" ]];then
        virttype="lxc"
    elif [[ $virtall = "openvz" ]];then
        virttype="openvz"
    elif [[ $virttypez = "/dev/mem:" ]];then
        virttype="openvz"
    else
        virttype="PASS"
fi

echo $virttype
}
#
# END VIRT TYPE
#

PWD_L=`pwd`
#echo "PWD is '$PWD'"

vtype="$(get_virt_lxc)"
if [[ $vtype = "lxc" ]];then
    echo "LXC no support for v3. Run the installer for OpenVZ as shown below."
    vopenvz="yes"
#    exit
fi

vtype="$(get_virt_lxc)"
if [[ $vtype = "openvz" ]];then
    echo "OpenVZ no support for v3. Run the installer for OpenVZ as shown below."
    vopenvz="yes"
#    exit
fi

echo "Virtualization test: "$vtype
echo ""
echo "*******  THE INSTALLER BRAINYCP CORE v3.02  *******"
echo ""

d=$(dirname $0)

if [ -f "/usr/local/brainycp/license" ]; then
    echo "The panel is already installed."
    exit 1
fi

ARRD="http://core.brainycp.com"

##
## RedHat
##
if [ -f "/etc/redhat-release" ]; then
    yum install -y polkit &>/dev/null
    systemctl restart polkit &>/dev/null

    v8=`cat /etc/redhat-release | grep -oE '[0-9.]'| sed ':a;N;$!ba;s/\n//g' | cut -c 1`
    if [[ "$v8" == "8" ]];then

	    yum clean all &>/dev/null
	    yum -y install rpm dnf yum python3-rpm &>/dev/null

	    # Show server params
	    echo -e "Detected OS Version: \033[1;32m${get_osname_org} ${v8} \033[0m";tput sgr0
	    echo -e "Detected Server Name: \033[1;32m${srvname} \033[0m";tput sgr0
	    echo -e "Detected Server IP: \033[1;32m${srvip} \033[0m";tput sgr0
	    if [ -f $TOTALFILE ]; then
    		    echo -e "Detected Server RAM memory: \033[1;32m${memtotal} KB\033[0m";tput sgr0
    		    echo -e "Detected Server SWAP memory: \033[1;32m${swaptotal} KB\033[0m";tput sgr0
	    fi

	    sys_err_swap="no"
	    sys_err_dhcp="no"
	    if [ -f $TOTALFILE ]; then
    		    echo -n "Checking RAM size... "
    		    if [ "${memtotal}" -ge "900000" ]; then
    			echo -en "\033[1;32mPASS \033[0m\n";tput sgr0
    		    else
    			echo -en "\033[1;31mFAIL \033[0m\n";tput sgr0
    			echo "There is not enough RAM on your server. A minimum of 900MB is required. Aborted.";echo ""
    			exit -1
		    fi

		    echo -n "Checking SWAP size... "
    		    if [ "${swaptotal}" -ge "2000000" ]; then
    			echo -en "\033[1;32mPASS \033[0m\n";tput sgr0
    		    else
    			echo -en "\033[1;31mFAIL \033[0m\n";tput sgr0
    			sys_err_swap="yes"
    			#echo "There is not enough SWAP on your server. A minimum of 2G is required. Aborted.";echo ""
    			#exit -1
    		    fi
	    fi

	    #DHCP
	    echo -n "Checking type IP address... "
	    if [[ "${dip}" == *"dynamic"* ]];then
    		    echo -en "\033[1;31mFAIL \033[0m\n";tput sgr0
    		    sys_err_dhcp="yes"
    		    #echo "Your IP address is of a dynamic type (DHCP), but you need a static one. Aborted.";echo ""
    		    #exit 1
	    else
    		    echo -en "\033[1;32mPASS \033[0m\n";tput sgr0
	    fi

	    #err
	    if [[ "${sys_err_swap}" == "yes" || "${sys_err_dhcp}" == "yes" ]];then
    		    echo ""
    		    echo "The following issues were found:"
	    fi
	    if [[ "${sys_err_swap}" == "yes" ]];then
    		    echo " *) There is not enough SWAP on your server. A minimum of 2G is required."
    		    echo "    The absence or insufficient volume of this section can lead to unstable operation of the Panel or its services."
	    fi
	    if [[ "${sys_err_dhcp}" == "yes" ]];then
    		    echo " *) Your IP address is of a dynamic type (DHCP), but you need a static one."
    		    echo "    A dynamic address of a network interface can lead to incorrect operation of some services, "
    		    echo "    for example, issuing certificates. And also, disrupting the installation process itself."
	    fi

	    if [[ "${sys_err_swap}" == "yes" || "${sys_err_dhcp}" == "yes" ]];then
    		    echo ""
    		    echo "Please also note that technical support cannot help you until you fix these problems."
	    fi

	    if [[ "${sys_err_swap}" == "yes" || "${sys_err_dhcp}" == "yes" ]];then
    		    echo ""
    		    while true; do
        		read -p "Continue installation? {y/n}: " yn
        		case $yn in
            			[Yy]* ) echo "Continue and ignore these errors."; break;;
            			[Nn]* ) echo "Abort the installation process and exit."; exit;;
            			* ) echo "Please answer yes or no.";;
        		esac
    		    done
	    fi

	    echo -en "Download install script... "
	    yum install -y wget &>/dev/null

	    #remove old
	    /bin/rm -f ${d}/_installCentos_t01v8_8.sh &>/dev/null
	    wget -t 4 --waitretry=3 --connect-timeout=10 --read-timeout=10  $ARRD/_installCentos_t01v8_8.sh &>/dev/null
	    if [ $? -eq 0 ]; then
		result=0
		echo -en "\033[1;32m [OK] \033[0m\n"
		tput sgr0
	    else
		result=1
		echo -e "\033[1;31m [ERROR] \033[0m\n"
		tput sgr0 
		echo -e "Please. Download new script for install. wget http://core.brainycp.com/install.sh"
		exit
	    fi

. ${d}/_installCentos_t01v8_8.sh
#. ${PWD_L}/_checkpkg.sh -u auto
	    brainy-core -s auto

	    #PWD_L=`pwd`
	    #echo "PWD is '$PWD_L'"
	    chmod 0750 /usr/local/brainycp/ssh/*.php
	    chmod 0750 /usr/local/brainycp/ssh/*.sh

	    echo -e "\n\n\033[1;34m BrainyCP was successfully installed! \033[0m\n\n";tput sgr0
	    echo -e "\nBy using this product you completely accept License Agreement - https://brainycp.com/license_agreement\n"
	    echo -e "To use it:\n"
	    echo -e "http://"$ip_serv":8002 or https://"$ip_serv":8000\n"
	    echo -e "username: root"
	    echo -e "password: YOUR ROOT PASSWORD"
	    echo ""
	    echo -e "\033[1;31m 1) WARNING!!! Kernel updated successfully. Please, reboot your system! \033[0m\n";tput sgr0

	    #
	    #sync ; echo 1 > /proc/sys/vm/drop_caches
	    exit 0

    fi
    # END 8

    # BEGIN 9
    if [[ "${v8}" == "9" ]]; then
	    echo ""
	    #echo -e "\033[1;31m Your system is not supported! \033[0m\n";tput sgr0
	    #exit 0

	    yum clean all &>/dev/null
	    yum -y install rpm dnf yum python3-rpm &>/dev/null

	    # Show server params
	    echo -e "Detected OS Version: \033[1;32m${get_osname_org} ${v8} \033[0m";tput sgr0
	    echo -e "Detected Server Name: \033[1;32m${srvname} \033[0m";tput sgr0
	    echo -e "Detected Server IP: \033[1;32m${srvip} \033[0m";tput sgr0
	    if [ -f $TOTALFILE ]; then
    		    echo -e "Detected Server RAM memory: \033[1;32m${memtotal} KB\033[0m";tput sgr0
    		    echo -e "Detected Server SWAP memory: \033[1;32m${swaptotal} KB\033[0m";tput sgr0
	    fi

	    sys_err_swap="no"
	    sys_err_dhcp="no"
	    if [ -f $TOTALFILE ]; then
    		    echo -n "Checking RAM size... "
    		    if [ "${memtotal}" -ge "900000" ]; then
    			echo -en "\033[1;32mPASS \033[0m\n";tput sgr0
    		    else
    			echo -en "\033[1;31mFAIL \033[0m\n";tput sgr0
    			echo "There is not enough RAM on your server. A minimum of 900MB is required. Aborted.";echo ""
    			exit 1
		    fi

		    echo -n "Checking SWAP size... "
    		    if [ "${swaptotal}" -ge "2000000" ]; then
    			echo -en "\033[1;32mPASS \033[0m\n";tput sgr0
    		    else
    			echo -en "\033[1;31mFAIL \033[0m\n";tput sgr0
    			sys_err_swap="yes"
    			#echo "There is not enough SWAP on your server. A minimum of 2G is required. Aborted.";echo ""
    			#exit 1
    		    fi
	    fi

	    #DHCP
	    echo -n "Checking type IP address... "
	    if [[ "${dip}" == *"dynamic"* ]];then
    		    echo -en "\033[1;31mFAIL \033[0m\n";tput sgr0
    		    sys_err_dhcp="yes"
    		    #echo "Your IP address is of a dynamic type (DHCP), but you need a static one. Aborted.";echo ""
    		    #exit 1
	    else
    		    echo -en "\033[1;32mPASS \033[0m\n";tput sgr0
	    fi

	    #err
	    if [[ "${sys_err_swap}" == "yes" || "${sys_err_dhcp}" == "yes" ]];then
    		    echo ""
    		    echo "The following issues were found:"
	    fi
	    if [[ "${sys_err_swap}" == "yes" ]];then
    		    echo " *) There is not enough SWAP on your server. A minimum of 2G is required."
    		    echo "    The absence or insufficient volume of this section can lead to unstable operation of the Panel or its services."
	    fi
	    if [[ "${sys_err_dhcp}" == "yes" ]];then
    		    echo " *) Your IP address is of a dynamic type (DHCP), but you need a static one."
    		    echo "    A dynamic address of a network interface can lead to incorrect operation of some services, "
    		    echo "    for example, issuing certificates. And also, disrupting the installation process itself."
	    fi

	    if [[ "${sys_err_swap}" == "yes" || "${sys_err_dhcp}" == "yes" ]];then
    		    echo ""
    		    echo "Please also note that technical support cannot help you until you fix these problems."
	    fi

	    if [[ "${sys_err_swap}" == "yes" || "${sys_err_dhcp}" == "yes" ]];then
    		    echo ""
    		    while true; do
        		read -p "Continue installation? {y/n}: " yn
        		case $yn in
            			[Yy]* ) echo "Continue and ignore these errors."; break;;
            			[Nn]* ) echo "Abort the installation process and exit."; exit;;
            			* ) echo "Please answer yes or no.";;
        		esac
    		    done
	    fi

            # remove old
            /bin/rm -f ${d}/_installCentos_t01v9_8.sh &>/dev/null
            wget -t 4 --waitretry=3 --connect-timeout=10 --read-timeout=10  $ARRD/_installCentos_t01v9_8.sh &>/dev/null
            if [ $? -eq 0 ]; then
                result=0
                echo -en "\033[1;32m [OK] \033[0m\n"
                tput sgr0
            else
                result=1
                echo -e "\033[1;31m [ERROR] \033[0m\n"
                tput sgr0
                echo -e "Please. Download new script for install. wget http://core.brainycp.com/install.sh"
                exit
            fi

. ${d}/_installCentos_t01v9_8.sh
#. ${PWD_L}/_checkpkg.sh -u auto
            brainy-core -s auto

            chmod 0750 /usr/local/brainycp/ssh/*.php
            chmod 0750 /usr/local/brainycp/ssh/*.sh

            echo -e "\n\n\033[1;34m BrainyCP was successfully installed! \033[0m\n\n";tput sgr0
            echo -e "\nBy using this product you completely accept License Agreement - https://brainycp.com/license_agreement\n"
            echo -e "To use it:\n"
            echo -e "http://"$ip_serv":8002 or https://"$ip_serv":8000\n"
            echo -e "username: root"
            echo -e "password: YOUR ROOT PASSWORD"
            echo ""
            echo -e "\033[1;31m 1) WARNING!!! Kernel updated successfully. Please, reboot your system! \033[0m\n";tput sgr0

#	    echo -en "Download install script... "
#	    yum install -y wget &>/dev/null
#	    # remove old
#	    if [ "${os_type}" == "rocky" ];then
#		/bin/rm -f ${d}/_installRocky_9_3 &>/dev/null
#		wget -t 2  $ARRD/_installRocky_9_3 &>/dev/null
#	    else
#		/bin/rm -f ${d}/_installAlma_9_3 &>/dev/null
#		wget -t 2  $ARRD/_installAlma_9_3 &>/dev/null
#	    fi
#	    if [ $? -eq 0 ]; then
#		result=0
#		echo -en "\033[1;32m [OK] \033[0m\n";tput sgr0
#	    else
#		result=1
#		echo -e "\033[1;31m [ERROR] \033[0m\n";tput sgr0
#		echo -e "Please. Download new script for install. wget http://core.brainycp.ru/install.sh"
#		exit 1
#	    fi
#
#	    if [ "${os_type}" == "rocky" ];then
#		chmod 0700 ${d}/_installRocky_9_3
#		${d}/_installRocky_9_3 "${1}"
#		/bin/rm -f ${d}/_installRocky_9_3 &>/dev/null
#		touch ${d}/install_rocky_9.txt
#	    else
#		chmod 0700 ${d}/_installAlma_9_3
#		${d}/_installAlma_9_3 "${1}"
#		/bin/rm -f ${d}/_installAlma_9_3 &>/dev/null
#		touch ${d}/install_alma_9.txt
#	    fi
#	    if [[ -f "/usr/local/brainycp/index.php" ]];then
#		brainy-core -s auto &>brainy_core.log
#		chmod 0750 /usr/local/brainycp/ssh/*.php
#		chmod 0750 /usr/local/brainycp/ssh/*.sh
#	    else
#		echo -e "Fatal error. Abort"
#	    fi
	    exit 0
    fi
    # END 9

    # BEGIN 10
    if [[ "${v10}" == "10" ]]; then
            echo ""
            #echo -e "\033[1;31m Your system is not supported! \033[0m\n";tput sgr0
            #exit 0

	    #echo -en "Update dnf service... "
	    echo -en "System update. Please wait... "
            yum clean all &>/dev/null
            #yum -y install rpm dnf yum python3-rpm openssl python3-dnf &>/dev/null
            yum -y update
	    if [ $? -eq 0 ]; then
		#result=0
		echo -en "\033[1;32m [OK] \033[0m\n";tput sgr0
	    else
		#result=1
		echo -e "\033[1;31m [ERROR] \033[0m\n";tput sgr0
		exit
	    fi

            # Show server params
            echo -e "Detected OS Version: \033[1;32m${get_osname_org} ${v10} \033[0m";tput sgr0
            echo -e "Detected Server Name: \033[1;32m${srvname} \033[0m";tput sgr0
            echo -e "Detected Server IP: \033[1;32m${srvip} \033[0m";tput sgr0
            if [ -f $TOTALFILE ]; then
                    echo -e "Detected Server RAM memory: \033[1;32m${memtotal} KB\033[0m";tput sgr0
                    echo -e "Detected Server SWAP memory: \033[1;32m${swaptotal} KB\033[0m";tput sgr0
            fi

            sys_err_swap="no"
            sys_err_dhcp="no"
            if [ -f $TOTALFILE ]; then
                    echo -n "Checking RAM size... "
                    if [ "${memtotal}" -ge "900000" ]; then
                        echo -en "\033[1;32mPASS \033[0m\n";tput sgr0
                    else
                        echo -en "\033[1;31mFAIL \033[0m\n";tput sgr0
                        echo "There is not enough RAM on your server. A minimum of 900MB is required. Aborted.";echo ""
                        exit 1
                    fi

                    echo -n "Checking SWAP size... "
                    if [ "${swaptotal}" -ge "2000000" ]; then
                        echo -en "\033[1;32mPASS \033[0m\n";tput sgr0
                    else
                        echo -en "\033[1;31mFAIL \033[0m\n";tput sgr0
                        sys_err_swap="yes"
                        #echo "There is not enough SWAP on your server. A minimum of 2G is required. Aborted.";echo ""
                        #exit 1
                    fi
            fi

            #DHCP
            echo -n "Checking type IP address... "
            if [[ "${dip}" == *"dynamic"* ]];then
                    echo -en "\033[1;31mFAIL \033[0m\n";tput sgr0
                    sys_err_dhcp="yes"
                    #echo "Your IP address is of a dynamic type (DHCP), but you need a static one. Aborted.";echo ""
                    #exit 1
            else
                    echo -en "\033[1;32mPASS \033[0m\n";tput sgr0
            fi

            #err
            if [[ "${sys_err_swap}" == "yes" || "${sys_err_dhcp}" == "yes" ]];then
                    echo ""
                    echo "The following issues were found:"
            fi
            if [[ "${sys_err_swap}" == "yes" ]];then
                    echo " *) There is not enough SWAP on your server. A minimum of 2G is required."
                    echo "    The absence or insufficient volume of this section can lead to unstable operation of the Panel or its services."
            fi
            if [[ "${sys_err_dhcp}" == "yes" ]];then
                    echo " *) Your IP address is of a dynamic type (DHCP), but you need a static one."
                    echo "    A dynamic address of a network interface can lead to incorrect operation of some services, "
                    echo "    for example, issuing certificates. And also, disrupting the installation process itself."
            fi

            if [[ "${sys_err_swap}" == "yes" || "${sys_err_dhcp}" == "yes" ]];then
                    echo ""
                    echo "Please also note that technical support cannot help you until you fix these problems."
            fi

            if [[ "${sys_err_swap}" == "yes" || "${sys_err_dhcp}" == "yes" ]];then
                    echo ""
                    while true; do
                        read -p "Continue installation? {y/n}: " yn
                        case $yn in
                                [Yy]* ) echo "Continue and ignore these errors."; break;;
                                [Nn]* ) echo "Abort the installation process and exit."; exit;;
                                * ) echo "Please answer yes or no.";;
                        esac
                    done
            fi

            # remove old
            /bin/rm -f ${d}/_installCentos_t01v10_8.sh &>/dev/null
            wget -t 4 --waitretry=3 --connect-timeout=10 --read-timeout=10  $ARRD/_installCentos_t01v10_8.sh &>/dev/null
            if [ $? -eq 0 ]; then
                result=0
                echo -en "\033[1;32m [OK] \033[0m\n"
                tput sgr0
            else
                result=1
                echo -e "\033[1;31m [ERROR] \033[0m\n"
                tput sgr0
                echo -e "Please. Download new script for install. wget http://core.brainycp.com/install.sh"
                exit
            fi

. ${d}/_installCentos_t01v10_8.sh
#. ${PWD_L}/_checkpkg.sh -u auto
            brainy-core -s auto


            chmod 0750 /usr/local/brainycp/ssh/*.php
            chmod 0750 /usr/local/brainycp/ssh/*.sh

            echo -e "\n\n\033[1;34m BrainyCP was successfully installed! \033[0m\n\n";tput sgr0
            echo -e "\nBy using this product you completely accept License Agreement - https://brainycp.com/license_agreement\n"
            echo -e "To use it:\n"
            echo -e "http://"$ip_serv":8002 or https://"$ip_serv":8000\n"
            echo -e "username: root"
            echo -e "password: YOUR ROOT PASSWORD"
            echo ""
            echo -e "\033[1;31m 1) WARNING!!! Kernel updated successfully. Please, reboot your system! \033[0m\n";tput sgr0


#           echo -en "Download install script... "
#           yum install -y wget &>/dev/null
#           # remove old
#           if [ "${os_type}" == "rocky" ];then
#               /bin/rm -f ${d}/_installRocky_9_3 &>/dev/null
#               wget -t 2  $ARRD/_installRocky_9_3 &>/dev/null
#           else
#               /bin/rm -f ${d}/_installAlma_9_3 &>/dev/null
#               wget -t 2  $ARRD/_installAlma_9_3 &>/dev/null
#           fi
#           if [ $? -eq 0 ]; then
#               result=0
#               echo -en "\033[1;32m [OK] \033[0m\n";tput sgr0
#           else
#               result=1
#               echo -e "\033[1;31m [ERROR] \033[0m\n";tput sgr0
#               echo -e "Please. Download new script for install. wget http://core.brainycp.ru/install.sh"
#               exit 1
#           fi

#           if [ "${os_type}" == "rocky" ];then
#               chmod 0700 ${d}/_installRocky_9_3
#               ${d}/_installRocky_9_3 "${1}"
#               /bin/rm -f ${d}/_installRocky_9_3 &>/dev/null
#               touch ${d}/install_rocky_9.txt
#           else
#               chmod 0700 ${d}/_installAlma_9_3
#               ${d}/_installAlma_9_3 "${1}"
#               /bin/rm -f ${d}/_installAlma_9_3 &>/dev/null
#               touch ${d}/install_alma_9.txt
#           fi
#           brainy-core -s auto &>brainy_core.log
#           chmod 0750 /usr/local/brainycp/ssh/*.php
#           chmod 0750 /usr/local/brainycp/ssh/*.sh
            exit 0
    fi
    # END 10

if [[ $vopenvz = "no" ]];then
#KVM
#echo $openvz
#exit

    osRhl="yes"
    rm -rf /etc/yum.repos.d/epel.repo
    rm -rf /etc/yum.repos.d/epel-testing.repo
    rm -rf /etc/yum.repos.d/ceph.repo
    rm -rf /etc/yum.repos.d/cm.repo
    yum clean all &>/dev/null
    yum -y install rpm yum python3-rpm rpm-build-libs rpm-libs &>/dev/null

#    echo "Detected OS Version: CentOS"
    echo -e "Detected OS Version: \033[1;32mCentOS ${v8} \033[0m";tput sgr0
    echo -e "Detected Server Name: \033[1;32m${srvname} \033[0m";tput sgr0
    echo -e "Detected Server IP: \033[1;32m${srvip} \033[0m";tput sgr0
    if [ -f $TOTALFILE ]; then
        echo -e "Detected Server RAM memory: \033[1;32m${memtotal} KB\033[0m";tput sgr0
        echo -e "Detected Server SWAP memory: \033[1;32m${swaptotal} KB\033[0m";tput sgr0
    fi

    sys_err_swap="no"
    sys_err_dhcp="no"
    if [ -f $TOTALFILE ]; then
        echo -n "Checking RAM size... "
        if [ "${memtotal}" -ge "1000000" ]; then
        echo -en "\033[1;32mPASS \033[0m\n";tput sgr0
        else
        echo -en "\033[1;31mFAIL \033[0m\n";tput sgr0
        echo "There is not enough RAM on your server. A minimum of 1G is required. Aborted.";echo ""
        exit -1
    fi

    echo -n "Checking SWAP size... "
        if [ "${swaptotal}" -ge "2000000" ]; then
        echo -en "\033[1;32mPASS \033[0m\n";tput sgr0
        else
        echo -en "\033[1;31mFAIL \033[0m\n";tput sgr0
        sys_err_swap="yes"
        #echo "There is not enough SWAP on your server. A minimum of 2G is required. Aborted.";echo ""
        #exit -1
        fi
    fi

    #DHCP
    echo -n "Checking type IP address... "
    if [[ "${dip}" == *"dynamic"* ]];then
      echo -en "\033[1;31mFAIL \033[0m\n";tput sgr0
      sys_err_dhcp="yes"
      #echo "Your IP address is of a dynamic type (DHCP), but you need a static one. Aborted.";echo ""
      #exit 1
    else
      echo -en "\033[1;32mPASS \033[0m\n";tput sgr0
    fi

    #err
    if [[ "${sys_err_swap}" == "yes" || "${sys_err_dhcp}" == "yes" ]];then
        echo ""
        echo "The following issues were found:"
    fi
    if [[ "${sys_err_swap}" == "yes" ]];then
        echo " *) There is not enough SWAP on your server. A minimum of 2G is required."
        echo "    The absence or insufficient volume of this section can lead to unstable operation of the Pael or its services."
    fi
    if [[ "${sys_err_dhcp}" == "yes" ]];then
        echo " *) Your IP address is of a dynamic type (DHCP), but you need a static one."
        echo "    A dynamic address of a network interface can lead to incorrect operation of some services, "
        echo "    for example, issuing certificates. And also, disrupting the installation process itself."
    fi

    if [[ "${sys_err_swap}" == "yes" || "${sys_err_dhcp}" == "yes" ]];then
        echo ""
        echo "Please also note that technical support cannot help you until you fix these problems."
    fi

    if [[ "${sys_err_swap}" == "yes" || "${sys_err_dhcp}" == "yes" ]];then
        echo ""
        while true; do
            read -p "Continue installation? {y/n}: " yn
            case $yn in
                [Yy]* ) echo "Continue and ignore these errors."; break;;
                [Nn]* ) echo "Abort the installation process and exit."; exit;;
                * ) echo "Please answer yes or no.";;
            esac
        done
    fi

    echo -en "Download install script..."
    yum install -y wget &>/dev/null

    rm -f ${d}/_installCentos_t01.sh &>/dev/null
    wget -t 2  $ARRD/_installCentos_t01.sh &>/dev/null
	if [ $? -eq 0 ]; then
		result=0
		echo -en "\033[1;32m [OK] \033[0m\n"
		tput sgr0
	else
		result=1
		echo -e "\033[1;31m [ERROR] \033[0m\n"
		tput sgr0 
		echo -e "Please. Download new script for install. wget http://core.brainycp.ru/install_t01.sh"
		exit
	fi

    wget -t 2  $ARRD/_checkpkg.sh &>/dev/null
    #chmod 755 ${d}/_checkpkg.sh

. ${d}/_installCentos_t01.sh
. ${PWD_L}/_checkpkg.sh -u auto

#PWD_L=`pwd`
#echo "PWD is '$PWD_L'"

echo -e "\n\n\033[1;34m BrainyCP was successfully installed! \033[0m\n\n";tput sgr0
echo -e "\nBy using this product you completely accept License Agreement - https://brainycp.com/license_agreement\n"
echo -e "To use it:\n"
echo -e "http://"$ip_serv":8002 or https://"$ip_serv":8000\n"
echo -e "username: root"
echo -e "password: YOUR ROOT PASSWORD"
echo ""
echo -e "\033[1;31m 1) WARNING!!! Kernel updated successfully. Please, reboot your system! \033[0m\n";tput sgr0
#sync ; echo 1 > /proc/sys/vm/drop_caches
exit 0

else
#OPENVZ
#echo $openvz
#exit

    yum -y install rpm python3-rpm rpm-build-libs rpm-libs &>/dev/null

    rm -f ${d}/_installCentos_01_openVZv3.sh &>/dev/null
    wget -t 2  $ARRD/_installCentos_01_openVZv3.sh &>/dev/null
        if [ $? -eq 0 ]; then
                result=0
                echo -en "\033[1;32m [OK] \033[0m\n"
                tput sgr0
        else
                result=1
                echo -e "\033[1;31m [ERROR] \033[0m\n"
                tput sgr0
                echo -e "Please. Download new script for install. wget http://core.brainycp.ru/install.sh"
                exit
        fi
. ${d}/_installCentos_01_openVZv3.sh
exit 0
fi

fi


##
## Debian,Ubuntu
##
if [ -f "/etc/debian_version" ]; then
    ver=`cat /etc/issue.net | awk '{print $1$3}'`
    echo "Detected OS Version: "$ver

# Debian 8
if [[ $ver == "Debian8" ]];then
    osDebian="yes"
    apt-get install -y --force-yes wget &>/dev/null
    echo -en "Download install script... "
    wget -t 2 http://core.brainycp.ru/_installDebian8.sh  &>/dev/null
	if [ $? -eq 0 ]; then
		result=0
		echo -en "\033[1;32m [OK] \033[0m\n"
		tput sgr0
	else
		result=1
		echo -e "\033[1;31m [ERROR] \033[0m\n"
		tput sgr0
		exit
	fi
. ${d}/_installDebian8.sh
exit 0
fi

# Debian 9
if [[ $ver == "Debian9__" ]];then
    osDebian="yes"
    apt-get install -y --force-yes wget &>/dev/null
    echo -en "Download install script... "
    wget -t 2 http://core.brainycp.ru/_installDebian9.sh  &>/dev/null
	if [ $? -eq 0 ]; then
		result=0
		echo -en "\033[1;32m [OK] \033[0m\n"
		tput sgr0
	else
		result=1
		echo -e "\033[1;31m [ERROR] \033[0m\n"
		tput sgr0
		exit
	fi
. ${d}/_installDebian9.sh
exit 0
fi

# Ubuntu
if [[ $ver == "Ubuntu__" ]];then
    osUbuntu="yes"
    apt-get install -y --force-yes wget &>/dev/null
    echo -en "Detected Download install script... "
    wget -t 2 http://core.brainycp.ru/_installUbuntu.sh  &>/dev/null
	if [ $? -eq 0 ]; then
		result=0
		echo -en "\033[1;32m [OK] \033[0m\n"
		tput sgr0
	else
		result=1
		echo -e "\033[1;31m [ERROR] \033[0m\n"
		tput sgr0
		exit
	fi
. ${d}/_installUbuntu.sh
exit 0
fi

fi

echo -e "\033[1;31mThe suitable system is not found or its parameters are not correct! \033[0m\n";tput sgr0
reset_screen
exit 0

