#!/bin/bash

# ==============================================================================
#
# Script to install or update sing-box on Debian-based systems.
#
# This script performs the following steps:
# 1. Checks for root privileges.
# 2. Detects the system's processor architecture (ARM64 or AMD64).
# 3. Downloads the appropriate sing-box .deb package.
# 4. Installs the package using dpkg.
# 5. Cleans up downloaded files.
# 6. Reloads the systemd daemon and restarts the sing-box service.
#
# ==============================================================================

# Exit immediately if a command exits with a non-zero status.
# This ensures that the script will stop if any step fails.
set -e

# --- Step 1: Check for root privileges ---
# The script needs root access to install packages and manage services.
if [ "$(id -u)" -ne 0 ]; then
  echo "Error: This script must be run as root. Please use 'sudo'." >&2
  exit 1
fi

# --- Step 2: Detect Processor Architecture ---
# 'uname -m' prints the machine hardware name (e.g., x86_64, aarch64).
ARCH=$(uname -m)
echo "Detected system architecture: $ARCH"

# --- Define download variables based on architecture ---
DOWNLOAD_URL=""
DEB_FILE=""

if [[ "$ARCH" == "aarch64" ]] || [[ "$ARCH" == "arm64" ]]; then
    # Set variables for ARM64 architecture.
    echo "Identified as ARM64 architecture."
    DOWNLOAD_URL="https://ashtreetown.org/resource/software/sing-box/latest/sing-box-linux_arm64.deb"
    DEB_FILE="sing-box-linux_arm64.deb"
elif [[ "$ARCH" == "x86_64" ]] || [[ "$ARCH" == "amd64" ]]; then
    # Set variables for x86_64 (AMD64) architecture.
    echo "Identified as x86_64 (AMD64) architecture."
    DOWNLOAD_URL="https://ashtreetown.org/resource/software/sing-box/latest/sing-box-linux_amd64.deb"
    DEB_FILE="sing-box-linux_amd64.deb"
else
    # Handle unsupported architectures.
    echo "Error: Unsupported architecture '$ARCH'."
    echo "This script only supports aarch64 (ARM64) and x86_64 (AMD64)."
    exit 1
fi

# --- Step 3: Download the Package ---
# Create a temporary directory to store the downloaded file.
# This helps to keep the filesystem clean.
TMP_DIR=$(mktemp -d)
echo "Downloading $DEB_FILE to temporary directory $TMP_DIR ..."

# Use wget to download the file.
# -q (quiet) is combined with --show-progress for a cleaner output.
# -O specifies the output file path.
# The script will exit here if wget fails because 'set -e' is active.
wget -q --show-progress -O "$TMP_DIR/$DEB_FILE" "$DOWNLOAD_URL"

echo "Download complete."

# --- Step 4: Install the Package ---
# Use 'dpkg -i' to install the .deb package.
# This command works for both new installations and upgrades.
echo "Installing/Updating sing-box..."
dpkg -i "$TMP_DIR/$DEB_FILE"

# --- Step 5: Clean up ---
# Remove the temporary directory and the downloaded file.
echo "Cleaning up temporary files..."
rm -r "$TMP_DIR"

# --- Step 6: Reload systemd and Restart Service ---
# 'systemctl daemon-reload' is needed to make systemd aware of any
# changes to service files that may have come with the package.
echo "Reloading systemd daemon..."
systemctl daemon-reload

# 'systemctl restart sing-box' restarts the service to apply the update.
echo "Restarting sing-box service..."
systemctl restart sing-box

echo ""
echo "sing-box installation/update successful!"
echo "You can check the service status with the command: 'systemctl status sing-box'"
