#!/bin/bash

# Determine OS and architecture
OS=$(uname -s)
ARCH=$(uname -m)

# swoole-cli 版本号
VERSION="5.1.4"

# Set the download URL based on OS and architecture
case "$OS" in
    Linux)
        case "$ARCH" in
            arm64)
                FILE_URL="https://wenda-1252906962.file.myqcloud.com/dist/swoole-cli-v${VERSION}-linux-arm64.tar.xz"
                ;;
            x86_64)
                FILE_URL="https://wenda-1252906962.file.myqcloud.com/dist/swoole-cli-v${VERSION}-linux-x64.tar.xz"
                ;;
            *)
                echo "Unsupported architecture: $ARCH"
                exit 1
                ;;
        esac
        ;;
    Darwin)
        case "$ARCH" in
            arm64)
                FILE_URL="https://wenda-1252906962.file.myqcloud.com/dist/swoole-cli-v${VERSION}-macos-arm64.tar.xz"
                ;;
            x86_64)
                FILE_URL="https://wenda-1252906962.file.myqcloud.com/dist/swoole-cli-v${VERSION}-macos-x64.tar.xz"
                ;;
            *)
                echo "Unsupported architecture: $ARCH"
                exit 1
                ;;
        esac
        ;;
    *)
        echo "Unsupported OS: $OS"
        exit 1
        ;;
esac

# Check if Node.js is installed
if command -v node &> /dev/null; then
    echo "Node.js is installed. Proceeding with Node.js script setup."

    # Download the Node.js proxy script
    echo "Downloading index.js..."
    if ! curl -o "index.js" "https://x-mo.cn:9009/XiaoMo/alist-proxy/raw/branch/master/index.js"; then
        echo "Failed to download index.js. Please check your internet connection and try again."
        exit 1
    fi

    # Run the Node.js proxy service
    echo "Running Node.js proxy service..."
    nohup node index.js > /dev/null 2>&1 &
    echo "Node.js proxy service running in the background. You can check the logs with 'tail -f nohup.out'."
else
    echo "Node.js is not installed. Proceeding with swoole-cli setup."

    # Set the fixed file name
    FILE_NAME="swoole-cli.tar.xz"

    # Create a temporary directory
    TEMP_DIR=$(mktemp -d)
    mkdir -p "$TEMP_DIR"

    # Download the file to the temporary directory
    echo "Downloading $FILE_NAME from $FILE_URL to $TEMP_DIR..."
    if ! curl -o "$TEMP_DIR/$FILE_NAME" "$FILE_URL"; then
        echo "Download failed. Please check your internet connection and try again."
        exit 1
    fi

    # Verify the download
    if [ ! -f "$TEMP_DIR/$FILE_NAME" ]; then
        echo "Downloaded file not found in $TEMP_DIR. Download may have failed."
        exit 1
    fi

    # Extract the file to the temporary directory
    tar -xf "$TEMP_DIR/$FILE_NAME" -C "$TEMP_DIR" || {
        echo "Extraction failed. Please make sure you have the necessary tools to extract the file."
        exit 1
    }

    # Move the extracted file to the current directory
    mv "$TEMP_DIR"/swoole-cli .

    # Remove unnecessary files
    echo "Removing unnecessary files..."
    rm -rf "$TEMP_DIR"

    # Check if index.php file exists, if not, download it
    if [ ! -f "index.php" ]; then
        echo "Downloading index.php..."
        if ! curl -o "index.php" "https://x-mo.cn:9009/XiaoMo/alist-proxy/raw/branch/master/index.php"; then
            echo "Failed to download index.php. Please check your internet connection and try again."
            exit 1
        fi
    fi

    # Run the PHP indexer
    echo "Running PHP indexer..."
    nohup ./swoole-cli ./index.php > /dev/null 2>&1 &
    echo "PHP indexer running in the background. You can check the logs with 'tail -f nohup.out'."
fi