#!/bin/bash

# VMID 6000 Startup Network Fix Script
# This script can be added to container startup to automatically activate network

set -euo pipefail

VMID=6000
IP="192.168.11.113/24"
GATEWAY="192.168.11.1"
INTERFACE="eth0"

echo "=== VMID 6000 Network Activation ==="

# Check if interface is UP
if ! ip link show $INTERFACE | grep -q "state UP"; then
    echo "Bringing interface $INTERFACE UP..."
    ip link set $INTERFACE up
fi

# Check if IP is assigned
if ! ip addr show $INTERFACE | grep -q "$IP"; then
    echo "Assigning IP $IP to $INTERFACE..."
    ip addr add $IP dev $INTERFACE
fi

# Check if default route exists
if ! ip route | grep -q "default via $GATEWAY"; then
    echo "Adding default route via $GATEWAY..."
    ip route add default via $GATEWAY dev $INTERFACE
fi

echo "Network activation complete"
echo "Interface: $(ip link show $INTERFACE | grep -o 'state [A-Z]*')"
echo "IP: $(ip addr show $INTERFACE | grep -oP 'inet \K[0-9.]+/[0-9]+' || echo 'Not assigned')"
echo "Gateway: $(ip route | grep default | awk '{print $3}')"
