#!/bin/bash

set -e

# Detect architecture
ARCH=$(uname -m)
case "${ARCH}" in
  x86_64 | x64 | amd64) XUI_ARCH="amd64" ;;
  i*86 | x86) XUI_ARCH="386" ;;
  armv8* | armv8 | arm64 | aarch64) XUI_ARCH="arm64" ;;
  armv7* | armv7) XUI_ARCH="armv7" ;;
  armv6* | armv6) XUI_ARCH="armv6" ;;
  armv5* | armv5) XUI_ARCH="armv5" ;;
  s390x) XUI_ARCH="s390x" ;;
  *) XUI_ARCH="amd64" ;;
esac

# Detect OS
if [[ -f /etc/os-release ]]; then
    source /etc/os-release
    release=$ID
elif [[ -f /usr/lib/os-release ]]; then
    source /usr/lib/os-release
    release=$ID
else
    echo "Failed to detect OS"
    exit 1
fi

cd /root/

# Cleanup old files
rm -rf x-ui/ /usr/local/x-ui/ /usr/bin/x-ui

# Download automatically
DOWNLOAD_URL="https://trex.emeraldteam.ir/Apps/x-ui-linux-${XUI_ARCH}.tar.gz"
echo "[+] Downloading from: $DOWNLOAD_URL"

curl -fLo x-ui-linux-${XUI_ARCH}.tar.gz "$DOWNLOAD_URL"

# Extract
tar zxvf x-ui-linux-${XUI_ARCH}.tar.gz

# Permissions
chmod +x x-ui/x-ui x-ui/bin/xray-linux-* x-ui/x-ui.sh
cp x-ui/x-ui.sh /usr/bin/x-ui

# Service setup
if [ -f "x-ui/x-ui.service" ]; then
    cp -f x-ui/x-ui.service /etc/systemd/system/
elif [[ "$release" == "ubuntu" || "$release" == "debian" || "$release" == "armbian" ]]; then
    if [ -f "x-ui/x-ui.service.debian" ]; then
        cp -f x-ui/x-ui.service.debian /etc/systemd/system/x-ui.service
    else
        echo "Service file not found, downloading..."
        curl -fLo /etc/systemd/system/x-ui.service https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.service.debian
    fi
else
    if [ -f "x-ui/x-ui.service.rhel" ]; then
        cp -f x-ui/x-ui.service.rhel /etc/systemd/system/x-ui.service
    else
        echo "Service file not found, downloading..."
        curl -fLo /etc/systemd/system/x-ui.service https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.service.rhel
    fi
fi

# Move and start
mv x-ui/ /usr/local/
systemctl daemon-reload
systemctl enable x-ui
systemctl restart x-ui

echo "[+] x-ui installed and started successfully!"