#!/bin/bash
# pack-harakiri-boot.sh — package a Harakiri boot.img for mona
#
# Prerequisites (on the build machine):
#   - Kernel built with CONFIG_HARAKIRI=y (built-in, not module)
#     Image at: <kdir>/arch/arm64/boot/Image
#   - Stock boot.img from the phone (for ramdisk + cmdline reference)
#   - mkbootimg (from Android build tools or: pip install mkbootimg)
#
# Usage:
#   ./pack-harakiri-boot.sh <kernel-dir> <stock-boot.img> <output-boot.img>
#
# This does NOT flash. It only builds the image. Flashing is a
# separate, deliberate step.
#
# WARNING: Do not flash this to a daily driver without:
#   1. Full backup (contacts, 2FA, messages, files)
#   2. Bootloader unlocked
#   3. A way to fastboot flash back to stock

set -e

KDIR="$1"
STOCK_BOOT="$2"
OUTPUT="$3"

if [ -z "$KDIR" ] || [ -z "$STOCK_BOOT" ] || [ -z "$OUTPUT" ]; then
    echo "Usage: $0 <kernel-dir> <stock-boot.img> <output-boot.img>"
    exit 1
fi

IMAGE="$KDIR/arch/arm64/boot/Image"
if [ ! -f "$IMAGE" ]; then
    echo "FAIL: $IMAGE not found. Build the kernel first."
    exit 1
fi

if [ ! -f "$STOCK_BOOT" ]; then
    echo "FAIL: $STOCK_BOOT not found. Pull it from the phone first:"
    echo "  adb pull /dev/block/by-name/boot stock-boot.img"
    exit 1
fi

# Check that Harakiri is built in (not a module)
if ! grep -q "CONFIG_HARAKIRI=y" "$KDIR/.config" 2>/dev/null; then
    echo "WARN: CONFIG_HARAKIRI=y not found in $KDIR/.config"
    echo "The kernel will boot but Harakiri won't be there."
    read -p "Continue anyway? [y/N] " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

WORK=$(mktemp -d)
echo "[*] work dir: $WORK"

# Unpack stock boot.img to steal the ramdisk and cmdline
echo "[*] unpacking stock boot.img..."
mkbootimg --unpack "$STOCK_BOOT" --output "$WORK" 2>/dev/null || {
    echo "FAIL: mkbootimg unpack failed. Install it first."
    rm -rf "$WORK"
    exit 1
}

# Find the ramdisk (name varies by mkbootimg version)
RAMDISK=$(find "$WORK" -name "*ramdisk*" -o -name "*initrd*" | head -1)
if [ -z "$RAMDISK" ]; then
    echo "FAIL: no ramdisk found in unpacked image"
    rm -rf "$WORK"
    exit 1
fi
echo "[*] ramdisk: $RAMDISK"

# Rebuild with our kernel + stock ramdisk
# NOTE: cmdline, pagesize, base, etc. are auto-detected from the
# unpacked image by mkbootimg v2+. If your version needs them
# explicitly, add: --pagesize 4096 --base 0x0 etc.
echo "[*] repacking with Harakiri kernel..."
mkbootimg \
    --kernel "$IMAGE" \
    --ramdisk "$RAMDISK" \
    --output "$OUTPUT" \
    2>/dev/null || {
    # Fallback: try with explicit header version
    echo "[*] retrying with header v2..."
    mkbootimg \
        --header_version 2 \
        --kernel "$IMAGE" \
        --ramdisk "$RAMDISK" \
        --output "$OUTPUT"
}

echo "[+] wrote $OUTPUT ($(stat -c%s "$OUTPUT") bytes)"
echo "[+] kernel: $(stat -c%s "$IMAGE") bytes"
echo ""
echo "To flash (UNLOCKED BOOTLOADER ONLY):"
echo "  fastboot flash boot $OUTPUT"
echo "  fastboot reboot"
echo ""
echo "To go back to stock:"
echo "  fastboot flash boot $STOCK_BOOT"

rm -rf "$WORK"
