#!/bin/bash
# Copyright (c) 2025 fithwum
# All rights reserved
set -e

ROOTFS_DIR="${1:-/mnt/alpine-rootfs}"
TARBALL="/workspace/output/alpine-base.tar.gz"  # <-- store output in workspace for CI

echo "[INFO] Unmounting system directories (ignore errors)..."
for dir in dev proc sys; do
  sudo umount -lf "$ROOTFS_DIR/$dir" 2>/dev/null || true
done

echo "[INFO] Cleaning up chroot script..."
sudo rm -f "$ROOTFS_DIR/alpine-base_pt2.sh"

echo "[INFO] Rootfs size:"
du -sh "$ROOTFS_DIR"

echo "[INFO] Creating tarball: $TARBALL"
sudo tar -czf "$TARBALL" -C "$ROOTFS_DIR" .

echo "[INFO] Tarball ready for CI: $TARBALL"