#!/bin/sh
# Phase-1 bootstrap init for the Nucleic Linux base build (docs/LINUX_VM.md §base build).
#
# macOS has no Linux filesystem tooling, so the ext4 root disk is assembled *inside the guest*: this
# runs as PID 1 from a tiny busybox initramfs, formats the raw block device, unpacks the Ubuntu rootfs
# tarball handed in over virtiofs, bakes in the Nucleic agent + first-boot provisioning hooks, then
# powers off. The host then boots phase 2 (the installed rootfs) to provision the desktop + toolchain.
#
# Everything here is a single static busybox — keep it POSIX sh and busybox-applet only.
set -e
export PATH=/bin:/sbin:/usr/bin:/usr/sbin

log() { echo "[nucleic-bootstrap] $*"; }

# busybox applet symlinks + core pseudo-filesystems.
/bin/busybox --install -s /bin 2>/dev/null || true
mkdir -p /proc /sys /dev /mnt/root /mnt/prov
mount -t proc proc /proc 2>/dev/null || true
mount -t sysfs sysfs /sys 2>/dev/null || true
mount -t devtmpfs devtmpfs /dev 2>/dev/null || true

fail() { log "FATAL: $*"; log "dropping to a shell for diagnosis"; exec /bin/sh; }

# The provisioning payload (rootfs tarball, agent binary, scripts, units) rides in over virtiofs.
log "mounting provisioning share"
mount -t virtiofs nucleicprov /mnt/prov || fail "could not mount the nucleicprov virtiofs share"

log "formatting the root disk (/dev/vda)"
# busybox ships mkfs.ext2; the ext4 driver mounts ext2 read/write, which is fine for a disposable
# clone. (A journalled ext4 via a static mke2fs is a documented upgrade — see docs/LINUX_VM.md.)
mkfs.ext2 -F /dev/vda >/dev/null 2>&1 || fail "mkfs.ext2 /dev/vda failed"
mount -t ext2 /dev/vda /mnt/root || fail "could not mount the new root filesystem"

log "unpacking the root filesystem"
cd /mnt/root
if [ -f /mnt/prov/rootfs.tar.xz ]; then
    unxz < /mnt/prov/rootfs.tar.xz | tar -x || fail "unpacking rootfs.tar.xz failed"
elif [ -f /mnt/prov/rootfs.tar.gz ]; then
    tar -xzf /mnt/prov/rootfs.tar.gz || fail "unpacking rootfs.tar.gz failed"
else
    fail "no rootfs tarball found in the provisioning share"
fi

log "installing the Nucleic agent + provisioning hooks"
mkdir -p /mnt/root/usr/local/bin /mnt/root/usr/local/sbin \
         /mnt/root/etc/systemd/system/multi-user.target.wants \
         /mnt/root/etc/systemd/network /mnt/root/var/lib/nucleic
install -m 0755 /mnt/prov/nucleic-linux-agent      /mnt/root/usr/local/bin/nucleic-linux-agent
install -m 0644 /mnt/prov/nucleic-linux-agent.service /mnt/root/etc/systemd/system/nucleic-linux-agent.service
install -m 0755 /mnt/prov/provision-linux-guest.sh /mnt/root/usr/local/sbin/provision-linux-guest.sh
install -m 0644 /mnt/prov/nucleic-firstboot.service /mnt/root/etc/systemd/system/nucleic-firstboot.service
[ -f /mnt/prov/nucleic-provision.env ] && install -m 0644 /mnt/prov/nucleic-provision.env /mnt/root/etc/nucleic-provision.env || true

# Enable the agent (always) + first-boot provisioning (removes itself when done).
ln -sf ../nucleic-linux-agent.service /mnt/root/etc/systemd/system/multi-user.target.wants/nucleic-linux-agent.service
ln -sf ../nucleic-firstboot.service   /mnt/root/etc/systemd/system/multi-user.target.wants/nucleic-firstboot.service

# Root fstab (the ext4 driver mounts the ext2 image); the session workspace share is mounted lazily by
# the provisioner/agent, not here.
cat > /mnt/root/etc/fstab <<EOF
/dev/vda / ext4 defaults 0 1
EOF

# NAT DHCP so phase-2 apt has network (the cloud rootfs uses systemd-networkd).
cat > /mnt/root/etc/systemd/network/10-nucleic-nat.network <<EOF
[Match]
Name=en* eth*
[Network]
DHCP=yes
EOF
mkdir -p /mnt/root/etc/systemd/system/multi-user.target.wants
ln -sf /lib/systemd/system/systemd-networkd.service \
       /mnt/root/etc/systemd/system/multi-user.target.wants/systemd-networkd.service 2>/dev/null || true

log "syncing"
cd /
sync
umount /mnt/prov || true
umount /mnt/root || true
log "done — powering off"
poweroff -f
