THREADS=${1:-5}
DIRECTORY=$(pwd)

cd CMake
./alib_cmake.py -w -m
cd ..

if [ ! -d debug ] && [ ! -L debug ]; then
  mkdir debug
fi

if [ -L debug ]; then
  LINK=$(readlink debug)
  if [ ! -d ${LINK} ]; then
    mkdir ${LINK}
  fi
  cd ${LINK}
  cmake ${DIRECTORY}
  cd ${DIRECTORY}/debug
else
  cd debug
  cmake ..
fi

CXX=clang++ make -j${THREADS} || exit 1

for test in $(ls */test-alib2*); do \
  if [ -f ${test} ]; then ./${test} || exit 1; fi \
done

if [ ! -d bin ]; then
  mkdir bin
fi

rm bin/*
cd bin
find .. -executable -type f | grep -v 'CMakeFiles' | while read line; do ln -s $line $(basename $line); done

cp ${DIRECTORY}/translateAddresses .
cp ${DIRECTORY}/xmlFormat .

cd ${DIRECTORY}

for test in $(ls tests.*.sh); do \
  ./${test} debug/bin ${THREADS}; \
done
