#!/usr/bin/env bash

# Vasile needs root privileges and a proper kernel to run

kernelconfig
checkroot

chrootpreparex64 () {
	# check our environment for sanity
	# if safe, trigger start && break the loop to build packages
	# else trigger stop && cleanup && check again
	while : true ; do
		if [[ ! -d "$rodir" && ! -d "$rwdir" && ! -d "$workdir" && ! -d "$overlaydir" ]] ; then
			for i in "$rodir" "$rwdir" "$workdir" "$overlaydir" ; do
				mkdir "$i"
			done
			chrootstartx64
			break
		elif [[ -d "$rodir" && -d "$rwdir" && -d "$workdir" && -d "$overlaydir" ]] ; then
			chrootstop
			for i in "$rodir" "$rwdir" "$workdir" "$overlaydir" ; do
				rm -rf "$i"
			done
			continue
		fi
	done
}

main () {
	chrootchecksumx64
	chrootpreparex64
	chrootoverlaysx64
	chrootbuildsystemx64
	chrootbuildx64
	chrootx64
	chrootstop
}

main
exit 0