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

action=${1}
version=${2}
php_path="${setup_path}/server/php/${version}"

add_arm=""
if [ ${ARCH} == "aarch64" ]; then
    add_arm="_arm"
fi

Install() {
    # 检查是否已经安装
    is_install=$(cat ${php_path}/etc/php.ini | grep 'ioncube_loader_lin')
    if [ "${is_install}" != "" ]; then
        error "PHP-${version} 已安装 ionCube"
    fi

    mkdir /usr/local/ioncube
    cd /usr/local/ioncube
    wget --retry-connrefused --retry-on-host-error --retry-on-http-error=429,500,502,503,504 -t 10 -T 120 -O /usr/local/ioncube/ioncube_loader_lin_${version}${add_arm}.so ${download_url}/php_exts/ioncube_loader_lin_${version}${add_arm}.so
    wget --retry-connrefused --retry-on-host-error --retry-on-http-error=429,500,502,503,504 -t 10 -T 120 -O /usr/local/ioncube/ioncube_loader_lin_${version}${add_arm}.so.sha256 ${download_url}/php_exts/ioncube_loader_lin_${version}${add_arm}.so.sha256

    if ! sha256sum --status -c /usr/local/ioncube/ioncube_loader_lin_${version}${add_arm}.so.sha256; then
        rm -f /usr/local/ioncube/ioncube_loader_lin_${version}${add_arm}.so
        rm -f /usr/local/ioncube/ioncube_loader_lin_${version}${add_arm}.so.sha256
        error "PHP-${version} ionCube 校验失败"
    fi

    rm -f /usr/local/ioncube/ioncube_loader_lin_${version}${add_arm}.so.sha256

    sed -i -e "/;panel/a\zend_extension=/usr/local/ioncube/ioncube_loader_lin_${version}${add_arm}.so" ${php_path}/etc/php.ini

    # 重载PHP
    systemctl reload php-fpm-${version}.service
    echo -e $HR
    echo "PHP-${version} ionCube 安装成功"
}

Uninstall() {
    # 检查是否已经安装
    is_install=$(cat ${php_path}/etc/php.ini | grep 'ioncube_loader_lin')
    if [ "${is_install}" == "" ]; then
        error "PHP-${version} 未安装 ionCube"
    fi

    rm -f /usr/local/ioncube/ioncube_loader_lin_${version}${add_arm}.so
    sed -i '/ioncube_loader_lin/d' ${php_path}/etc/php.ini

    # 重载PHP
    systemctl reload php-fpm-${version}.service
    echo -e $HR
    echo "PHP-${version} ionCube 卸载成功"
}

if [ "$action" == 'install' ]; then
    Install
fi
if [ "$action" == 'uninstall' ]; then
    Uninstall
fi