#!/bin/bash
# Removes the bad static-IP NetworkManager connection from benhost's
# internal disk so enp2s0 falls back to DHCP on next normal boot.
# Run from the live USB session. Safe: only deletes the one bad file.

echo ">>> Looking for benhost's system disk..."

FOUND=0
for p in /dev/nvme0n1p2 /dev/nvme0n1p3 /dev/nvme0n1p5 /dev/nvme0n1p1 /dev/nvme0n1p4; do
    sudo mount "$p" /mnt 2>/dev/null || continue
    if [ -d "/mnt/etc/NetworkManager/system-connections" ]; then
        echo ">>> Found benhost root on $p"
        sudo rm -f "/mnt/etc/NetworkManager/system-connections/Wired connection 1.nmconnection"
        # belt and braces: remove any other connection pinned to the bad static IP
        sudo grep -rl "192.168.1.1" /mnt/etc/NetworkManager/system-connections/ 2>/dev/null | while read f; do
            echo ">>> Removing extra bad connection: $f"
            sudo rm -f "$f"
        done
        sync
        sudo umount /mnt
        echo ""
        echo "============================================"
        echo ">>> DONE. Static IP config removed from $p"
        echo ">>> Now: shut down, REMOVE the USB stick,"
        echo ">>> and start benhost normally."
        echo "============================================"
        FOUND=1
        break
    fi
    sudo umount /mnt 2>/dev/null
done

if [ "$FOUND" = "0" ]; then
    echo ""
    echo "!!! Could not find benhost's NetworkManager config on any"
    echo "!!! partition. The disk may be LVM or encrypted."
    echo "!!! Photograph this whole screen and send it to Jason."
fi
