#!/usr/bin/env bash
# alfred-zfs-boot-mode — Explicit ZFS Root Encryption & Boot-Unlock Policy Selector
# Modes:
#   status       Audit ZFS encryptionroot, keyformat, keylocation & all /boot + ESP initrds
#   passphrase   Zero-Keyfile Local Console Passphrase (Workstation default; wraps alfred-zfs-harden console)
#   dropbear     Headless Remote SSH Unlock via dropbear-initramfs (wraps alfred-zfs-harden dropbear)
#   lab-auto     Explicit opt-in unattended lab policy documentation (never silently enables plaintext keyfiles)
set -euo pipefail

CMD="${1:-status}"
shift || true

show_help() {
  cat <<'EOF'
Alfred Linux — Explicit ZFS Root Boot-Unlock Policy Selector

Usage:
  alfred-zfs-boot-mode status
  alfred-zfs-boot-mode passphrase [--yes]
  alfred-zfs-boot-mode dropbear --pubkey-file /path/to/key.pub [--ip <spec>] [--port 2222] [--yes]
  alfred-zfs-boot-mode lab-auto

Modes:
  1. passphrase (RECOMMENDED DEFAULT — Workstations & Laptops):
     Zero cleartext keyfile on /boot or inside initramfs. Prompts for your AES-256-GCM
     passphrase at local console during every boot. Protects against cold disk seizure.

  2. dropbear (RECOMMENDED FOR HEADLESS SERVERS / DATACENTER):
     Zero cleartext keyfile on /boot or inside initramfs. Boots a minimal SSH daemon
     (dropbear-initramfs) before root mount so you unlock ZFS remotely via `zfsunlock`,
     verifying dropbear compiles into initramfs BEFORE removing any keyfile.

  3. lab-auto (EXPLICIT OPT-IN CONVENIENCE ONLY):
     Documents unattended boot tradeoffs. Automated boot with an unencrypted local keyfile
     provides ZERO protection against physical disk theft; never enabled silently by Alfred.

Run `alfred-zfs-harden --help` for full OpenZFS forensic audit & master-key rotation notes.
EOF
}

case "$CMD" in
  -h|--help|help)
    show_help
    exit 0
    ;;
  status)
    echo "Configured Policy: $(cat /etc/alfred/zfs-boot-mode 2>/dev/null || echo 'passphrase (default)')"
    if [ -x /usr/local/bin/alfred-zfs-harden ]; then
      exec /usr/local/bin/alfred-zfs-harden status
    else
      zfs get -H -o name,property,value encryption,encryptionroot,keyformat,keylocation rpool 2>/dev/null || echo "ZFS rpool not detected"
      ls -la /etc/zfs/initramfs-tools-load-key* 2>/dev/null || echo "No initramfs cleartext keyfiles present (PASS)"
    fi
    ;;
  passphrase)
    mkdir -p /etc/alfred
    echo "passphrase" > /etc/alfred/zfs-boot-mode
    exec /usr/local/bin/alfred-zfs-harden console "$@"
    ;;
  dropbear)
    mkdir -p /etc/alfred
    echo "dropbear" > /etc/alfred/zfs-boot-mode
    exec /usr/local/bin/alfred-zfs-harden dropbear "$@"
    ;;
  lab-auto)
    cat <<'EOF'
[alfred-zfs-boot-mode] Mode: lab-auto (Explicit Opt-In Unattended Convenience)
- Alfred Linux NEVER silently enables plaintext initramfs keyfiles on fresh installs.
- An unencrypted keyfile inside initramfs or /boot allows unattended reboots in a sealed
  lab or datacenter rack, but gives ZERO cryptographic protection against cold disk seizure
  or physical theft.
- For unattended remote servers with real physical security, use Dropbear SSH unlock:
    sudo alfred-zfs-boot-mode dropbear --pubkey-file ~/.ssh/authorized_keys
EOF
    exit 0
    ;;
  *)
    echo "Unknown mode: $CMD" >&2
    show_help >&2
    exit 2
    ;;
esac
