set -e

# get 'lazy'
if [ -d lazy ]; then
	cd lazy; git pull; cd ../
else
	git clone git://www.midipix.org/lazy
fi

# get musl
if [ -d musl ]; then
	cd musl; git pull; cd ../
else
	git clone git://www.musl-libc.org/musl
fi

# get libc-testsuite
if [ -d libc-testsuite ]; then
	cd libc-testsuite; git pull; cd ../
else
	git clone http://git.etalabs.net/cgit/libc-testsuite
fi

# copy the project-specific recipes to their respective directories
cp -t musl            lazy/templates/musl.lzy
cp -t libc-testsuite  lazy/templates/testsuite.lzy

# create a temporary prefix for musl-libc
mkdir -p musl-tmp 

# create a build directory for musl
mkdir -p build-musl

# create a build directory for libc-testsuite
mkdir -p build-testsuite

# enter the musl build directory
cd build-musl

# configure
../lazy/lazy           \
	-x config      \
	-t native      \
	-c gcc         \
	-n musl        \
	-p ../musl     \
	-f ../musl-tmp

# build & install
./lazy -x build -e install

# enter the testsuite build directory
cd ../build-testsuite

# configure (gcc: and see below)
musl_specs_file=`readlink -e ../musl-tmp/lib/musl-gcc.specs`

lz_cflags_cmdline="-specs=$musl_specs_file" \
../lazy/lazy                  \
	-x config             \
	-t native             \
	-c gcc                \
	-n testsuite          \
	-p ../libc-testsuite

# build & test
./lazy -x build -e test

# uncomment if you have clang installed
# clangs_cflags="-nostdinc -I../musl-tmp/include -B../musl-tmp/lib"

# configure (clang)
# ../lazy/lazy               \
#	-x config            \
#	-t native            \
#	-c clang             \
#	-n testsuite         \
#	-p ../libc-testsuite

# build & test
# ./lazy -x build -e test

