#!/data/data/com.termux/files/usr/bin/bash

# Combined Hotspot Scanner Script
# This script handles root permissions automatically

echo "=================================="
echo "   Hotspot Network Scanner"
echo "=================================="

# Check if running as root
if [ "$EUID" -eq 0 ]; then
    # We're already root, run the scan directly
    CURRENT_SCRIPT=$(readlink -f "$0")
    SCRIPT_DIR=$(dirname "$CURRENT_SCRIPT")
    
    # Get your IP and network
    MY_IP=$(ip addr show | grep -E "wlan[0-9]" | grep "inet " | awk '{print $2}' | cut -d/ -f1 | head -1)
    
    if [ -z "$MY_IP" ]; then
        # Try common hotspot interfaces
        for IFACE in wlan1 wlan0 ap0; do
            MY_IP=$(ip addr show $IFACE 2>/dev/null | grep "inet " | awk '{print $2}' | cut -d/ -f1)
            [ -n "$MY_IP" ] && break
        done
    fi
    
    NETWORK=$(echo $MY_IP | cut -d. -f1-3)
    
    echo "Your IP: $MY_IP"
    echo "Scanning network: $NETWORK.0/24"
    echo "=================================="
    echo ""
    
    # ARP cache check (needs root)
    if [ -r /proc/net/arp ]; then
        echo "[*] Checking ARP cache..."
        cat /proc/net/arp 2>/dev/null | grep -v "IP address" | while read line; do
            MAC=$(echo $line | awk '{print $4}')
            if [ "$MAC" != "00:00:00:00:00:00" ] && [ "$MAC" != "" ] && [ "$MAC" != "00:00:00:00:00:00" ]; then
                IP=$(echo $line | awk '{print $1}')
                # Skip if IP is 0.0.0.0
                if [ "$IP" != "0.0.0.0" ]; then
                    echo "Found: $IP -> $MAC"
                fi
            fi
        done
    fi
    
    # Ping scan
    echo ""
    echo "[*] Performing ping scan (this will take about 30 seconds)..."
    echo "Active devices:"
    
    # Create temp file
    TEMP_FILE=/data/local/tmp/ping_results.txt
    > $TEMP_FILE
    
    # Scan IPs 1-254
    i=1
    while [ $i -le 254 ]; do
        ping -c 1 -W 1 $NETWORK.$i > /dev/null 2>&1 &
        i=$((i + 1))
    done
    
    # Wait for pings to complete
    sleep 3
    
    # Check which IPs responded
    i=1
    while [ $i -le 254 ]; do
        IP="$NETWORK.$i"
        # Skip our own IP
        if [ "$IP" != "$MY_IP" ]; then
            # Check ARP for MAC
            arp -n $IP 2>/dev/null | grep -v "incomplete" | grep "$IP" | while read line; do
                MAC=$(echo $line | awk '{print $3}')
                if [ "$MAC" != "(incomplete)" ] && [ "$MAC" != "" ]; then
                    echo "$IP - $MAC"
                fi
            done
        fi
        i=$((i + 1))
    done
    
    # Also try to get from ping (for non-ARP devices)
    i=1
    while [ $i -le 254 ]; do
        IP="$NETWORK.$i"
        if [ "$IP" != "$MY_IP" ]; then
            ping -c 1 -W 1 $IP > /dev/null 2>&1
            if [ $? -eq 0 ]; then
                # Check if we already found it via ARP
                if ! arp -n $IP 2>/dev/null | grep -q "$IP"; then
                    echo "$IP - (no MAC)"
                fi
            fi
        fi
        i=$((i + 1))
    done
    
    rm -f $TEMP_FILE
    
    echo ""
    echo "=================================="
    echo "Scan complete!"
    
else
    # Not root, try to re-run with root
    echo "[*] Not running as root. Attempting to gain root privileges..."
    
    # Check if we have su
    if command -v su >/dev/null 2>&1; then
        echo "[*] Using su to get root..."
        # Get the full path of this script
        SCRIPT_PATH=$(readlink -f "$0")
        # Re-run with root
        exec su -c "sh $SCRIPT_PATH"
    else
        echo "[!] su not found. Running without root (limited functionality)..."
        # Run without root
        exec sh "$0"
    fi
fi