#!/bin/sh
set -e
echo "Checking toolchain..."
for cmd in gcc ld objcopy grub-mkrescue qemu-system-x86_64; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Warning: $cmd not found."
fi
done
echo "Building project using Makefile..."
if [ ! -f Makefile ]; then
echo "Makefile not found in current directory."
exit 1
fi
make clean || true
if ! make; then
echo "Build failed."
exit 1
fi
if command -v grub-mkrescue >/dev/null 2>&1; then
echo "Creating ISO with grub-mkrescue..."
if [ ! -f iso/boot/grub/grub.cfg ]; then
echo "GRUB config not found, creating default grub.cfg..."
mkdir -p iso/boot/grub
cat > iso/boot/grub/grub.cfg <<'EOF'
set timeout=0
set default=0
menuentry "CrystalOS" {
multiboot2 /boot/kernel.bin
}
EOF
fi
grub-mkrescue -o tinyos.iso iso || { echo "grub-mkrescue failed"; exit 1; }
echo "ISO created: tinyos.iso"
else
echo "grub-mkrescue not available; skip ISO creation. kernel.bin produced."
fi
ARCHIVE="tinycrystalos-build.tar.gz"
echo "Packaging build artifacts into $ARCHIVE..."
tar -czf "$ARCHIVE" kernel.bin kernel/*.o modules/*.o modules/*.c kernel/*.c kernel/*.S kernel/linker.ld || true
echo "Packaged build artifacts."
echo "Done."
echo "To run in QEMU (if tinyos.iso was created):"
echo " qemu-system-x86_64 -cdrom tinyos.iso -m 512"