#!/bin/bash
: '
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/>.
'

source <(curl -f -s --connect-timeout 10 --retry 3 https://dl.cdn.haozi.net/panel/public.sh)
if [ $? -ne 0 ]; then
    echo "下载 public.sh 失败,请检查网络或稍后重试。"
    echo "Download public.sh failed, please check the network or try again later."
    exit 1
fi

channel=${1}
version=${2}
mysql_path="${setup_path}/server/mysql"
mysql_password=$(panel getSetting mysql_root_password)
j=$(calculate_j)

# 安装依赖
if [ ${OS} == "rhel" ]; then
    dnf makecache -y
    dnf groupinstall "Development Tools" -y
    dnf install cmake bison ncurses-devel libtirpc-devel openssl-devel pkg-config systemd-devel openldap-devel libudev-devel cyrus-sasl-devel cyrus-sasl-scram patchelf rpcgen rpcsvc-proto-devel krb5-devel zlib-devel readline-devel libcurl-devel -y
elif [ ${OS} == "debian" ] || [ ${OS} == "ubuntu" ]; then
    apt-get update
    apt-get install build-essential cmake bison libncurses5-dev libtirpc-dev libssl-dev pkg-config libsystemd-dev libldap2-dev libudev-dev libsasl2-dev libsasl2-modules-gssapi-mit patchelf libkrb5-dev zlib1g-dev libreadline-dev libcurl4-openssl-dev -y
else
    error "不支持的操作系统"
fi
if [ "$?" != "0" ]; then
    error "安装依赖软件失败"
fi

# 预检查
systemctl is-active --quiet mysqld
if [ "$?" != "0" ]; then
    error "应用运行状态不正常"
fi

# 停止已有服务
systemctl stop mysqld

# 准备目录
cd ${mysql_path}
rm -rf src

# 下载源码
wget -T 120 -t 3 -O ${mysql_path}/percona-server-${version}.7z ${download_url}/mysql/percona-server-${version}.7z
wget -T 20 -t 3 -O ${mysql_path}/percona-server-${version}.7z.sha256 ${download_url}/mysql/percona-server-${version}.7z.sha256
if ! sha256sum --status -c percona-server-${version}.7z.sha256; then
    rm -f percona-server-${version}.7z
    rm -f percona-server-${version}.7z.sha256
    error "mysql 校验失败"
fi

7z x percona-server-${version}.7z
rm -f percona-server-${version}.7z
rm -f percona-server-${version}.7z.sha256

# 编译
mv percona-server-${version} src
chmod -R 700 src
cd src
mkdir dist
cd dist

# 5.7 和 8.0 需要 boost
if [[ ${channel} == "percona_57" ]] || [[ ${channel} == "percona_80" ]]; then
    WITH_BOOST="-DWITH_BOOST=../boost"
fi

cmake .. -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_INSTALL_PREFIX=${mysql_path} -DMYSQL_DATADIR=${mysql_path}/data -DSYSCONFDIR=${mysql_path}/conf -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DENABLED_LOCAL_INFILE=1 -DWITH_DEBUG=0 -DWITH_UNIT_TESTS=OFF -DINSTALL_MYSQLTESTDIR= -DCMAKE_BUILD_TYPE=Release -DWITH_SYSTEMD=1 -DSYSTEMD_PID_DIR=${mysql_path} ${WITH_BOOST}
if [ "$?" != "0" ]; then
    error "编译初始化失败"
fi

make "-j${j}"
if [ "$?" != "0" ]; then
    error "编译失败"
fi

# 安装
make install
if [ "$?" != "0" ]; then
    error "安装失败"
fi

# 设置权限
chown -R mysql:mysql ${mysql_path}
chmod -R 700 ${mysql_path}

# 启动服务
systemctl daemon-reload
systemctl start mysqld
if [ "$?" != "0" ]; then
    error "启动失败"
fi

# 执行更新后的初始化
if [[ "${channel}" == "percona_57" ]]; then
    ${mysql_path}/bin/mysql_upgrade -uroot -p${mysql_password}
fi
${mysql_path}/bin/mysql -uroot -p${mysql_password} -e "DROP DATABASE test;"
${mysql_path}/bin/mysql -uroot -p${mysql_password} -e "DELETE FROM mysql.user WHERE user='';"
${mysql_path}/bin/mysql -uroot -p${mysql_password} -e "FLUSH PRIVILEGES;"

panel-cli app write mysql ${channel} ${version}

echo -e $HR
echo "升级完成"
echo -e $HR