#!/bin/bash

clear

echo "Sourcing colors..."
# Colors defined for text
source colors.sh

echo "Sourcing environment variables..."
# LFS build environment settings
source env.sh

echo "Sourcing spinner..."
# Spinner function (returns an exit value)
source spinner.sh

# Version check script
#source versioncheck.sh

echo -e "\n\n\n${CYN}BUILDING ${YLW}${LFS_VER} $(uname -m)${CYN} at \
${PRP}${LFS}${CYN} on ${PRP}${LFS_DISK}${LFS_PART}${RST}\n\n\n"

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

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

echo -e "${GRN}Setting up partition on ${YLW}${LFS_DISK} ${GRN}\
with ${YLW}ext4${GRN} filesystem at ${YLW}${LFS_DISK}${LFS_ROOT} ${GRN}\
and mounting at ${YLW}${LFS}${RST}..."
# Setup partition, filesystem, format and mount if
# LFS filesystem is not previously mounted
if ! grep -q "${LFS}" /proc/mounts; then
    source setupdisk.sh "${LFS_DISK}" "${LFS_PART}"
    sudo mount "${LFS_DISK}${LFS_PART}" "${LFS}"
    sudo chown -v ${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

#echo -e "${LFS_PWD}\n${LFS_PWD}\n" | sudo passwd lfs

# Copy scripts and csv files to LFS sources target
cp -rf *.sh chapter* *.csv "${LFS}/sources"
cd "${LFS}/sources"

# Download packages and patches
source download.sh
retval=$?
if [ "$retval" -ne 0 ]; then
    exit 1
fi

# Chapter 5
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
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

chmod ugo+x preparechroot.sh
chmod ugo+x insidechroot.sh
echo -e "${CYN}PREPARING CHROOT ENVIRONMENT...${RST}"
sudo ./preparechroot.sh ${LFS}
echo -e "${CYN}ENTERING CHROOT ENVIRONMENT...${RST}"
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}" \
	/bin/bash --login +h -c "/sources/insidechroot.sh"