#!/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}
redis_path="${setup_path}/server/redis"

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

# 停止已有服务
systemctl stop redis

# 读取信息
redis_config="${redis_path}/redis.conf"
redis_port=$(cat ${redis_config} | grep 'port ' | grep -v '#' | awk '{print $2}')
redis_pass=$(cat ${redis_config} | grep 'requirepass ' | grep -v '#' | awk '{print $2}')
redis_host=$(cat ${redis_config} | grep 'bind ' | grep -v '#' | awk '{print $2}')
redis_dir=$(cat ${redis_config} | grep 'dir ' | grep -v '#' | awk '{print $2}')

# 备份
if [ -z "${redis_pass}" ]; then
    redis-cli -p ${redis_port} <<EOF
SAVE
EOF
else
    redis-cli -p ${redis_port} -a ${redis_pass} <<EOF
SAVE
EOF
fi
mv -f ${redis_dir}/redisdump.rdb /tmp/redisdump.rdb.bak

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

# 下载源码
wget -T 120 -t 3 -O ${redis_path}/redis-${version}.tar.gz ${download_url}/redis/redis-${version}.tar.gz
wget -T 20 -t 3 -O ${redis_path}/redis-${version}.tar.gz.sha256 ${download_url}/redis/redis-${version}.tar.gz.sha256

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

tar -zxvf redis-${version}.tar.gz
rm -f redis-${version}.tar.gz
rm -f redis-${version}.tar.gz.sha256
mv redis-${version}/* ./ && rm -rf redis-${version}
mkdir -p ${redis_path}/bin

make BUILD_TLS=yes USE_SYSTEMD=yes -j${CORES}
if [ "$?" != "0" ]; then
    error "Redis 编译失败"
fi
make PREFIX=${redis_path} install
if [ ! -f "${redis_path}/bin/redis-server" ]; then
    error "Redis 升级失败"
fi

# 设置配置文件
sed -i 's/dir .\//dir \/www\/server\/redis\//g' ${redis_path}/redis.conf
sed -i 's/# supervised.*/supervised systemd/g' ${redis_path}/redis.conf
sed -i 's/daemonize.*/daemonize no/g' ${redis_path}/redis.conf

if [ ${ARCH} == "aarch64" ]; then
    echo "ignore-warnings ARM64-COW-BUG" >>${redis_path}/redis.conf
fi

# 恢复配置
if [ -n "${redis_pass}" ]; then
    sed -i "s!# requirepass .*!requirepass ${redis_pass}!g" ${redis_path}/redis.conf
fi
if [ -n "${redis_host}" ]; then
    sed -i "s!bind .*!bind ${redis_host}!g" ${redis_path}/redis.conf
fi
if [ -n "${redis_port}" ]; then
    sed -i "s!port .*!port ${redis_port}!g" ${redis_path}/redis.conf
fi
if [ -n "${redis_dir}" ]; then
    sed -i "s!dir .*!dir ${redis_dir}!g" ${redis_path}/redis.conf
fi

# 恢复数据
if [ -f "/tmp/redisdump.rdb.bak" ]; then
    mv /tmp/redisdump.rdb.bak ${redis_dir}/redisdump.rdb
fi

chown -R redis:redis ${redis_path}
chmod -R 700 ${redis_path}

# 设置服务
cp -r utils/systemd-redis_server.service /etc/systemd/system/redis.service
sed -i "s!ExecStart=.*!ExecStart=${redis_path}/bin/redis-server ${redis_path}/redis.conf!g" /etc/systemd/system/redis.service
sed -i "s!#User=.*!User=redis!g" /etc/systemd/system/redis.service
sed -i "s!#Group=.*!Group=redis!g" /etc/systemd/system/redis.service
sed -i "s!#WorkingDirectory=.*!WorkingDirectory=${redis_path}!g" /etc/systemd/system/redis.service

systemctl daemon-reload
systemctl start redis

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

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