#!/bin/ksh93
# Build script for hammerhead-kernel
# Packages kernel modules, boot infrastructure, and CPU microcode from proto area
#
# Requires PROTO_AREA to be set to the hammerhead proto path, e.g.:
#   export PROTO_AREA=/usr/src/hammerhead-build/root_amd64
#
# Optional: ROOTFS_DIR for rootfs overlay (boot archive filelist, etc.)
#   export ROOTFS_DIR=/usr/src/hammerhead/rootfs

set -e

PROTO_AREA="${PROTO_AREA:?PROTO_AREA must be set to hammerhead proto path}"

# Validate required directories
for dir in kernel boot; do
    if [ ! -d "$PROTO_AREA/$dir" ]; then
        printf "ERROR: %s/%s does not exist\n" "$PROTO_AREA" "$dir"
        printf "Has the hammerhead build completed successfully?\n"
        exit 1
    fi
done

# Validate critical files
for f in kernel/genunix kernel/unix boot/loader64.efi; do
    if [ ! -f "$PROTO_AREA/$f" ]; then
        printf "ERROR: %s not found in proto area\n" "$f"
        exit 1
    fi
done

# --- Kernel modules (includes driver .conf files) ---
printf "Packaging kernel modules...\n"
mkdir -p "$PKGDIR/kernel"
(cd "$PROTO_AREA/kernel" && find . | cpio -pdm "$PKGDIR/kernel" 2>/dev/null)

# Set ownership: kernel modules are root:sys
chown -R root:sys "$PKGDIR/kernel"

# --- Boot infrastructure (loader, fonts, splash, forth scripts) ---
printf "Packaging boot files...\n"
mkdir -p "$PKGDIR/boot"
(cd "$PROTO_AREA/boot" && find . | cpio -pdm "$PKGDIR/boot" 2>/dev/null)

# Apply rootfs overlay for boot files (filelist.ramdisk, etc.)
if [ -n "$ROOTFS_DIR" ] && [ -d "$ROOTFS_DIR/boot" ]; then
    printf "Applying rootfs overlay for boot...\n"
    (cd "$ROOTFS_DIR/boot" && find . | cpio -pdm "$PKGDIR/boot" 2>/dev/null)
fi

chown -R root:sys "$PKGDIR/boot"

# --- CPU microcode ---
if [ -d "$PROTO_AREA/platform/amd64/ucode" ]; then
    printf "Packaging CPU microcode...\n"
    mkdir -p "$PKGDIR/platform/amd64/ucode"
    (cd "$PROTO_AREA/platform/amd64/ucode" && find . | cpio -pdm "$PKGDIR/platform/amd64/ucode" 2>/dev/null)
    chown -R root:sys "$PKGDIR/platform/amd64/ucode"
fi

# --- Post-install script ---
mkdir -p "$PKGDIR/.SCRIPTS"
cat > "$PKGDIR/.SCRIPTS/post-install.sh" << 'SCRIPT'
#!/bin/ksh93
# Update boot archive to include new kernel modules
# Only run if we're on a live system (not during initial install to altroot)
if [ -f /kernel/genunix ]; then
    # Generate boot archive using cpio method (not bootadm)
    if [ -f /boot/solaris/filelist.ramdisk ]; then
        printf "Regenerating boot archive...\n"
        tmpfilelist=/tmp/boot_archive_files.$$
        tmparchive=/tmp/boot_archive.$$
        (cd / && while IFS= read -r line; do
            [[ -z "$line" || "$line" = \#* ]] && continue
            [[ -e "$line" ]] && printf "%s\n" "$line"
        done < /boot/solaris/filelist.ramdisk) > "$tmpfilelist"
        (cd / && cpio -o -H odc < "$tmpfilelist" 2>/dev/null) > "$tmparchive"
        digest -a sha1 "$tmparchive" > /boot/boot_archive.hash
        gzip -9 -c "$tmparchive" > /boot/boot_archive
        rm -f "$tmpfilelist" "$tmparchive"
        printf "Boot archive updated.\n"
    fi
fi
printf "Kernel updated. A reboot is required to load the new kernel.\n"
SCRIPT
chmod 755 "$PKGDIR/.SCRIPTS/post-install.sh"

# --- Summary ---
MODULES=$(find "$PKGDIR/kernel" -type f | wc -l)
BOOT_FILES=$(find "$PKGDIR/boot" -type f | wc -l)
SIZE=$(du -sh "$PKGDIR" | cut -f1)
printf "Packaged %d kernel files, %d boot files (%s total)\n" "$MODULES" "$BOOT_FILES" "$SIZE"
