# generic functions (used for both build && iso creation)

kernelconfig () {
	if [[ $(zgrep 'CONFIG_OVERLAYFS_FS=' /proc/config.gz) && $(zgrep "CONFIG_SQUASHFS=" /proc/config.gz) &&  $(zgrep "CONFIG_BLK_DEV_LOOP=" /proc/config.gz) ]] ; then
		# if you use 3.18 LTS or newer kernels, change CONFIG_OVERLAYFS_FS to CONFIG_OVERLAY_FS
		echo ""
		echo "Kernel configuration seems OK, moving on"
		echo ""
		sleep 1
	else
		echo ""
		echo "Vasile needs OVERLAYFS && SQUASHFS && LOOP DEVICES to work"
		echo "Please rebuild the kernel with those activated to use it"
		echo ""
		exit 1
	fi
}

checkroot () {
	if [[ "$(whoami)" != root ]] ; then
		echo ""
		echo "You're not root?...No cookies for you, go away !!!"
		echo ""
		exit 1
	fi
}

chrootchecksum () {
	while : true ; do
		if [[ -f "$chrootx64" && -f "$chrootx64md5" ]] ; then
			echo "good, squashed chroot && checksum file found ... verifying integrity"
			echo ""
			if [[ "$(md5sum -c "$chrootx64md5")" ]] ; then
				echo "good, squashed chroot checksum passed ... starting engines"
				echo ""
				sleep 1
				break
			else
				echo "ooops, squashed chroot checksum failed ... aborting"
				exit 1
			fi
		else
			echo "ooops, squashed chroot or checksum file not found ... aborting"
			exit 1
		fi
	done
}

# build functions

chrootstop () {
	umount -l "$overlaydir"/proc > /dev/null 2>&1
	umount -l "$overlaydir"/sys > /dev/null 2>&1
	umount -l "$overlaydir"/dev/pts > /dev/null 2>&1
	umount -l "$overlaydir"/dev/shm > /dev/null 2>&1
	umount -l "$overlaydir"/dev > /dev/null 2>&1
	umount -l "$overlaydir"/tmp > /dev/null 2>&1
	umount -l "$overlaydir"/usr/portage/packages > /dev/null 2>&1
	umount -l "$overlaydir"/usr/portage/distfiles > /dev/null 2>&1
	umount -l "$overlaydir" > /dev/null 2>&1
	umount -l "$rodir" > /dev/null 2>&1
}

chrootstart () {
	mount -t squashfs "$chrootx64" "$rodir"
	if [[ "$(uname -r | cut -d . -f1,2)" = "3.14" ]] ; then
		mount -t overlayfs -o lowerdir="$rodir",upperdir="$rwdir" overlayfs "$overlaydir"
	else # kernel version equal or above 3.18
		mount -t overlay -o lowerdir="$rodir",upperdir="$rwdir",workdir="$workdir" overlay "$overlaydir"
	fi
	mount -o bind packages "$overlaydir"/usr/portage/packages
	mount -o bind distfiles "$overlaydir"/usr/portage/distfiles
	mount -t proc proc "$overlaydir"/proc
	mount -t sysfs sysfs "$overlaydir"/sys
	mount -t devtmpfs -o relatime,size=3055348k,nr_inodes=763837,mode=755 none "$overlaydir"/dev
	mount -t devpts -o nosuid,noexec,relatime,gid=5,mode=620 none "$overlaydir"/dev/pts
	mount -t tmpfs -o nosuid,nodev none "$overlaydir"/dev/shm
	mount -t tmpfs -o nosuid,nodev,noexec none  "$overlaydir"/tmp
}

chrootdevtree () {
	echo ""
	echo "injecting full portage tree into build environment"
	echo ""
	sleep 1
	chroot "$overlaydir" su - "$chrootuser" -c "$chrootdevtree"
}

chrootoverlays () {
	echo "injecting "$releasename" "$releasetarget" overlay into build environment"
	sleep 1
	chroot "$overlaydir" su - "$chrootuser" -c "$chrootmainoverlay"
}

chrootbuildsystem () {
	echo "injecting "$releasename" buildsystem && setting up portage"
	echo ""
	sleep 1
	chroot "$overlaydir" su - "$chrootuser" -c "$chrootbuildgit"
}

chrootbuild () {
	echo ""
	echo "Environment is up && running ... building targets"
	sleep 1
	chroot "$overlaydir" su - "$chrootuser" -c "$chrootbuildtarget"
}

chrootx64 () {
	echo -e ""
	echo -e "#################################################################"
	echo -e "#       ENTERING CHROOT ENV FOR YOU TO PUSH BUILT PACKAGES      #"
	echo -e "#               OR TO FIX EVENTUAL BUILD ERRORS                 #"
	echo -e "#################################################################"
	echo -e "#            !!! WARNING !!! WARNING !!! WARNING !!!            #"
	echo -e "#################################################################"
	echo -e "#       NEXT RUN OF THIS SCRIPT WILL DESTROY ALL YOUR WORK      #"
	echo -e "#   DO NOT EXIT CHROOT UNTIL ALL PACKAGES ARE PUSHED TO REPOS   #"
	echo -e "#  OR, IN CASE OF BUILD FAILURES, UNTIL ALL FIXES ARE COMMITED  #"
	echo -e "#################################################################"
	echo -e "#            !!! WARNING !!! WARNING !!! WARNING !!!            #"
	echo -e "#################################################################"
	echo -e ""
	echo -e ""
	chroot "$overlaydir" su - "$chrootuser"
}

# iso functions

isostart () {
	mount -o bind packages "$isosyncdir"/usr/portage/packages
	mount -t proc proc "$isosyncdir"/proc
	mount -t sysfs sysfs "$isosyncdir"/sys
	mount -t devtmpfs -o relatime,size=3055348k,nr_inodes=763837,mode=755 none "$isosyncdir"/dev
	mount -t devpts -o nosuid,noexec,relatime,gid=5,mode=620 none "$isosyncdir"/dev/pts
	mount -t tmpfs -o nosuid,nodev none "$isosyncdir"/dev/shm
	mount -t tmpfs -o nosuid,nodev,noexec none  "$isosyncdir"/tmp
}

isostop () {
	umount -l "$isosyncdir"/proc > /dev/null 2>&1
	umount -l "$isosyncdir"/sys > /dev/null 2>&1
	umount -l "$isosyncdir"/dev/pts > /dev/null 2>&1
	umount -l "$isosyncdir"/dev/shm > /dev/null 2>&1
	umount -l "$isosyncdir"/dev > /dev/null 2>&1
	umount -l "$isosyncdir"/tmp > /dev/null 2>&1
	umount -l "$isosyncdir"/usr/portage/packages > /dev/null 2>&1
}

isousertree () {
	echo ""
	echo "injecting minimal portage tree into environment (user mode)"
	echo ""
	sleep 1
	chroot "$isosyncdir" su - "$isouser" -c "$isousertree"
}

isooverlays () {
	echo "injecting "$releasename" "$releasetarget" overlay into environment"
	sleep 1
	chroot "$isosyncdir" su - "$isouser" -c "$isomainoverlay"
}

isobuildsystem () {
	echo "injecting "$releasename" buildsystem && setting up portage"
	echo ""
	sleep 1
	chroot "$isosyncdir" su - "$isouser" -c "$isobuildgit"
}

isoramfs () {
	chroot "$isosyncdir" su - "$isouser" -c "$isodracutcmd"
}

systemramfs () {
	chroot "$isosyncdir" su - "$isouser" -c "$dracutcmd"
}

isoefiimg () {
	chroot "$isosyncdir" su - "$isouser" -c "$isoeficmd"
}

isochroot () {
	echo -e ""
	echo -e "DROPPING YOU TO A ROOT SHELL INTO ISO ENVIRONMENT"
	echo -e "INSTALL APPS & TARGETS & DES YOU WANT INSIDE ISO"
	echo -e "EXIT WHEN YOU'RE DONE AND I'LL CREATE THE IMAGE"
	echo -e ""
	chroot "$isosyncdir" su - "$isouser"
}