#!/bin/sh
# /etc/initramfs-tools/hooks/waouw-cryptroot
#
# initramfs-tools hook: includes WiFi drivers, networking tools,
# and the waouw-keyfetch keyscript into the initramfs image.
#
# Installed to: /etc/initramfs-tools/hooks/waouw-cryptroot
# Run by: update-initramfs -u
#
# Depends on cryptroot hook running first so LUKS infrastructure is present.

PREREQ="cryptroot"

prereqs() {
    echo "$PREREQ"
}

case "$1" in
    prereqs)
        prereqs
        exit 0
        ;;
esac

. /usr/share/initramfs-tools/hook-functions

# -------------------------------------------------------------------
# 1. WiFi kernel modules (Broadcom BCM43455 on Raspberry Pi 3B+/4/5)
# -------------------------------------------------------------------
manual_add_modules brcmfmac
manual_add_modules brcmutil
manual_add_modules cfg80211
manual_add_modules rfkill

# -------------------------------------------------------------------
# 2. WiFi firmware (BCM43455 SDIO — RPi 3B+/4/Zero 2W)
# -------------------------------------------------------------------
FIRMWARE_DIR="/lib/firmware/brcm"
if [ -d "$FIRMWARE_DIR" ]; then
    mkdir -p "${DESTDIR}${FIRMWARE_DIR}"
    for f in "${FIRMWARE_DIR}"/brcmfmac43455-sdio.*; do
        [ -e "$f" ] && cp -a "$f" "${DESTDIR}${FIRMWARE_DIR}/"
    done
    # Also grab the regulatory database if present
    for f in /lib/firmware/regulatory.db /lib/firmware/regulatory.db.p7s; do
        if [ -e "$f" ]; then
            mkdir -p "${DESTDIR}/lib/firmware"
            cp -a "$f" "${DESTDIR}/lib/firmware/"
        fi
    done
else
    echo "W: waouw-cryptroot: firmware directory ${FIRMWARE_DIR} not found" >&2
fi

# -------------------------------------------------------------------
# 3. Networking binaries
# -------------------------------------------------------------------
# wpa_supplicant + wpa_cli for WiFi authentication
copy_exec /sbin/wpa_supplicant /sbin
copy_exec /sbin/wpa_cli /sbin

# DHCP client — prefer udhcpc (busybox), fall back to dhclient
if [ -x /usr/bin/udhcpc ]; then
    copy_exec /usr/bin/udhcpc /usr/bin
elif [ -x /sbin/udhcpc ]; then
    copy_exec /sbin/udhcpc /sbin
elif [ -x /sbin/dhclient ]; then
    copy_exec /sbin/dhclient /sbin
else
    echo "W: waouw-cryptroot: no DHCP client (udhcpc or dhclient) found" >&2
fi

# curl for HTTPS key fetch
if [ -x /usr/bin/curl ]; then
    copy_exec /usr/bin/curl /usr/bin
else
    echo "E: waouw-cryptroot: curl not found — key fetch will fail" >&2
fi

# ip for network interface management (iproute2)
if [ -x /sbin/ip ]; then
    copy_exec /sbin/ip /sbin
elif [ -x /usr/sbin/ip ]; then
    copy_exec /usr/sbin/ip /usr/sbin
fi

# openssl for HMAC-SHA256 computation
if [ -x /usr/bin/openssl ]; then
    copy_exec /usr/bin/openssl /usr/bin
else
    echo "E: waouw-cryptroot: openssl not found — HMAC auth will fail" >&2
fi

# xxd for hex encoding (often part of vim-common)
if [ -x /usr/bin/xxd ]; then
    copy_exec /usr/bin/xxd /usr/bin
else
    echo "W: waouw-cryptroot: xxd not found — will use od fallback" >&2
fi

# -------------------------------------------------------------------
# 4. CA certificates for HTTPS (curl needs them)
# -------------------------------------------------------------------
CA_BUNDLE="/etc/ssl/certs/ca-certificates.crt"
if [ -f "$CA_BUNDLE" ]; then
    mkdir -p "${DESTDIR}/etc/ssl/certs"
    cp "$CA_BUNDLE" "${DESTDIR}/etc/ssl/certs/"
else
    echo "W: waouw-cryptroot: CA bundle not found at ${CA_BUNDLE}" >&2
fi

# -------------------------------------------------------------------
# 5. WiFi configuration (prepared during SD card setup)
# -------------------------------------------------------------------
WIFI_CONF="/boot/firmware/wifi-initramfs.conf"
if [ -f "$WIFI_CONF" ]; then
    mkdir -p "${DESTDIR}/boot/firmware"
    cp "$WIFI_CONF" "${DESTDIR}/boot/firmware/"
    # Restrict permissions — contains WiFi password
    chmod 600 "${DESTDIR}/boot/firmware/wifi-initramfs.conf"
else
    echo "I: waouw-cryptroot: no WiFi config at ${WIFI_CONF} (Ethernet-only mode)" >&2
fi

# Also copy the device-id file if present
DEVICE_ID="/boot/firmware/waouw-device-id"
if [ -f "$DEVICE_ID" ]; then
    mkdir -p "${DESTDIR}/boot/firmware"
    cp "$DEVICE_ID" "${DESTDIR}/boot/firmware/"
fi

# -------------------------------------------------------------------
# 6. The keyscript itself
# -------------------------------------------------------------------
KEYSCRIPT="/etc/waouw/waouw-keyfetch"
if [ -f "$KEYSCRIPT" ]; then
    copy_exec "$KEYSCRIPT" /etc/waouw
else
    echo "E: waouw-cryptroot: keyscript not found at ${KEYSCRIPT}" >&2
    echo "E: waouw-cryptroot: LUKS unlock will fail without it" >&2
fi

# -------------------------------------------------------------------
# 7. udhcpc default script (needed if using busybox udhcpc)
# -------------------------------------------------------------------
UDHCPC_SCRIPT="/etc/waouw/udhcpc-initramfs.script"
if [ -f "$UDHCPC_SCRIPT" ]; then
    copy_exec "$UDHCPC_SCRIPT" /etc/waouw
elif [ -f "/usr/share/udhcpc/default.script" ]; then
    copy_exec "/usr/share/udhcpc/default.script" /usr/share/udhcpc
fi

# -------------------------------------------------------------------
# 8. Encryption script (for first-boot encryption from initramfs)
# -------------------------------------------------------------------
ENCRYPT_SCRIPT="/etc/initramfs-tools/scripts/local-premount/waouw-encrypt"
if [ -f "$ENCRYPT_SCRIPT" ]; then
    mkdir -p "${DESTDIR}/scripts/local-premount"
    cp "$ENCRYPT_SCRIPT" "${DESTDIR}/scripts/local-premount/waouw-encrypt"
    chmod +x "${DESTDIR}/scripts/local-premount/waouw-encrypt"
fi

# cryptsetup (should already be included by cryptroot hook, but ensure)
if [ -x /sbin/cryptsetup ]; then
    copy_exec /sbin/cryptsetup /sbin
fi

# mkfs.ext4 for creating filesystem inside LUKS
if [ -x /sbin/mkfs.ext4 ]; then
    copy_exec /sbin/mkfs.ext4 /sbin
elif [ -x /usr/sbin/mkfs.ext4 ]; then
    copy_exec /usr/sbin/mkfs.ext4 /usr/sbin
fi

# -------------------------------------------------------------------
# 9. Boot partition files needed by encryption/keyfetch
# -------------------------------------------------------------------
for f in .waouw-lot-id .waouw-encrypt-pending waouw-admin-url.txt .waouw-luks-localkey waouw-device-id; do
    src="/boot/firmware/$f"
    [ -f "$src" ] || src="/boot/$f"
    if [ -f "$src" ]; then
        mkdir -p "${DESTDIR}/boot/firmware"
        cp "$src" "${DESTDIR}/boot/firmware/"
    fi
done

echo "I: waouw-cryptroot: hook completed successfully" >&2
exit 0
