#!ipxe
###############################################################################
# Centra Cloud PXE Boot Menu
#
# This iPXE script provides network boot options for bare-metal provisioning
# of Centra Cloud infrastructure nodes.
#
# Boot Profiles:
# - Control Plane: All 8 core services (flaredb, iam, plasmavmc, etc.)
# - Worker: Compute-focused services (k8shost, plasmavmc, basic services)
# - All-in-One: Testing/homelab deployment with all services on a single node
#
# Network Boot Flow:
# 1. DHCP assigns IP and points to TFTP server
# 2. TFTP serves iPXE bootloader (undionly.kpxe or ipxe.efi)
# 3. iPXE requests this script via HTTP
# 4. User selects profile or automatic selection via MAC mapping
# 5. iPXE loads NixOS kernel and initrd via HTTP
# 6. NixOS installer provisions the node based on profile
###############################################################################

# Set console output
console --picture --left 0 --right 0

# Configuration Variables
set boot-server 10.0.100.10
set boot-url http://${boot-server}/boot
set nixos-url ${boot-url}/nixos
set provisioning-server http://${boot-server}
set deployer-url http://${boot-server}:8080

# Detect network configuration
echo Network Configuration:
echo IP Address: ${ip}
echo Subnet Mask: ${netmask}
echo Gateway: ${gateway}
echo MAC Address: ${mac}
echo

# MAC-based Profile Selection
# This section automatically selects a profile based on MAC address
# Useful for automated provisioning without user interaction

isset ${profile} || set profile unknown

# Control-plane nodes (MAC address mapping)
iseq ${mac} 52:54:00:12:34:56 && set profile control-plane && set hostname control-plane-01 && goto boot ||
iseq ${mac} 52:54:00:12:34:59 && set profile control-plane && set hostname control-plane-02 && goto boot ||
iseq ${mac} 52:54:00:12:34:5a && set profile control-plane && set hostname control-plane-03 && goto boot ||

# Worker nodes (MAC address mapping)
iseq ${mac} 52:54:00:12:34:57 && set profile worker && set hostname worker-01 && goto boot ||
iseq ${mac} 52:54:00:12:34:5b && set profile worker && set hostname worker-02 && goto boot ||
iseq ${mac} 52:54:00:12:34:5c && set profile worker && set hostname worker-03 && goto boot ||

# All-in-one nodes (MAC address mapping)
iseq ${mac} 52:54:00:12:34:58 && set profile all-in-one && set hostname all-in-one-01 && goto boot ||

# No MAC match - show interactive menu
goto menu

###############################################################################
# Interactive Boot Menu
###############################################################################

:menu
clear menu
menu Centra Cloud - Bare-Metal Provisioning
item --gap -- ------------------------- Boot Profiles -------------------------
item control-plane    1. Control Plane Node (All Services)
item worker           2. Worker Node (Compute Services)
item all-in-one       3. All-in-One Node (Testing/Homelab)
item --gap -- ------------------------- Advanced Options -------------------------
item shell            iPXE Shell (for debugging)
item reboot           Reboot System
item exit             Exit to BIOS
item --gap -- ------------------------- Information -------------------------
item --gap -- MAC: ${mac}
item --gap -- IP: ${ip}
choose --timeout 30000 --default control-plane selected || goto cancel
goto ${selected}

:cancel
echo Boot cancelled, rebooting in 5 seconds...
sleep 5
reboot

###############################################################################
# Control Plane Profile
###############################################################################

:control-plane
set profile control-plane
echo
echo ========================================================================
echo Booting: Control Plane Node
echo ========================================================================
echo
echo This profile includes ALL Centra Cloud services:
echo  - FlareDB: Distributed database (PD, Store, TiKV-compatible)
echo  - IAM: Identity and Access Management
echo  - PlasmaVMC: Virtual Machine Controller
echo  - K8sHost: Kubernetes node agent
echo  - FlashDNS: High-performance DNS server
echo  - ChainFire: Firewall/networking service
echo  - Object Storage: S3-compatible storage
echo  - Monitoring: Prometheus, Grafana, AlertManager
echo
echo Target use case: Control plane nodes in production clusters
echo
sleep 2
goto boot

###############################################################################
# Worker Profile
###############################################################################

:worker
set profile worker
echo
echo ========================================================================
echo Booting: Worker Node
echo ========================================================================
echo
echo This profile includes COMPUTE-FOCUSED services:
echo  - K8sHost: Kubernetes node agent (primary workload runner)
echo  - PlasmaVMC: Virtual Machine Controller (VM workloads)
echo  - ChainFire: Firewall/networking (network policy enforcement)
echo  - FlashDNS: Local DNS caching
echo  - Basic monitoring agents
echo
echo Target use case: Worker nodes for running customer workloads
echo
sleep 2
goto boot

###############################################################################
# All-in-One Profile
###############################################################################

:all-in-one
set profile all-in-one
echo
echo ========================================================================
echo Booting: All-in-One Node
echo ========================================================================
echo
echo This profile includes ALL services on a SINGLE node:
echo  - Complete Centra Cloud stack
echo  - Suitable for testing, development, and homelab use
echo  - NOT recommended for production (no HA, resource intensive)
echo
echo Target use case: Development, testing, homelab deployments
echo
sleep 2
goto boot

###############################################################################
# Boot Logic - Load NixOS Kernel and Initrd
###############################################################################

:boot
# Set hostname if not already set
isset ${hostname} || set hostname centra-node-${mac:hexhyp}

echo
echo ========================================================================
echo Network Boot Configuration
echo ========================================================================
echo Profile:      ${profile}
echo Hostname:     ${hostname}
echo MAC Address:  ${mac}
echo IP Address:   ${ip}
echo Boot Server:  ${boot-server}
echo ========================================================================
echo

# Kernel parameters for NixOS netboot
# These parameters are passed to the NixOS installer
set kernel-params initrd=initrd ip=dhcp
set kernel-params ${kernel-params} centra.profile=${profile}
set kernel-params ${kernel-params} centra.hostname=${hostname}
set kernel-params ${kernel-params} centra.mac=${mac}
set kernel-params ${kernel-params} centra.provisioning-server=${provisioning-server}
set kernel-params ${kernel-params} plasmacloud.deployer_url=${deployer-url}
set kernel-params ${kernel-params} console=tty0 console=ttyS0,115200n8

# For debugging, enable these:
# set kernel-params ${kernel-params} boot.shell_on_fail
# set kernel-params ${kernel-params} systemd.log_level=debug

echo Loading NixOS kernel...
# NOTE: These paths will be populated by the S3 image builder (T032.S3)
# For now, they point to placeholder paths that need to be updated
kernel ${nixos-url}/bzImage ${kernel-params} || goto failed

echo Loading NixOS initrd...
initrd ${nixos-url}/initrd || goto failed

echo
echo Booting NixOS installer for ${profile} provisioning...
echo
boot || goto failed

###############################################################################
# Error Handling
###############################################################################

:failed
echo
echo ========================================================================
echo Boot Failed!
echo ========================================================================
echo
echo Failed to load kernel or initrd from ${nixos-url}
echo
echo Troubleshooting:
echo  1. Check that the HTTP server is running on ${boot-server}
echo  2. Verify that NixOS boot files exist at ${nixos-url}/
echo  3. Check network connectivity: ping ${boot-server}
echo  4. Review firewall rules (HTTP port 80/443 should be open)
echo
echo Dropping to iPXE shell for debugging...
echo Type 'menu' to return to the boot menu
echo
goto shell

###############################################################################
# iPXE Shell (for debugging)
###############################################################################

:shell
echo
echo Entering iPXE shell. Useful commands:
echo  - dhcp: Renew DHCP lease
echo  - ifstat: Show network interface status
echo  - route: Show routing table
echo  - ping <host>: Test connectivity
echo  - menu: Return to boot menu
echo  - kernel <url>: Load kernel manually
echo  - initrd <url>: Load initrd manually
echo  - boot: Boot loaded kernel
echo
shell

###############################################################################
# Reboot
###############################################################################

:reboot
echo Rebooting system...
reboot

###############################################################################
# Exit to BIOS
###############################################################################

:exit
echo Exiting iPXE and returning to BIOS boot menu...
exit
