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

if ! id -u "redis" >/dev/null 2>&1; then
    groupadd redis
    useradd -s /sbin/nologin -g redis redis
fi

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

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

# 下载源码
wget --retry-connrefused --retry-on-host-error --retry-on-http-error=429,500,502,503,504 -t 10 -T 120 -O ${redis_path}/redis-${version}.tar.gz ${download_url}/redis/redis-${version}.tar.gz
wget --retry-connrefused --retry-on-host-error --retry-on-http-error=429,500,502,503,504 -t 10 -T 120 -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 -rf ${redis_path}
    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
    rm -rf ${redis_path}
    error "Redis 编译失败"
fi
make PREFIX=${redis_path} install
if [ ! -f "${redis_path}/bin/redis-server" ]; then
    rm -rf ${redis_path}
    error "Redis 安装失败"
fi

# 设置软链接
ln -sf ${redis_path}/bin/* /usr/bin/

# 设置配置文件
VM_OVERCOMMIT_MEMORY=$(cat /etc/sysctl.conf | grep vm.overcommit_memory)
NET_CORE_SOMAXCONN=$(cat /etc/sysctl.conf | grep net.core.somaxconn)
if [ -z "${VM_OVERCOMMIT_MEMORY}" ] && [ -z "${NET_CORE_SOMAXCONN}" ]; then
    echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf
    echo "net.core.somaxconn = 1024" >>/etc/sysctl.conf
    sysctl -p
fi

sed -i "s|dir \./|dir ${redis_path}/|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
sed -i 's/# maxmemory-policy.*/maxmemory-policy allkeys-lfu/g' ${redis_path}/redis.conf

if [ ${ARCH} == "aarch64" ]; then
    echo "ignore-warnings ARM64-COW-BUG" >>${redis_path}/redis.conf
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
chmod 644 /etc/systemd/system/redis.service

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

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

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