#!/bin/sh

# Define the version number
VERSION="v4.1.1"

# Determine the system architecture
ARCH=$(uname -m)
case $ARCH in
    x86_64) ARCH="amd64" ;;
    i386) ARCH="i386" ;;
    aarch64) ARCH="aarch64" ;;
    armv7l) ARCH="armv7l" ;;
    *) echo "Architecture $ARCH is not supported by this script."; exit 1 ;;
esac

# Define the download URL and filename based on the architecture
URL="https://dl.nssurge.com/snell/snell-server-${VERSION}-linux-${ARCH}.zip"
FILENAME="snell-server-${VERSION}-linux-${ARCH}.zip"

# Check and install unzip if not present
if ! command -v unzip > /dev/null 2>&1; then
    read -p "unzip could not be found, do you want to install unzip? (y/n): " install_zip
    if [ "$install_zip" != "${install_zip#[Yy]}" ]; then
        echo "Installing unzip..."
        # Determine the package manager and install unzip
        if command -v apt-get > /dev/null 2>&1; then
            sudo apt-get update && sudo apt-get install -y unzip
        elif command -v yum > /dev/null 2>&1; then
            sudo yum install -y unzip
        elif command -v dnf > /dev/null 2>&1; then
            sudo dnf install -y unzip
        elif command -v pacman > /dev/null 2>&1; then
            sudo pacman -S --noconfirm unzip
        else
            echo "No suitable package manager found. Please install unzip manually."
            exit 1
        fi
    else
        echo "unzip is required for this script. Exiting."
        exit 1
    fi
fi

# Check if curl is installed, if not, prompt to install it
if ! command -v curl > /dev/null 2>&1; then
    read -p "curl could not be found, do you want to install curl? (y/n): " install_curl
    if [ "$install_curl" != "${install_curl#[Yy]}" ]; then
        echo "Installing curl..."
        # Determine the package manager and install curl
        if command -v apt-get > /dev/null 2>&1; then
            sudo apt-get update && sudo apt-get install -y curl
        elif command -v yum > /dev/null 2>&1; then
            sudo yum install -y curl
        elif command -v dnf > /dev/null 2>&1; then
            sudo dnf install -y curl
        elif command -v pacman > /dev/null 2>&1; then
            sudo pacman -S --noconfirm curl
        else
            echo "No suitable package manager found. Please install curl manually."
            exit 1
        fi
    else
        echo "curl is required for this script. Exiting."
        exit 1
    fi
fi

# Download snell-server using curl
echo "Downloading Snell Server from $URL..."
curl -L -o $FILENAME $URL

# Extract files
echo "Extracting files..."
unzip $FILENAME

# Check the extracted files
echo "Listing extracted files..."
ls -l

# Find the snell-server binary
EXTRACTED=$(find . -name 'snell-server' -type f)
if [ ! -f "$EXTRACTED" ]; then
    echo "Error: snell-server binary not found after extraction."
    exit 1
fi

# Move snell-server to /usr/local/bin
echo "Installing Snell Server..."
sudo mv "$EXTRACTED" /usr/local/bin/snell-server
sudo chmod +x /usr/local/bin/snell-server

# Create systemd service file
echo "Setting up systemd service..."
sudo tee /etc/systemd/system/snell.service > /dev/null << EOT
[Unit]
Description=Snell Proxy Service
After=network.target

[Service]
Type=simple
User=nobody
Group=nogroup
LimitNOFILE=32768
ExecStart=/usr/local/bin/snell-server -c /etc/snell/snell-server.conf
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=snell-server

[Install]
WantedBy=multi-user.target
EOT

# Create snell configuration file in the current directory
yes | /usr/local/bin/snell-server

# Create configuration directory and move the configuration file
sudo mkdir -p /etc/snell
sudo mv snell-server.conf /etc/snell/snell-server.conf

# Enable and start the service
echo "Enabling and starting Snell service..."
sudo systemctl enable snell.service
sudo systemctl start snell.service

echo "Snell Server installation and setup completed!"

# Display the configuration file content
cat /etc/snell/snell-server.conf