#!/bin/bash
#
# INTERCEPT Setup Script
# Installs Python dependencies and checks for external tools
#

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}"
echo "  ___ _   _ _____ _____ ____   ____ _____ ____ _____ "
echo " |_ _| \\ | |_   _| ____|  _ \\ / ___| ____|  _ \\_   _|"
echo "  | ||  \\| | | | |  _| | |_) | |   |  _| | |_) || |  "
echo "  | || |\\  | | | | |___|  _ <| |___| |___|  __/ | |  "
echo " |___|_| \\_| |_| |_____|_| \\_\\\\____|_____|_|    |_|  "
echo -e "${NC}"
echo "Signal Intelligence Platform - Setup Script"
echo "============================================"
echo ""

# Detect OS
detect_os() {
    if [[ "$OSTYPE" == "darwin"* ]]; then
        OS="macos"
        PKG_MANAGER="brew"
    elif [[ -f /etc/debian_version ]]; then
        OS="debian"
        PKG_MANAGER="apt"
    elif [[ -f /etc/redhat-release ]]; then
        OS="redhat"
        PKG_MANAGER="dnf"
    elif [[ -f /etc/arch-release ]]; then
        OS="arch"
        PKG_MANAGER="pacman"
    else
        OS="unknown"
        PKG_MANAGER="unknown"
    fi
    echo -e "${BLUE}Detected OS:${NC} $OS (package manager: $PKG_MANAGER)"
}

# Check if a command exists
check_cmd() {
    command -v "$1" &> /dev/null
}

# Install Python dependencies
install_python_deps() {
    echo ""
    echo -e "${BLUE}[1/3] Installing Python dependencies...${NC}"

    if ! check_cmd python3; then
        echo -e "${RED}Error: Python 3 is not installed${NC}"
        echo "Please install Python 3.7 or later"
        exit 1
    fi

    PYTHON_VERSION=$(python3 -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
    echo "Python version: $PYTHON_VERSION"

    if check_cmd pip3; then
        pip3 install -r requirements.txt
    elif check_cmd pip; then
        pip install -r requirements.txt
    else
        python3 -m pip install -r requirements.txt
    fi

    echo -e "${GREEN}Python dependencies installed successfully${NC}"
}

# Check external tools
check_tools() {
    echo ""
    echo -e "${BLUE}[2/3] Checking external tools...${NC}"
    echo ""

    MISSING_TOOLS=()

    # Core SDR tools
    echo "Core SDR Tools:"
    check_tool "rtl_fm" "RTL-SDR FM demodulator"
    check_tool "rtl_test" "RTL-SDR device detection"
    check_tool "multimon-ng" "Pager decoder"
    check_tool "rtl_433" "433MHz sensor decoder"
    check_tool "dump1090" "ADS-B decoder"

    echo ""
    echo "Additional SDR Hardware (optional):"
    check_tool "SoapySDRUtil" "SoapySDR (for LimeSDR/HackRF)"
    check_tool "LimeUtil" "LimeSDR tools"
    check_tool "hackrf_info" "HackRF tools"

    echo ""
    echo "WiFi Tools:"
    check_tool "airmon-ng" "WiFi monitor mode"
    check_tool "airodump-ng" "WiFi scanner"

    echo ""
    echo "Bluetooth Tools:"
    check_tool "bluetoothctl" "Bluetooth controller"
    check_tool "hcitool" "Bluetooth HCI tool"

    if [ ${#MISSING_TOOLS[@]} -gt 0 ]; then
        echo ""
        echo -e "${YELLOW}Some tools are missing. See installation instructions below.${NC}"
    fi
}

check_tool() {
    local cmd=$1
    local desc=$2
    if check_cmd "$cmd"; then
        echo -e "  ${GREEN}✓${NC} $cmd - $desc"
    else
        echo -e "  ${RED}✗${NC} $cmd - $desc ${YELLOW}(not found)${NC}"
        MISSING_TOOLS+=("$cmd")
    fi
}

# Show installation instructions
show_install_instructions() {
    echo ""
    echo -e "${BLUE}[3/3] Installation instructions for missing tools${NC}"
    echo ""

    if [ ${#MISSING_TOOLS[@]} -eq 0 ]; then
        echo -e "${GREEN}All tools are installed!${NC}"
        return
    fi

    echo "Run the following commands to install missing tools:"
    echo ""

    if [[ "$OS" == "macos" ]]; then
        echo -e "${YELLOW}macOS (Homebrew):${NC}"
        echo ""

        # Check if Homebrew is installed
        if ! check_cmd brew; then
            echo "First, install Homebrew:"
            echo '  /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"'
            echo ""
        fi

        echo "# Core SDR tools"
        echo "brew install librtlsdr multimon-ng rtl_433 dump1090-mutability"
        echo ""
        echo "# LimeSDR support (optional)"
        echo "brew install soapysdr limesuite soapylms7"
        echo ""
        echo "# HackRF support (optional)"
        echo "brew install hackrf soapyhackrf"
        echo ""
        echo "# WiFi tools"
        echo "brew install aircrack-ng"

    elif [[ "$OS" == "debian" ]]; then
        echo -e "${YELLOW}Ubuntu/Debian:${NC}"
        echo ""
        echo "# Core SDR tools"
        echo "sudo apt update"
        echo "sudo apt install rtl-sdr multimon-ng rtl-433 dump1090-mutability"
        echo ""
        echo "# LimeSDR support (optional)"
        echo "sudo apt install soapysdr-tools limesuite soapysdr-module-lms7"
        echo ""
        echo "# HackRF support (optional)"
        echo "sudo apt install hackrf soapysdr-module-hackrf"
        echo ""
        echo "# WiFi tools"
        echo "sudo apt install aircrack-ng"
        echo ""
        echo "# Bluetooth tools"
        echo "sudo apt install bluez bluetooth"

    elif [[ "$OS" == "arch" ]]; then
        echo -e "${YELLOW}Arch Linux:${NC}"
        echo ""
        echo "# Core SDR tools"
        echo "sudo pacman -S rtl-sdr multimon-ng"
        echo "yay -S rtl_433 dump1090"
        echo ""
        echo "# LimeSDR/HackRF support (optional)"
        echo "sudo pacman -S soapysdr limesuite hackrf"

    elif [[ "$OS" == "redhat" ]]; then
        echo -e "${YELLOW}Fedora/RHEL:${NC}"
        echo ""
        echo "# Core SDR tools"
        echo "sudo dnf install rtl-sdr"
        echo "# multimon-ng, rtl_433, dump1090 may need to be built from source"

    else
        echo "Please install the following tools manually:"
        for tool in "${MISSING_TOOLS[@]}"; do
            echo "  - $tool"
        done
    fi
}

# RTL-SDR udev rules (Linux only)
setup_udev_rules() {
    if [[ "$OS" != "macos" ]] && [[ "$OS" != "unknown" ]]; then
        echo ""
        echo -e "${BLUE}RTL-SDR udev rules (Linux only):${NC}"
        echo ""
        echo "If your RTL-SDR is not detected, you may need to add udev rules:"
        echo ""
        echo "sudo bash -c 'cat > /etc/udev/rules.d/20-rtlsdr.rules << EOF"
        echo 'SUBSYSTEM=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="2838", MODE="0666"'
        echo 'SUBSYSTEM=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="2832", MODE="0666"'
        echo "EOF'"
        echo ""
        echo "sudo udevadm control --reload-rules"
        echo "sudo udevadm trigger"
        echo ""
        echo "Then unplug and replug your RTL-SDR device."
    fi
}

# Main
main() {
    detect_os
    install_python_deps
    check_tools
    show_install_instructions
    setup_udev_rules

    echo ""
    echo "============================================"
    echo -e "${GREEN}Setup complete!${NC}"
    echo ""
    echo "To start INTERCEPT:"
    echo "  sudo python3 intercept.py"
    echo ""
    echo "Then open http://localhost:5050 in your browser"
    echo ""
}

main "$@"
