
. ${CONFIG_DIR}/compile-config

Is_true () {
	case "$(echo ${1} | tr A-Z a-z)" in
		yes|true|on|y|1)
			return 0
			;;
	esac

	return 1
}

Echo_error () {
	echo "E: ${@}, exiting..." 1>&2
}

Echo_info () {
	if Is_true ${INFO}
	then
		echo "I: ${@}" 1>&2
	fi
}

Echo_debug () {
	if Is_true ${DEBUG}
	then
		echo "D: ${@}" 1>&2
	fi
}

Copy_file () {
	local FILE
	FILE="${1}"

	Echo_debug "Copying ${FILE} to chroot"

	if Is_true ${DEBUG}
	then
		cp -L -r --parents -v -- "${FILE}" "${RT}"
	else
		cp -L -r --parents -- "${FILE}" "${RT}"
	fi
}

Copy_libs_for_binary () {
	local BINARY
	BINARY="${1}"

	Echo_debug "Copying libraries for ${BINARY}"
	
	for LIB in $(ldd "${BINARY}" 2>/dev/null | sed -n 's/[[:blank:]][^\/]*\(\/.*\)[[:blank:]].*/\1/p')
	do
		Copy_file "${LIB}"
	done
}

Test_chroot () {
	if [ ! -d "${RT}" ] || [ ! -e "${RT}/t.cpp" ]
	then
		Echo_error "${RT} does not exist - run /usr/sbin/geordi-refresh-chroot"
		exit 1
	fi
}

Cond_refresh_chroot () {
	if Is_true ${ALWAYS_REFRESH_CHROOT}
	then
		Echo_info "Refreshing chroot"
		Echo_info "(To avoid refreshing every time, edit ALWAYS_REFRESH_CHROOT in"
		Echo_info "/etc/geordi/scripts-config)"
		/usr/sbin/geordi-refresh-chroot || exit $?
	fi
}
