#! /bin/bash

# URLs and versions
GLIBC_VERS='2.14'
GLIBC_URL="http://ftp.gnu.org/gnu/glibc/glibc-${GLIBC_VERS}.tar.gz"
GLIBC_TARBALL="src/glibc-${GLIBC_VERS}.tar.gz"
GLIBC_DIR="glibc-${GLIBC_VERS}"

GLIBC_PORTS_VERS='2.13'
GLIBC_PORTS_URL="http://ftp.gnu.org/gnu/glibc/glibc-ports-${GLIBC_PORTS_VERS}.tar.gz"
GLIBC_PORTS_TARBALL="src/glibc-ports-${GLIBC_PORTS_VERS}.tar.gz"
GLIBC_PORTS_DIR="$(pwd)/glibc-ports-${GLIBC_PORTS_VERS}"

# Main script
CCNAME="$1"
CCDIR="$2"
PREFIX="$3"
STAGE="$4"

# Clean
if [ "$1" = "distclean" ]; then
	rm -f "${GLIBC_TARBALL}"

	set -- clean
fi

if [ "$1" = "clean" ]; then
	rm -rf "${GLIBC_DIR}"
	rm -rf "${GLIBC_PORTS_DIR}"
	rm -rf glibc-*-*-*

	exit 0
fi

# Only build glibc for linux-gnu platforms
if ! echo "${CCNAME}" | grep -- '-linux-gnu' >/dev/null; then
	exit 0
fi

# Only build the first time around
if [ "${STAGE}" != "stage1" ]; then
	exit 0
fi

# Inform the user of what we are doing
echo ' * Building GNU C Library (glibc)'

# Download source
. 'scripts/common'

if [ ! -d "${GLIBC_DIR}" ]; then
	download "${GLIBC_URL}" "${GLIBC_TARBALL}" || exit 1

	gzip -dc "${GLIBC_TARBALL}" | tar -xf -
fi

if [ ! -d "${GLIBC_PORTS_DIR}" ]; then
	download "${GLIBC_PORTS_URL}" "${GLIBC_PORTS_TARBALL}" || exit 1

	gzip -dc "${GLIBC_PORTS_TARBALL}" | tar -xf -
fi

rm -rf "glibc-${CCNAME}"
mkdir "glibc-${CCNAME}"
cd "glibc-${CCNAME}" || exit 1

"../${GLIBC_DIR}/configure" --prefix="${PREFIX}" --host="${CCNAME}" --enable-add-ons="${GLIBC_PORTS_DIR}" || exit 1

${MAKE} || exit 1

${MAKE} install || exit 1

rm -rf "glibc-${CCNAME}"

exit 0
