#!/bin/bash

file="${LFS_VER}-ch7-backup.tar.xz"
directories=(bin dev etc lib lib64 proc run sbin sources sys tools usr var)

if [ -f "${LFS}/sources/chapter7/backup" ]; then
	echo -e "${YLW}Chapter 7 ${GRN}Backup found. Remove manually to backup again.${RST}"
else

	echo -ne "${GRN}Backing up ${YLW}${LFS}${GRN} to ${YLW}${file}${RST}... "
	pushd "${LFS}" > /dev/null || exit 1
		sudo XZ_OPTS="-T${NUMPROCS} -e" tar cJpf "${HOME}"/"${file}" "${directories[@]}" & pid=$!
		spinner "$pid"
		retval=$?
		if [ "$retval" -ne 0 ]; then
			echo -e "${RED}Backup FAILED!${RST}"
		else
			echo -e "${GRN}OK${RST}"
		fi
	popd > /dev/null || exit 1

	touch "${LFS}/sources/chapter7/backup"
fi

unset file directories