#!/bin/bash
# install-dhcp.sh — installs isc-dhcp-server + Webmin on a netinfra node.
# Does NOT start the DHCP service. Run on the target node itself.
set -e

echo "=== Installing isc-dhcp-server ==="
apt-get update -qq
apt-get install -y isc-dhcp-server

echo "=== Installing Webmin ==="
if ! dpkg -l | grep -q '^ii.*webmin'; then
  curl -fsSL https://raw.githubusercontent.com/webmin/webmin/master/webmin-setup-repo.sh -o /tmp/webmin-setup.sh
  sh /tmp/webmin-setup.sh -f
  rm -f /tmp/webmin-setup.sh
  apt-get install -y webmin
else
  echo "Webmin already installed"
fi

echo "=== Writing /etc/default/isc-dhcp-server ==="
cat > /etc/default/isc-dhcp-server <<'EOF'
# Defaults for isc-dhcp-server (sourced by /etc/init.d/isc-dhcp-server)
INTERFACESv4="ens18"
INTERFACESv6=""
EOF

echo "=== Stopping DHCP service (should not serve yet) ==="
systemctl stop isc-dhcp-server 2>/dev/null || true
systemctl disable isc-dhcp-server 2>/dev/null || true

echo "=== Done. DHCP installed but NOT started. ==="
