#!/bin/bash
#Install Latest Stable 1Panel Release

# 定义绿色字体的广告变量
GREEN='\033[0;32m'
NC='\033[0m' # 没有颜色（恢复默认）

clear

echo -e ""

# 显示广告信息
echo -e "${GREEN}广告位出租 200U 一个月${NC}"

echo -e ""

echo -e "${GREEN}QQ群：1055169232${NC}"
echo -e "${GREEN}TG群：@PanelLeYuan${NC}"

echo -e ""

# 倒计时
for i in {3..1}; do
    echo "$i"
    sleep 1
done

echo -e ""

echo "倒计时结束，继续进行安装..."

sleep 2

clear

osCheck=`uname -a`
if [[ $osCheck =~ 'x86_64' ]];then
    architecture="amd64"
elif [[ $osCheck =~ 'arm64' ]] || [[ $osCheck =~ 'aarch64' ]];then
    architecture="arm64"
elif [[ $osCheck =~ 'armv7l' ]];then
    architecture="armv7"
elif [[ $osCheck =~ 'ppc64le' ]];then
    architecture="ppc64le"
elif [[ $osCheck =~ 's390x' ]];then
    architecture="s390x"
else
    clear
    echo "暂不支持的系统架构，请参阅官方文档，选择受支持的系统。"
    exit 1
fi

if [[ ! ${INSTALL_MODE} ]];then
	INSTALL_MODE="stable"
else
    if [[ ${INSTALL_MODE} != "dev" && ${INSTALL_MODE} != "stable" ]];then
        echo "请输入正确的安装模式（dev or stable）"
        exit 1
    fi
fi

VERSION=$(curl -s https://resource.1panel.sb/1panel/package/${INSTALL_MODE}/latest)
HASH_FILE_URL="https://resource.1panel.sb/1panel/package/${INSTALL_MODE}/${VERSION}/release/checksums.txt"

if [[ "x${VERSION}" == "x" ]];then
    echo "获取最新版本失败，请稍候重试"
    exit 1
fi

package_file_name="1panel-${VERSION}-linux-${architecture}.tar.gz"
package_download_url="https://resource.1panel.sb/1panel/package/${INSTALL_MODE}/${VERSION}/release/${package_file_name}"
expected_hash=$(curl -s "$HASH_FILE_URL" | grep "$package_file_name" | awk '{print $1}')

if [ -f ${package_file_name} ];then
    actual_hash=$(sha256sum "$package_file_name" | awk '{print $1}')
    if [[ "$expected_hash" == "$actual_hash" ]];then
        echo "安装包已存在，跳过下载"
        rm -rf 1panel-${VERSION}-linux-${architecture}
        tar zxvf ${package_file_name}
        cd 1panel-${VERSION}-linux-${architecture}
        /bin/bash install.sh
        exit 0
    else
        echo "已存在安装包，但是哈希值不一致，开始重新下载"
        rm -f ${package_file_name}
    fi
fi

echo "开始下载 1Panel ${VERSION} 版本在线安装包"
echo "安装包下载地址： ${package_download_url}"

curl -LOk -o ${package_file_name} ${package_download_url}
if [ ! -f ${package_file_name} ];then
	echo "下载安装包失败，请稍候重试。"
	exit 1
fi

tar zxvf ${package_file_name}
if [ $? != 0 ];then
	echo "下载安装包失败，请稍候重试。"
	rm -f ${package_file_name}
	exit 1
fi
cd 1panel-${VERSION}-linux-${architecture}

echo -e ""
# 显示广告信息
echo -e "${GREEN}广告位出租 200U 一个月${NC}"

echo -e ""

echo -e "${GREEN}QQ群：1055169232${NC}"
echo -e "${GREEN}TG群：@PanelLeYuan${NC}"

echo -e ""

# 倒计时
for i in {3..1}; do
    echo "$i"
    sleep 1
done

/bin/bash install.sh


