#!/bin/bash

source colors.sh

export LFS="$1"

if [ -f chrootprepared ]; then
	printf "%b" "${RED}CHROOT ${GRN}already prepared...\n${RST}"
	exit 0
fi

if [ -z "${LFS}" ]; then
	printf "%b" "${RED}Require first argument as path to LFS build!${RST}\n"
	exit 1
fi

if [ "${USER}" == "root" ]; then
	printf "%b" "${GRN}Changing ownership to root...${RST}\n"
	chown -R root:root "${LFS}"/bin
	chown -R root:root "${LFS}"/etc
	chown -R root:root "${LFS}"/lib
	chown -R root:root "${LFS}"/sbin
	chown -R root:root "${LFS}"/tools
	chown -R root:root "${LFS}"/usr
	chown -R root:root "${LFS}"/var

	case $(uname -m) in
		x86_64) chown -R root:root "${LFS}"/lib64 ;;
	esac

	printf "%b" "${GRN}Creating virtual kernel filesystem mountpoints...${RST}\n"
	mkdir -p "${LFS}"/dev
	mkdir -p "${LFS}"/proc
	mkdir -p "${LFS}"/run
	mkdir -p "${LFS}"/sys
	
	printf "%b" "${GRN}Creating initial device nodes...${RST}\n"
	mknod -m 600 "${LFS}"/dev/console c 5 1 >/dev/null 2>&1
	mknod -m 666 "${LFS}"/dev/null c 1 3 >/dev/null 2>&1
	
	source mountvirtfs.sh

	touch chrootprepared

else
	printf "%b" "${RED}preparechroot.sh must be ran as ${YLW}root${RED} user!${RST}\n"
fi