#! /bin/bash

# URLs and versions
UCLIBC_VERS='0.9.32.1'
UCLIBC_URL="http://uclibc.org/downloads/uClibc-${UCLIBC_VERS}.tar.bz2"
UCLIBC_TARBALL="src/uClibc-${UCLIBC_VERS}.tar.bz2"
UCLIBC_DIR="uClibc-${UCLIBC_VERS}"

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

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

	set -- clean
fi

if [ "$1" = "clean" ]; then
	rm -rf "${UCLIBC_DIR}"

	exit 0
fi

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

# Skip building if we are already installed
if [ -f "${PREFIX}/lib/ld-uClibc.so.0" ]; 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 micro C library (uClibc)'

# Download source
. 'scripts/common'

# Ensure that we get a consistent build between the C compiler and the C library
case "${CCNAME}" in
	armel-*)
		extra_make_options="${extra_make_options} ARCH_LITTLE_ENDIAN=y ARCH_BIG_ENDIAN=n"
		;;
	armeb-*)
		extra_make_options="${extra_make_options} ARCH_LITTLE_ENDIAN=n ARCH_BIG_ENDIAN=y"
		;;
	mipsel-*)
		extra_make_options="${extra_make_options} ARCH_LITTLE_ENDIAN=y ARCH_BIG_ENDIAN=n"
		;;
esac

download "${UCLIBC_URL}" "${UCLIBC_TARBALL}" || exit 1

rm -rf "${UCLIBC_DIR}"
bzip2 -dc "${UCLIBC_TARBALL}" | tar -xf -

(
	cd "${UCLIBC_DIR}" || exit 1

	# Use default configuration
	${MAKE} ${BUILD_CC_MAKE_FLAGS_SINGLE} CROSS="${CCNAME}-" PREFIX="${PREFIX}" ARCH="${arch}" KERNEL_HEADERS="${PREFIX}/include" RUNTIME_PREFIX="/" DEVEL_PREFIX="/" ${extra_make_options} defconfig || exit 1

	# Update default configuration and rebuild
	sed '
s@^HAS_NO_THREADS=y@# HAS_NO_THREADS is not set@;
s@^# LINUXTHREADS_NEW.*$@LINUXTHREADS_NEW=y@;
s@^# UCLIBC_SUSV4_LEGACY.*$@UCLIBC_SUSV4_LEGACY=y@;
s@^# DO_C99_MATH.*$@DO_C99_MATH=y@;
s@^# DO_XSI_MATH.*$@DO_XSI_MATH=y@;
' .config > .config.new
	mv .config.new .config
	yes '' | ${MAKE} ${BUILD_CC_MAKE_FLAGS_SINGLE} CROSS="${CCNAME}-" PREFIX="${PREFIX}" ARCH="${arch}" KERNEL_HEADERS="${PREFIX}/include" RUNTIME_PREFIX="/" DEVEL_PREFIX="/" ${extra_make_options} oldconfig

	# Build
	${MAKE} ${BUILD_CC_MAKE_FLAGS} CROSS="${CCNAME}-" PREFIX="${PREFIX}" ARCH="${arch}" KERNEL_HEADERS="${PREFIX}/include" RUNTIME_PREFIX="/" DEVEL_PREFIX="/" ${extra_make_options} || exit 1

	# Install
	${MAKE} ${BUILD_CC_MAKE_FLAGS} CROSS="${CCNAME}-" PREFIX="${PREFIX}" ARCH="${arch}" KERNEL_HEADERS="${PREFIX}/include" RUNTIME_PREFIX="/" DEVEL_PREFIX="/" ${extra_make_options} install || exit 1
) || exit 1

# Perform fix-ups
if [ ! -f "${PREFIX}/include/utime.h" ]; then
	echo '#include <linux/utime.h>' > "${PREFIX}/include/utime.h"
fi

# Modify "libc.so" to contain the correct paths
for file in "${PREFIX}/lib"/lib*.so; do
	if ! grep '^GROUP' "${file}" >/dev/null 2>/dev/null; then
		continue
	fi

	sed 's|'"${PREFIX}"'/*|/|g' "${file}" > "${file}.new"
	cat "${file}.new" > "${file}"
	rm -f "${file}.new"
done

rm -rf "${UCLIBC_DIR}"

exit 0
