#!/bin/bash

clear

printf "%b" "Sourcing colors...\n"
# Colors defined for text
source colors.sh

printf "%b" "Sourcing environment variables...\n"
# LFS build environment settings
source env.sh

printf "%b" "Sourcing functions...\n"
# Functions used during build
source functions.sh

# Version check script
#source versioncheck.sh

printf "%b" "\n\n\n${CYN}BUILDING ${YLW}${LFS_VER} $(uname -m)${CYN} at \
${IWHT}${LFS}${CYN} on ${IWHT}${LFS_DISK}${LFS_PART}${RST}\n\n"

printf "%b" "${IRED}*** ${IREDB}WARNING${RST} \
${IWHT}POTENTIAL DATA LOSS \
${IREDB}WARNING ${RST}${IRED}***${GRN}\n${RST}"

printf "%b" "${GRN}Automatic creation of partition(s), filesystem(s) \n\
and mounting will occur unless manually performed\n\
before continuing.${RST}\n\n\n"

# Continue or Abort
printf "%b" "${IGRN}CONTINUE ${IWHTB}(C)${RST} / ${IRED}ABORT ${IWHTB}(OTHER)${RST}\n"
if read -r -n 1 contabort; then
	if [ "$contabort" == "C" ] || [ "$contabort" == "c" ]; then
		printf "%b" "\b${IWHT}Build is ${IGRN}CONTINUING${RST}\n\n"
	else	
		printf "%b" "\b${IWHT}Build ${IRED}ABORTED!\n\n"
		exit 0
	fi
else
	printf "%b" "\b${IWHT}Selection ${IRED}FAILURE!${RST}\n\n"
	exit 1
fi

printf "%b" "${GRN}Creating LFS directory at ${YLW}${LFS}${GRN} if it does not exist...${RST}\n"
# Create LFS directory if it doesn't exist
[[ -d /mnt/lfs ]] || sudo mkdir "${LFS}"

# Setup partition, filesystem, format and mount if
# LFS filesystem is not previously mounted
if ! grep -q "${LFS}" /proc/mounts; then
	printf "%b" "${GRN}Preparing LFS partition and filesystem${RST}...\n"

    source setupdisk.sh "${LFS_DISK}" "${LFS_PART}"
	printf "%b" "  ${IGRN}Mounting ${IWHT}${LFS_DISK}${LFS_PART} ${IGRN}at \
${IWHT}${LFS}${RST}\n"
    sudo mount "${LFS_DISK}${LFS_PART}" "${LFS}"
	printf "%b" "  ${IGRN}Changing ownership of ${IWHT}${LFS}${IGRN} \
to user ${IWHT}${USER}${RST}\n"
    sudo chown "${USER}" "${LFS}"
fi

# Create limited directory layout
mkdir -p "${LFS}"/sources
mkdir -p "${LFS}"/tools
mkdir -p "${LFS}"/bin
mkdir -p "${LFS}"/etc
mkdir -p "${LFS}"/lib
mkdir -p "${LFS}"/sbin
mkdir -p "${LFS}"/usr
mkdir -p "${LFS}"/var

case $(uname -m) in
    x86_64) mkdir -p "${LFS}"/lib64 ;;
esac

# Will use normal user and not special lfs user
#sudo groupadd lfs
#sudo useradd -s /bin/bash -g lfs -m -k /dev/null lfs

#printf "%b" "${LFS_PWD}\n${LFS_PWD}\n" | sudo passwd lfs

# Copy scripts and csv files to LFS sources target
printf "%b" "${GRN}Copying required files to ${YLW}${LFS}/sources \
${GRN}target directory...${RST}\n"

cp -rf ./*.sh chapter* ./*.csv "${LFS}/sources"
cd "${LFS}/sources" || exit 1

# Download packages and patches
printf "%b" "${GRN}Downloading source packages and patches... \n"
source download.sh
retval=$?
if [ "$retval" -ne 0 ]; then
    exit 1
fi

# Chapter 5
printf "%b" "${IWHT}============ ${IGRN}Building ${IYLW}Chapter 5${IWHT} \
============${RST}\n"
for package in binutils gcc linux-api-headers glibc libstdc++; do
    source packageinstall.sh 5 $package
    retval=$?
    if [ "$retval" -ne 0 ]; then
        exit 1
    fi
done

# Chapter 6
printf "%b" "${IWHT}============ ${IGRN}Building ${IYLW}Chapter 6${IWHT} \
============${RST}\n"
for package in m4 ncurses bash coreutils diffutils file findutils gawk grep gzip make patch sed tar xz binutils gcc; do
    source packageinstall.sh 6 $package
    retval=$?
    if [ "$retval" -ne 0 ]; then
        exit 1
    fi
done

source chapter6/cleanup.sh
source chapter6/backup.sh

# Prepare chroot scripts to be executable for owner, group and others
chmod ugo+x preparechroot.sh
chmod ugo+x insidechroot.sh

printf "%b" "${CYN}PREPARING ${RED}CHROOT${CYN} ENVIRONMENT...${RST}\n"
sudo ./preparechroot.sh "${LFS}"

printf "%b" "${CYN}ENTERING ${RED}CHROOT${CYN} ENVIRONMENT...${RST}\n"
sleep 3

sudo chroot "${LFS}" /usr/bin/env \
	HOME=/root \
	TERM="${TERM}" \
	PS1='(lfs chroot) \u:\w\$ ' \
	PATH=/bin:/usr/bin:/sbin:/usr/sbin \
	NUMPROCS="${NUMPROCS}" \
	MAKEFLAGS="${MAKEFLAGS}" \
	TESTERUID="$(id -u)" \
	/bin/bash --login +h -c "/sources/insidechroot.sh 7"
retval=$?

printf "%b" "${CYN}EXITED ${RED}CHROOT${RST} ENVIRONMENT...${RST}\n"
# Cleanup and Backup if chapter 7 successfully finished
if [ "$retval" -eq 7 ]; then
	printf "%b" "${GRN}Unmounting Virtual Kernel Filesystems...\n"
	sudo umount "${LFS}"/dev/pts
	sudo umount "${LFS}"/dev/
	sudo umount "${LFS}"/run
	sudo umount "${LFS}"/sys
	sudo umount "${LFS}"/proc

	printf "%b" "${GRN}Cleaning and backing up before starting Chapter 8.\n"
	source "${LFS}"/sources/chapter7/cleanup.sh
	source "${LFS}"/sources/chapter7/backup.sh
else
	exit "$retval"
fi

# Mount virtual kernel filesystems after cleanup and backup
source mountvirtfs.sh

# Enter CHROOT for chapter 8, 9 and 10
sudo chroot "${LFS}" /usr/bin/env \
	HOME=/root \
	TERM="${TERM}" \
	PS1='(lfs chroot) \u:\w\$ ' \
	PATH=/bin:/usr/bin:/sbin:/usr/sbin \
	NUMPROCS="${NUMPROCS}" \
	MAKEFLAGS="${MAKEFLAGS}" \
	TESTERUID="$(id -u)" \
	/bin/bash --login +h -c "/sources/insidechroot.sh 8"

printf "%b" "${CYN}EXITED ${RED}CHROOT${RST} ENVIRONMENT...${RST}\n"