#!/bin/bash

. /lib/.config
. /lib/functions

if [ ! -e /lib/.escaped ]; then
	SWITCH=/m
	mkdir -p ${SWITCH}
	mount -t tmpfs -o size="100%" tmpfs ${SWITCH}
	cp -a /??* ${SWITCH} 2>/dev/null
	cd ${SWITCH}
	mount -t proc proc /proc
	echo "0" >/proc/sys/kernel/printk
	:>lib/.escaped
	d&&echo "[!] Reloading ${0}"
	mount --move /proc proc
	exec switch_root . ${0}
fi
mount -t devtmpfs devtmpfs /dev >/dev/null 2>&1
ln -sf /proc/self/fd /dev/fd
exec > >(tee -i /tmp/lif.log) 2>&1
d&&echo "'debug' bootloader variable is set!"&&set -x
mount -t sysfs sysfs /sys >/dev/null 2>&1
mkdir -p ${UNION} ${DATAMNT} ${SYSTEM}
isShellEnabled
ln -sf /proc/mounts /etc/mtab
find /lib/modules/$(uname -r) -name "*.ko" -type f | sed -r "s:^.*/|[.]ko\$::g" | xargs -n 1 modprobe >/dev/null 2>&1
sleep 3
mdev -s
debugShell
isZramEnabled
mdadm --assemble --scan >/dev/null 2>&1||true
sleep 0.5
debugShell
findData
debugShell
findModules
debugShell
persistentChanges ${SYSTEM} ${CHANGES_MNT}
mkdir -p ${CHANGES} ${WORK}
debugShell
mountOverlay >/dev/null 2>&1
debugShell
d&&echo "Starting ${DISTRO}"
d&&sleep 2
changeRoot "$UNION" || clear; echo "!!ERROR occured, you shouldn't be here.!!"; sleep 3
emergencyShell
