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

: '
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}
phpmyadmin_path="${setup_path}/server/phpmyadmin"
random_dir="$(cat /dev/urandom | head -n 16 | md5sum | head -c 10)"

# 准备安装目录
rm -rf ${phpmyadmin_path}
mkdir -p ${phpmyadmin_path}
cd ${phpmyadmin_path}

wget -T 60 -t 3 -O phpMyAdmin-${version}-all-languages.zip ${download_url}/phpmyadmin/phpMyAdmin-${version}-all-languages.zip
wget -T 20 -t 3 -O phpMyAdmin-${version}-all-languages.zip.sha256 ${download_url}/phpmyadmin/phpMyAdmin-${version}-all-languages.zip.sha256

if ! sha256sum --status -c phpMyAdmin-${version}-all-languages.zip.sha256; then
    rm -rf ${phpmyadmin_path}
    error "phpMyAdmin 校验失败"
fi

unzip -o phpMyAdmin-${version}-all-languages.zip
mv phpMyAdmin-${version}-all-languages phpmyadmin_${random_dir}
chown -R www:www ${phpmyadmin_path}
chmod -R 755 ${phpmyadmin_path}
rm -rf phpMyAdmin-${version}-all-languages.zip
rm -rf phpMyAdmin-${version}-all-languages.zip.sha256

# 判断PHP版本
php_version=""
if [ -d "${setup_path}/server/php/74" ]; then
    php_version="74"
fi
if [ -d "${setup_path}/server/php/80" ]; then
    php_version="80"
fi
if [ -d "${setup_path}/server/php/81" ]; then
    php_version="81"
fi
if [ -d "${setup_path}/server/php/82" ]; then
    php_version="82"
fi
if [ -d "${setup_path}/server/php/83" ]; then
    php_version="83"
fi
if [ -d "${setup_path}/server/php/84" ]; then
    php_version="84"
fi

if [ "${php_version}" == "" ]; then
    rm -rf ${phpmyadmin_path}
    error "未安装 PHP"
fi

# 写入 phpMyAdmin 配置文件
cat >${setup_path}/server/vhost/phpmyadmin.conf <<EOF
server
{
    listen 888;
    server_name phpmyadmin;
    index index.php;
    root ${setup_path}/server/phpmyadmin;

    include enable-php-${php_version}.conf;

    access_log ${setup_path}/wwwlogs/phpmyadmin.log;
    error_log ${setup_path}/wwwlogs/phpmyadmin.log;
}
EOF

# 设置文件权限
chown -R root:root ${setup_path}/server/vhost/phpmyadmin.conf
chmod -R 644 ${setup_path}/server/vhost/phpmyadmin.conf
chmod -R 755 ${phpmyadmin_path}
chown -R www:www ${phpmyadmin_path}

# 放行端口
firewall-cmd --permanent --zone=public --add-port=888/tcp
firewall-cmd --reload

panel-cli app write phpmyadmin ${channel} ${version}
systemctl reload nginx

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