#!/bin/bash

set -x -e

ANSI_CLEAR="\033[0m"
ANSI_RED="\033[1;31m"
ANSI_BOLD="\033[1m"

function warn() {
	echo -e "${ANSI_RED}error${ANSI_CLEAR}: ${ANSI_BOLD}$1${ANSI_CLEAR}"
}

function newest_file() {
  local latest file
  for file; do
    [[ $file && $file -nt $latest ]] || latest=$file
  done
}

function has_newer_files_than() {
  [[ "$(newest_file "$1"/*)" -nt "$(newest_file "$2" "$2"/*)" ]]
}

# check if the packages directory is not newer than the system-root
if has_newer_files_than "./sysroot/packages" "./system/system-root"; then
	warn "\`./sysroot/packages\` is newer than \`./sysroot/system-root\`, run \`xbstrap install --all\`"
	exit 1
fi

# sync the sysroot
echo "sysroot: syncing base-files"
cp -r base-files/. sysroot/system-root/

IMAGE_PATH=build/disk.img

# set $SUID_BINARY based on installed SUID binary
if command -v sudo; then
	SUID_BINARY=$(which sudo)
else
	echo "mkimage.sh: sudo not found, attempting to use opendoas"
	SUID_BINARY=$(which doas)
fi

# make the disk image
rm -rf $IMAGE_PATH
dd if=/dev/zero bs=1G count=0 seek=512 of=$IMAGE_PATH
parted -s $IMAGE_PATH mklabel gpt
parted -s $IMAGE_PATH mkpart primary 2048s 100%

# ensure loop kernel module is enabled
if ! lsmod | grep -q 'loop'; then
	echo 'mkimage.sh: `loop` kernel module not found, attempting to load'
	$SUID_BINARY modprobe loop
fi

$SUID_BINARY losetup -Pf --show $IMAGE_PATH > loopback_dev
$SUID_BINARY mkfs.ext2 `cat loopback_dev`p1 -I128
rm -rf disk_image/
mkdir disk_image
$SUID_BINARY mount `cat loopback_dev`p1 disk_image
$SUID_BINARY cp -r -v sysroot/system-root/. disk_image/
pushd disk_image
$SUID_BINARY mkdir -p dev
$SUID_BINARY mkdir -p home
$SUID_BINARY mkdir -p tmp
$SUID_BINARY mkdir -p proc
$SUID_BINARY mkdir -p var
$SUID_BINARY mkdir -p mnt
popd
sync
$SUID_BINARY umount disk_image/

$SUID_BINARY losetup -d `cat loopback_dev`
sync

rm -rf loopback_dev
rm -rf disk_image
