#!/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}
pureftpd_path="${setupPath}/server/pure-ftpd"
j=$(calculate_j)

# 安装依赖
if [ ${OS} == "rhel" ]; then
    dnf makecache -y
    dnf groupinstall "Development Tools" -y
elif [ ${OS} == "debian" ] || [ ${OS} == "ubuntu" ]; then
    apt-get update
    apt-get install build-essential -y
else
    error "不支持的操作系统"
fi
if [ "$?" != "0" ]; then
    error "安装依赖软件失败"
fi

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

# 停止已有服务
systemctl stop pure-ftpd

# 准备安装目录
cp ${pureftpd_path}/etc/pureftpd.passwd /tmp/pureftpd.passwd
cp ${pureftpd_path}/etc/pureftpd.pdb /tmp/pureftpd.pdb
cp ${pureftpd_path}/etc/pureftpd.conf /tmp/pureftpd.conf
systemctl stop pure-ftpd.service

cd ${pureftpd_path}
rm -rf src

wget -T 60 -t 3 -O ${pureftpd_path}/pure-ftpd-${version}.tar.gz ${download_url}/pureftpd/pure-ftpd-${version}.tar.gz
wget -T 20 -t 3 -O ${pureftpd_path}/pure-ftpd-${version}.tar.gz.sha256 ${download_url}/pureftpd/pure-ftpd-${version}.tar.gz.sha256

if ! sha256sum --status -c pure-ftpd-${version}.tar.gz.sha256; then
    rm -f pure-ftpd-${version}.tar.gz
    rm -f pure-ftpd-${version}.tar.gz.sha256
    error "Pure-Ftpd 校验失败"
fi

tar -xvf pure-ftpd-${version}.tar.gz
rm -f pure-ftpd-${version}.tar.gz
rm -f pure-ftpd-${version}.tar.gz.sha256
mv pure-ftpd-${version} src
cd src

./configure --prefix=${pureftpd_path} CFLAGS=-O2 --with-puredb --with-quotas --with-cookie --with-virtualhosts --with-diraliases --with-sysquotas --with-ratios --with-altlog --with-paranoidmsg --with-shadow --with-welcomemsg --with-throttling --with-uploadscript --with-language=simplified-chinese --with-rfc2640 --with-ftpwho --with-tls
if [ "$?" != "0" ]; then
    error "Pure-Ftpd 编译初始化失败"
fi

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

make install
if [ ! -f "${pureftpd_path}/bin/pure-pw" ]; then
    error "Pure-Ftpd 安装失败"
fi

# 还原配置
cp /tmp/pureftpd.passwd ${pureftpd_path}/etc/pureftpd.passwd
cp /tmp/pureftpd.pdb ${pureftpd_path}/etc/pureftpd.pdb
cp /tmp/pureftpd.conf ${pureftpd_path}/etc/pureftpd.conf

systemctl daemon-reload
systemctl start pure-ftpd
if [ "$?" != "0" ]; then
    error "启动失败"
fi

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

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