#!/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"
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 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 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

mysql_user_check=$(cat /etc/passwd | grep mysql)
if [ "${mysql_user_check}" == "" ]; then
    groupadd mysql
    useradd -s /sbin/nologin -g mysql mysql
fi

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

# 下载源码
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 -rf ${mysql_path}
    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
    rm -rf ${mysql_path}
    error "编译初始化失败"
fi

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

# 安装
make install
if [ "$?" != "0" ]; then
    rm -rf ${mysql_path}
    error "安装失败"
fi

# 配置
mkdir ${mysql_path}/conf
cat >${mysql_path}/conf/my.cnf <<EOF
[client]
port = 3306
socket = /tmp/mysql.sock

[mysqld]
port = 3306
socket = /tmp/mysql.sock
datadir = ${mysql_path}/data
default_storage_engine = InnoDB
skip-external-locking
table_definition_cache = 400
performance_schema_max_table_instances = 400
key_buffer_size = 8M
max_allowed_packet = 1G
table_open_cache = 32
sort_buffer_size = 256K
net_buffer_length = 4K
read_buffer_size = 128K
read_rnd_buffer_size = 256K
myisam_sort_buffer_size = 4M
thread_cache_size = 4
query_cache_size = 4M
tmp_table_size = 8M
explicit_defaults_for_timestamp = 1
#skip-name-resolve
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
early-plugin-load = ""

log-bin = mysql-bin
server-id = 1
slow_query_log = 1
slow-query-log-file = ${mysql_path}/mysql-slow.log
long_query_time = 3
log-error = ${mysql_path}/mysql-error.log

innodb_data_home_dir = ${mysql_path}/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = ${mysql_path}/data
innodb_buffer_pool_size = 16M
innodb_redo_log_capacity = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
innodb_max_dirty_pages_pct = 90
innodb_read_io_threads = 4
innodb_write_io_threads = 4

[mysqldump]
quick
max_allowed_packet = 500M

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
EOF

# 根据CPU核心数确定写入线程数
sed -i 's/innodb_write_io_threads = 4/innodb_write_io_threads = '${CORES}'/g' ${mysql_path}/conf/my.cnf
sed -i 's/innodb_read_io_threads = 4/innodb_read_io_threads = '${CORES}'/g' ${mysql_path}/conf/my.cnf

if [[ ${channel} == "percona_80" ]] || [[ ${channel} == "percona_84" ]]; then
    sed -i '/query_cache_size/d' ${mysql_path}/conf/my.cnf
fi
if [[ ${channel} == "percona_57" ]]; then
    sed -i '/innodb_redo_log_capacity/d' ${mysql_path}/conf/my.cnf
fi

# 根据内存大小调参
if [[ ${MEM} -gt 1024 && ${MEM} -lt 2048 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 32M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 128#" ${mysql_path}/conf/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 768K#" ${mysql_path}/conf/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 768K#" ${mysql_path}/conf/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 8M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 16#" ${mysql_path}/conf/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 16M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 32M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 128M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_redo_log_capacity.*#innodb_redo_log_capacity = 64M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_log_buffer_size.*#innodb_log_buffer_size = 16M#" ${mysql_path}/conf/my.cnf
elif [[ ${MEM} -ge 2048 && ${MEM} -lt 4096 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 64M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 256#" ${mysql_path}/conf/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 1M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 1M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 16M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 32#" ${mysql_path}/conf/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 32M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 256M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_redo_log_capacity.*#innodb_redo_log_capacity = 128M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_log_buffer_size.*#innodb_log_buffer_size = 32M#" ${mysql_path}/conf/my.cnf
elif [[ ${MEM} -ge 4096 && ${MEM} -lt 8192 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 128M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 512#" ${mysql_path}/conf/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 2M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 2M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 32M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 64#" ${mysql_path}/conf/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 64M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 512M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_redo_log_capacity.*#innodb_redo_log_capacity = 256M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_log_buffer_size.*#innodb_log_buffer_size = 64M#" ${mysql_path}/conf/my.cnf
elif [[ ${MEM} -ge 8192 && ${MEM} -lt 16384 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 256M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 1024#" ${mysql_path}/conf/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 4M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 4M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 64M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 128#" ${mysql_path}/conf/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 128M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 128M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 1024M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_redo_log_capacity.*#innodb_redo_log_capacity = 512M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_log_buffer_size.*#innodb_log_buffer_size = 128M#" ${mysql_path}/conf/my.cnf
elif [[ ${MEM} -ge 16384 && ${MEM} -lt 32768 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 512M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 2048#" ${mysql_path}/conf/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 8M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 8M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 128M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 256#" ${mysql_path}/conf/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 256M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 256M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 2048M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_redo_log_capacity.*#innodb_redo_log_capacity = 1G#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_log_buffer_size.*#innodb_log_buffer_size = 256M#" ${mysql_path}/conf/my.cnf
elif [[ ${MEM} -ge 32768 ]]; then
    sed -i "s#^key_buffer_size.*#key_buffer_size = 1024M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^table_open_cache.*#table_open_cache = 4096#" ${mysql_path}/conf/my.cnf
    sed -i "s#^sort_buffer_size.*#sort_buffer_size = 16M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^read_buffer_size.*#read_buffer_size = 16M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 256M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^thread_cache_size.*#thread_cache_size = 512#" ${mysql_path}/conf/my.cnf
    sed -i "s#^query_cache_size.*#query_cache_size = 512M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^tmp_table_size.*#tmp_table_size = 512M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 4096M#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_redo_log_capacity.*#innodb_redo_log_capacity = 2G#" ${mysql_path}/conf/my.cnf
    sed -i "s#^innodb_log_buffer_size.*#innodb_log_buffer_size = 512M#" ${mysql_path}/conf/my.cnf
fi

# 初始化
cd ${mysql_path}
rm -rf ${mysql_path}/src
rm -rf ${mysql_path}/data
mkdir -p ${mysql_path}/data
chown -R mysql:mysql ${mysql_path}
chmod -R 700 ${mysql_path}

${mysql_path}/bin/mysqld --initialize-insecure --user=mysql --basedir=${mysql_path} --datadir=${mysql_path}/data

# 软链接
ln -sf ${mysql_path}/bin/* /usr/bin/

# 写入 systemd 配置
cat >/etc/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network-online.target
Wants=network-online.target
After=syslog.target
After=local-fs.target remote-fs.target
Requires=local-fs.target remote-fs.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql

Type=forking

PIDFile=${mysql_path}/mysqld.pid

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Execute pre and post scripts as root
PermissionsStartOnly=true

# Start main service
ExecStart=${mysql_path}/bin/mysqld --daemonize --pid-file=${mysql_path}/mysqld.pid \$MYSQLD_OPTS

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 500000

Restart=on-failure

RestartPreventExitStatus=1

# Set enviroment variable MYSQLD_PARENT_PID. This is required for restart.
Environment=MYSQLD_PARENT_PID=1

PrivateTmp=false
EOF

systemctl daemon-reload
systemctl enable --now mysqld
if [ "$?" != "0" ]; then
    error "启动失败"
fi

# 等待 MySQL 启动
for i in {1..60}; do
    if mysqladmin ping &>/dev/null; then
        echo "MySQL 服务已启动"
        break
    fi
    echo "等待 MySQL 启动..."
    sleep 1
done

if ! mysqladmin ping &>/dev/null; then
    error "MySQL 启动超时"
fi

root_password=$(cat /dev/urandom | head -n 16 | md5sum | head -c 16)
${mysql_path}/bin/mysqladmin -u root password ${root_password}
if [ "$?" != "0" ]; then
    error "初始化密码失败"
fi
${mysql_path}/bin/mysql -uroot -p${root_password} -e "DROP DATABASE test;"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "DELETE FROM mysql.user WHERE user='';"
${mysql_path}/bin/mysql -uroot -p${root_password} -e "FLUSH PRIVILEGES;"

panel-cli app write mysql ${channel} ${version}
panel-cli setting write mysql_root_password ${root_password}

echo -e $HR
echo "安装完成"
echo -e $HR