#!/bin/sh

IFACE="wlan1"
SSID="vegetablack"
PSK="supermarioinreallife"
CHANNEL="11"
AP_IP="192.168.43.1"

[ "$(id -u)" -ne 0 ] && exit 1

echo "[*] Hard reset of Wi-Fi stack..."

# Full path to Android's svc
/system/bin/svc wifi disable 2>/dev/null
killall hostapd dnsmasq wpa_supplicant wificond 2>/dev/null
sleep 1
pkill -9 hostapd
sleep 3

# Delete the interface
iw dev $IFACE del 2>/dev/null
sleep 1

# Create a fresh AP interface
iw phy phy0 interface add $IFACE type __ap 2>/dev/null || \
iw phy phy0 interface add $IFACE type ap 2>/dev/null

sleep 1

ip link set $IFACE down 2>/dev/null
ip addr flush dev $IFACE 2>/dev/null
ip link set $IFACE up
sleep 1

# Simple hostapd config
cat > /tmp/hostapd.conf <<EOF
interface=$IFACE
driver=nl80211
ssid=$SSID
hw_mode=g
channel=$CHANNEL
auth_algs=1
ignore_broadcast_ssid=0
EOF

echo "[*] Starting hostapd (running in foreground)..."
echo "Look for the line: AP-ENABLED"
echo "Press Ctrl+C to stop later"
echo

hostapd /tmp/hostapd.conf

# Assign static IP to the AP interface
ip addr add "$AP_IP/24" dev "$IFACE"
ip link set "$IFACE" up

# Start dnsmasq (DHCP only – no DNS forwarding / no internet)
dnsmasq \
    --interface="$IFACE" \
    --bind-interfaces \
    --dhcp-range="$DHCP_RANGE" \
    --dhcp-option=3,"$AP_IP" \
    --dhcp-option=6,"$AP_IP" \
    --port=0 \
    --no-resolv \
    --pid-file=/tmp/dnsmasq-"$IFACE".pid \
    --log-facility=/tmp/dnsmasq-"$IFACE".log &

sleep 1

# Show status
echo
echo "Local-only hotspot started"
echo "  Interface : $IFACE"
echo "  SSID      : $SSID"
echo "  Password  : $PSK"
echo "  Gateway   : $AP_IP"
echo
iw dev "$IFACE" info 2>/dev/null | head -n 15
echo
ip addr show "$IFACE"
echo
echo "Clients will receive IPs in the range 192.168.43.10 – 192.168.43.50"
echo "No internet is shared (local network only)."

