#!/bin/sh

# ----------------------
# test-toolchain.sh
# version 1.8.5.4
# ----------------------

cd `dirname "${0}"`
exit_code=0

echo "--------------------------------------------------"
gcc -v
echo "--------------------------------------------------"
echo ""

echo '++ 1. Testing if toolchain can build and run C programs, and that it produces uClibc binaries...'

cat >./helloworld.c << HERE
#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}
HERE

gcc ./helloworld.c -o ./helloworld || echo "GCC error!"
[ -f ./helloworld ] && chmod +x ./helloworld
rm -f ./helloworld.c

lines_of_uClibc=0
[ -f ./helloworld ] && lines_of_uClibc=`objdump -T ./helloworld | grep uClibc | wc -l`
echo "--------------------"
success_c_flag=1
if [ $lines_of_uClibc -gt 0 ]; then
  hello_output=`./helloworld 2>&1 || echo "ERROR"`
  tail_c_test=`echo "${hello_output}" | tail -1`
  if [ "${tail_c_test}" = "ERROR" ]; then
    success_c_flag=-1
    printf "\033[1m%s\033[0m\n" `echo "${hello_output}" | head -1 | tr 'a-z' 'A-Z' | sed 's/ /_/g'`
  else
    success_c_flag=0
    echo "${hello_output}"
  fi
fi
if [ $success_c_flag -eq 0 ]; then
  echo "--------------------"
  echo ">> TEST #1 PASSED!"
else
  exit_code=`expr $exit_code '+' 1`
  echo ">> TEST #1 FAILED!"
  echo "--------------------"
  if [ -f ./helloworld ] && [ $success_c_flag -ne -1 ]; then
    echo 'objdump -T ./helloworld'
    objdump -T ./helloworld | while read curr_line; do
      [ -z "${curr_line}" ] || printf "\t%s\n" "${curr_line}"
    done
  fi
fi
rm -f ./helloworld
echo ""

echo '++ 2. Testing if toolchain can build from C++ sources...'

echo '#include <iostream>' > ./hello.c++
echo '' >> ./hello.c++
echo 'int main(void)' >> ./hello.c++
echo '{' >> ./hello.c++
echo '    std::cout << "Hello, C++" << std::endl;' >> ./hello.c++
echo '    return 0;' >> ./hello.c++
echo '}' >> ./hello.c++

echo "--------------------"
cplusplus_success_flag=1
g++ ./hello.c++ -o ./helloc++ 1>/dev/null 2>&1 && cplusplus_success_flag=2
rm -f ./hello.c++

if [ $cplusplus_success_flag -eq 2 ]; then
  helloplus_output=`./helloc++ 2>&1 || echo "ERROR"`
  the_tail_plus=`echo "${helloplus_output}" | tail -1`
  if [ "${the_tail_plus}" = "ERROR" ]; then
    cplusplus_success_flag=-1
    printf "\033[1m%s\033[0m\n" `echo "${helloplus_output}" | head -1 | tr 'a-z' 'A-Z' | sed 's/ /_/g'`
  else
    cplusplus_success_flag=0
    echo "${helloplus_output}"
  fi
fi
if [ $cplusplus_success_flag -eq 0 ]; then
  echo "--------------------"
  echo ">> TEST #2 PASSED!"
else
  exit_code=`expr $exit_code '+' 2`
  echo ">> TEST #2 FAILED!"
  echo "--------------------"
fi
rm -f ./helloc++
echo ""

echo '++ 3. Testing linker (ld)...'

cat >_ld_test.c << HERE
#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}
HERE

# the -c option for gcc says not to run the linker right now
gcc -c _ld_test.c

_ld_static_linking="ok"
_ld_dynamic_linking="ok"
ld -Bstatic -o _ld_test-static -L`gcc -print-file-name=` /usr/lib/crt1.o /usr/lib/crti.o \
   _ld_test.o /usr/lib/crtn.o -lc -lgcc 1>_ld_test-static.ld_output 2>&1 || _ld_static_linking="error!"
ld -Bdynamic -o _ld_test-dynamic -L`gcc -print-file-name=` /usr/lib/crt1.o /usr/lib/crti.o \
   _ld_test.o /usr/lib/crtn.o -lc -lgcc 1>_ld_test-dynamic.ld_output 2>&1 || _ld_dynamic_linking="error!"

echo "--------------------"
echo "a) linking  statically:" "${_ld_static_linking}"
echo "b) linking dynamically:" "${_ld_dynamic_linking}"
echo "--------------------"

if [ "${_ld_static_linking}" = "ok" ] && [ "${_ld_dynamic_linking}" = "ok" ]; then
  echo ">> TEST #3 PASSED!"
else
  exit_code=`expr $exit_code '+' 4`
  echo ">> TEST #3 FAILED!"
  mv _ld_test-static.ld_output ../ld_test-static.ld_output
  mv _ld_test-dynamic.ld_output ../ld_test-dynamic.ld_output
fi
rm -f _ld_test*

exit $exit_code
