#!/bin/bash

set -e

. inc/releng_getopts.inc

# xorriso binary is not needed for that particular test

SAMPLE_CODE_DIR=codesamples
CC=g++

# check data dir
if [ -d "${GEN_DATA_DIR}" ]; then
 printf "\n${SELF}: directory %s exists!" ${GEN_DATA_DIR}
 printf "\n${SELF}: use '${SELF} -c1' to remove.\n"
 exit 5
else
 mkdir "${GEN_DATA_DIR}"
fi

# check compiler
if ! which "${CC}" >/dev/null 2>&1; then
    printf "\n${SELF}: Not found: ${CC}. Install ${CC}.\n"
    cleanup
    exit 6
fi

# process sample code tests
# if test -f "${SAMPLE_CODE_DIR}"/*.cpp; then
 for SMPL in `ls "${SAMPLE_CODE_DIR}"/*.cpp`; do
   CMD_CPL="${CC} -I../ -L ../libisoburn/.libs/ -lisoburn -o ${SMPL}.obj ${SMPL}"
   printf "${SELF}: ${CMD_CPL}\n"
   set +e
   ${CMD_CPL}
   RET_CPL="$?"
   if [ ${RET_CPL} == 0 -a -f ${SMPL}.obj ]; then
      mv ${SMPL}.obj ${GEN_DATA_DIR}
   else
      printf "\n${SELF}: FAIL Compilation of ${SMPL}\n"
      cleanup
      exit 7
   fi
   BASE=$(basename ${SMPL}.obj)
   printf "${SELF}: Running LD_LIBRARY_PATH=../libisoburn/.libs/:${LD_LIBRARY_PATH} ${GEN_DATA_DIR}/${BASE}"
   LD_LIBRARY_PATH=../libisoburn/.libs/:${LD_LIBRARY_PATH} ${GEN_DATA_DIR}/${BASE}
   RET_SMPL="$?"
   case ${RET_SMPL} in
    0)
     printf "...ok\n"
    ;;
    *)
     printf "exit code: ${RET_SMPL}\n"
     cleanup
     exit 8
    ;;
   esac
   set -e
 done
#else
# printf "\n${SELF}: No C++ code samples found in ${SAMPLE_CODE_DIR}\n"
#fi

# clean
cleanup

exit 0