## 自动更新naiveproxy脚本
# 脚本会自动下载最新版本的naiveproxy并保存到/var/www/download/naive目录下，并记录版本号到version.txt文件中。
# 脚本会自动下载最新版本的naiveproxy插件并保存到/var/www/download/naive目录下，并命名为naiveproxy-plugin-android.apk。
# 脚本会记录日志到/var/www/download/naive/.update.log文件中。

#!/bin/sh
# 测试代码。获取脚本位置并进入脚本
# SCRIPT_DIR=$(cd $(dirname $0); pwd)
# cd $SCRIPT_DIR
#DOWNLOAD_DIR=$SCRIPT_DIR
DOWNLOAD_DIR="/var/www/download/naive"
LOG_FILE="$DOWNLOAD_DIR/.update.log"
VERSION_FILE="$DOWNLOAD_DIR/version.txt"
REPO="klzgrad/naiveproxy"
API_URL="https://api.github.com/repos/$REPO/releases/latest"
VERSIONS=("linux-x64" "mac-arm64" "mac-x64")
NAME="naiveproxy"
TAIL="tar.xz"
# 检查jq curl 是否安装，如果未安装，则退出脚本
for cmd in jq curl; do
    if ! command -v $cmd &>/dev/null; then
        echo "$cmd 未安装，请安装后再运行脚本"
        if [ -f /etc/redhat-release ]; then
            sudo yum install -y $cmd
        elif [ -f /etc/lsb-release ]; then
            sudo apt-get install -y $cmd
        else
            echo "系统不支持，请手动安装 $cmd"
            exit 1
        fi
    fi
done

LATEST_VERSION=$(curl -s $API_URL | jq -r '.tag_name')
# 如果LATEST_VERSION为空或者为null，说明获取失败，退出脚本
if [ -z "$LATEST_VERSION" ] || [ "$LATEST_VERSION" = "null" ]; then
    echo "获取版本号失败，退出脚本"
    exit 1
fi

# 检查本地是否有版本文件
if [ -f "$DOWNLOAD_DIR/version.txt" ]; then
    LOCAL_VERSION=$(cat "$DOWNLOAD_DIR/version.txt")
else
    LOCAL_VERSION=""
fi

# 比较本地版本和最新版本
if [ "$LATEST_VERSION" != "$LOCAL_VERSION" ]; then
#    echo "版本不一致，下载最新版本 $LATEST_VERSION"

    # 删除旧文件
    rm -rf "$DOWNLOAD_DIR"/*.${TAIL}
    rm -rf "$DOWNLOAD_DIR"/*.apk

    # 下载新版本
    for VERSION in "${VERSIONS[@]}"; do
        FILENAME="${NAME}-${LATEST_VERSION}-${VERSION}.${TAIL}"
        URL="https://github.com/$REPO/releases/download/$LATEST_VERSION/$FILENAME"
        if ! curl -s -o "$DOWNLOAD_DIR/$FILENAME" "$URL"; then
            echo "下载 $FILENAME 失败"
            echo "$(date) [FAILD] $FILENAME DOWNLOAD FAILED." >>"$LOG_FILE"
            continue
        fi
        # 修改最新的文件名
        if [ -f "$DOWNLOAD_DIR/$FILENAME" ]; then
            mv "$DOWNLOAD_DIR/$FILENAME" "$DOWNLOAD_DIR/${NAME}-${VERSION}.${TAIL}"
            echo "$(date) [SUCESS]  UPDATED $FILENAME." >>"$LOG_FILE"
#            echo "下载 $FILENAME 成功"
        else
            echo "下载 $FILENAME 失败"
            echo "$(date) [FAILD] $FILENAME DOWNLOAD FAILED." >>"$LOG_FILE"
        fi
    done

    # 下载apk插件
    APK_FILENAME="naiveproxy-plugin-${LATEST_VERSION}-arm64-v8a.apk"
    URL="https://github.com/$REPO/releases/download/$LATEST_VERSION/$APK_FILENAME"
    if ! curl -s -o "$DOWNLOAD_DIR/$APK_FILENAME" "$URL"; then
        echo "下载 $APK_FILENAME 失败"
        echo "$(date) [FAILD] $APK_FILENAME DOWNLOAD FAILED." >>"$LOG_FILE"
    elif [ -f "$DOWNLOAD_DIR/$APK_FILENAME" ]; then
        # 如果文件下载成功，则重命名文件
        mv "$DOWNLOAD_DIR/$APK_FILENAME" "$DOWNLOAD_DIR/naiveproxy-plugin-android.apk"
#        echo "下载 $APK_FILENAME 成功"
        echo "$(date) [SUCESS]  UPDATED $_APK_FILENAME." >>"$LOG_FILE"
    else
        echo "下载 $APK_FILENAME 失败"
        echo "$(date) [FAILD] $APK_FILENAME DOWNLOAD FAILED." >>"$LOG_FILE"
    fi
    # 记录日志到文件

    # 保存最新版本号
    echo "$LATEST_VERSION" >"$VERSION_FILE"
else
    echo "版本一致，无需更新"
    # 记录日志到文:
    echo "$(date) $LATEST_VERSION NO UPDATE NEEDED" >>"$LOG_FILE"
fi
