#!/bin/bash
# Build ISO script for badlinux
# Bad Apple ASCII animation player for kernel panic
# Plays Bad Apple animation using console/framebuffer
# Copyright (C) 2026  Badlinux authors
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, see
# <https://www.gnu.org/licenses/>.
*/
set -e

WORKDIR="/tmp/bad-apple-iso"
KERNEL="/path2kernel"
OUTPUT_ISO="/path2iso"

echo "Bad Apple Linux ISO Builder"
echo "Cleaning previous build..."
rm -rf "$WORKDIR"
mkdir -p "$WORKDIR"/{isofiles/boot/grub,initramfs/{bin,sbin,etc,proc,sys,dev}}
echo "Creating initramfs with BusyBox shell..."
cd "$WORKDIR/initramfs"

# Cp busybox
if [ -f /bin/busybox-static ]; then
    cp /bin/busybox-static bin/busybox
elif [ -f /usr/bin/busybox-static ]; then
    cp /usr/bin/busybox-static bin/busybox
else
    cp "$(which busybox)" bin/busybox
fi

chmod +x bin/busybox
for cmd in sh ash mount umount echo cat ls ps kill insmod lsmod rmmod mkdir ln; do
    ln -sf /bin/busybox bin/$cmd
done

# init script
cat > init << 'EOFFF'
#!/bin/busybox sh
/bin/busybox echo "Mounting filesystems..."
/bin/busybox mount -t proc none /proc
/bin/busybox mount -t sysfs none /sys
/bin/busybox mount -t devtmpfs none /dev 2>/dev/null || true
/bin/busybox echo 1 > /proc/sys/kernel/sysrq
/bin/busybox clear
/bin/busybox echo "========================================"
/bin/busybox echo "            Bad Apple Kernel            "
/bin/busybox echo "========================================"
/bin/busybox echo ""
/bin/busybox echo "You now have a shell. To trigger kernel"
/bin/busybox echo "panic and play Bad Apple, run:"
/bin/busybox echo ""
/bin/busybox echo "    echo c > /proc/sysrq-trigger"
/bin/busybox echo ""
/bin/busybox echo "========================================"
/bin/busybox echo ""

# Export PATH
export PATH=/bin:/sbin

# Start shell
exec /bin/busybox sh
EOFFF

chmod +x init

echo "Packing initramfs..."
find . | cpio -o -H newc 2>/dev/null | gzip > "$WORKDIR/isofiles/boot/initramfs.gz"
cd "$WORKDIR"
echo "Copying kernel..."
cp "$KERNEL" "$WORKDIR/isofiles/boot/vmlinuz"

echo "Creating GRUB configuration..."
cat > "$WORKDIR/isofiles/boot/grub/grub.cfg" << 'EOF'
set timeout=5
set default=0

menuentry "Bad Apple Linux" {
    linux /boot/vmlinuz console=tty0 panic=0 loglevel=4 nomodeset
    initrd /boot/initramfs.gz
}
menuentry "Bad Apple Linux Auto Panic" {
    linux /boot/vmlinuz console=tty0 panic=0 loglevel=7 nomodeset
}
EOF
echo "Building bootable ISO..."
grub-mkrescue -o "$OUTPUT_ISO" "$WORKDIR/isofiles" 2>&1 | grep -v "warning:"
# Cleanup
echo "Cleaning up temporary files..."
rm -rf "$WORKDIR"
echo "ISO created."
echo "ISO location: $OUTPUT_ISO"
echo "Size: $(du -h "$OUTPUT_ISO" | cut -f1)"
