#!/bin/bash
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
export LC_ALL=C

: '
Copyright (C) 2022 - now  HaoZi Technology Co., Ltd.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
'

export HR="+----------------------------------------------------"
export ARCH=$(uname -m)
# hce Huawei Cloud EulerOS
# openEuler openEuler
export OS=$(source /etc/os-release && { [[ "$ID" == "debian" ]] || [[ "$ID_LIKE" =~ "debian" ]] && echo "debian"; } || { [[ "$ID" == "ubuntu" ]] || [[ "$ID_LIKE" =~ "ubuntu" ]] && echo "ubuntu"; } || { [[ "$ID" == "tencentos" ]] || [[ "$ID" == "opencloudos" ]] || [[ "$ID" == "hce" ]] || [[ "$ID" == "openEuler" ]] || [[ "$ID" == "rhel" ]] || [[ "$ID_LIKE" =~ "rhel" ]] && echo "rhel"; } || echo "unknown")
export CORES=$(nproc)
export MEM=$(LC_ALL=C free -m | grep Mem | awk '{print  $2}')
export SWAP=$(LC_ALL=C free -m | grep Swap | awk '{print  $2}')

export download_url="https://dl.cdn.haozi.net/panel"
export setup_path="/www"

# 从配置文件中读取 setup_path
if [ -f /usr/local/etc/panel/config.yml ]; then
    setup_path=$(grep root /usr/local/etc/panel/config.yml | awk '{print $2}')
fi

# 计算 j 值(通用)
calculate_j() {
    total=$((MEM + SWAP))
    j_value=$((total / 1024))

    [ "$j_value" -eq 0 ] && j_value=1
    [ "$j_value" -gt "$CORES" ] && j_value=$CORES
    [ "$j_value" -gt 1 ] && j_value=$((j_value - 1)) # 大于1则减1

    echo "$j_value"
}

# 计算 j 值(2倍内存)
calculate_j2() {
    total=$((MEM + SWAP))
    j_value=$((total / 2024))

    [ "$j_value" -eq 0 ] && j_value=1
    [ "$j_value" -gt "$CORES" ] && j_value=$CORES
    [ "$j_value" -gt 1 ] && j_value=$((j_value - 1)) # 大于1则减1

    echo "$j_value"
}

# 系统信息
sys_info() {
    dir=$(pwd)
    cd /tmp || cd /

    if [ -f /etc/os-release ]; then
        system_version=$(grep PRETTY_NAME /etc/os-release | awk -F '"' '{print $2}')
    else
        system_version="unknown"
    fi

    kernel_version=$(uname -a)
    bitness=$(getconf LONG_BIT)-bit
    cpu_name=$(lscpu | grep "^Model name:" | awk -F: '{print $2}' | sed 's/^[ \t]*//')
    cpu_flags=$(lscpu | grep "^Flags:" | awk -F: '{print $2}' | sed 's/^[ \t]*//' | sed 's/ /,/g')

    if command -v gcc >/dev/null 2>&1; then
        gcc_version=$(gcc -v 2>&1 | tail -n1 | awk '{print $3}')
    else
        gcc_version="no gcc"
    fi

    if command -v cmake >/dev/null 2>&1; then
        cmake_version=$(cmake --version | head -n1 | awk '{print $3}')
    else
        cmake_version="no cmake"
    fi

    echo -e $HR
    echo -e "| Kernel: $kernel_version"
    echo -e "| CPU: $cpu_name | Arch: $ARCH | Core: $CORES"
    echo -e "| OS: $system_version | Bit: $bitness | Mem: $MEM | Swap: $SWAP | gcc: $gcc_version | cmake: $cmake_version"
    echo -e $HR

    [ -d "$dir" ] && cd "$dir"
}

# 错误输出
error() {
    echo -e $HR
    echo -e "| 错误:$1"
    echo -e "| Error: $1"
    echo -e "| 可截图错误信息寻求帮助,或者联系面板官方支持。"
    echo -e "| Take a screenshot of the error message for help, or contact the panel official support."
    sys_info
    exit 1
}