#! /usr/bin/env bash

LAST_DIR="$PWD"
cd "$(dirname "$0")/.." || exit 1

STD_FLAG='gnu++20'

COMPILER_CXX="$( \
  command -v c++ \
  || command -v clang++ \
  || command -v g++ \
  || command -v circle \
  || command -v cl \
  || command -v cl.exe)"

if $COMPILER_CXX -v 2>&1 | grep -q 'clang'
then
  if test -d '/usr/local/include/c++/v1'
  then
    SYS_INCLUDE_DIR='/usr/local/include/c++/v1'
  elif test -d '/usr/include/c++/v1'
  then
    SYS_INCLUDE_DIR='/usr/include/c++/v1'
  else
    echo 'Cannot find system include directory.'
    echo "Tried: '/usr/local/include/c++/v1', '/usr/include/c++/v1'"
    cd "$LAST_DIR" || exit 1
    exit 1
  fi
elif $COMPILER_CXX -v 2>&1 | grep -q 'gcc version'
then
  if test -d '/usr/local/include/c++/11'
  then
    SYS_INCLUDE_DIR='/usr/local/include/c++/11'
  elif test -d '/usr/include/c++/11'
  then
    SYS_INCLUDE_DIR='/usr/include/c++/11'
  else
    echo 'Cannot find system include directory.'
    echo "Tried: '/usr/local/include/c++/11', '/usr/include/c++/11'"
    cd "$LAST_DIR" || exit 1
    exit 1
  fi
elif $COMPILER_CXX -v 2>&1 | grep -q 'Microsoft'
then
  CANDIDATE="$(\
    find 'C://Program Files/Microsoft Visual Studio' -name include -type d \
      | grep MSVC \
      | grep include \
      | tail -n 1)"
  if test -d "$CANDIDATE"
  then
    SYS_INCLUDE_DIR="$CANDIDATE"
  else
    echo 'Cannot find system include directory.'
    echo "Tried: $CANDIDATE"
    cd "$LAST_DIR" || exit 1
    exit 1
  fi
else
  echo 'Cannot find system include directory.'
  cd "$LAST_DIR" || exit 1
  exit 1
fi

echo "[
{
  \"directory\": \"${PWD}/build/out/this/test/nosan\",
  \"command\": \"${COMPILER_CXX} -DSNITCH_DEFAULT_WITH_COLOR=1 -DSNITCH_DEFINE_MAIN=1 -DSNITCH_MAX_COMMAND_LINE_ARGS=1024 -DSNITCH_MAX_EXPR_LENGTH=1024 -DSNITCH_MAX_MESSAGE_LENGTH=1024 -DSNITCH_MAX_NESTED_SECTIONS=8 -DSNITCH_MAX_TEST_CASES=5000 -DSNITCH_MAX_TEST_NAME_LENGTH=1024 -DSNITCH_MAX_UNIQUE_TAGS=1024 -DSNITCH_WITH_EXCEPTIONS=1 -DSNITCH_WITH_SHORTHAND_MACROS=1 -DSNITCH_WITH_TIMINGS=1 -I${PWD}/include -I${SYS_INCLUDE_DIR} -I${PWD}/build/out/this/test/nosan/_deps/snitch-src/include -I${PWD}/build/out/this/test/nosan/_deps/snitch-build -Wall -Wextra -Werror -std=${STD_FLAG} -o CMakeFiles/wtr.test_watcher.test_watch_targets.dir${PWD}/src/test_watcher/test_watch_targets/main.cpp.o -c ${PWD}/src/test_watcher/test_watch_targets/main.cpp\",
  \"file\": \"${PWD}/src/test_watcher/test_watch_targets/main.cpp\"
},
{
  \"directory\": \"${PWD}/build/out/this/test/nosan\",
  \"command\": \"${COMPILER_CXX} -DSNITCH_DEFAULT_WITH_COLOR=1 -DSNITCH_DEFINE_MAIN=1 -DSNITCH_MAX_COMMAND_LINE_ARGS=1024 -DSNITCH_MAX_EXPR_LENGTH=1024 -DSNITCH_MAX_MESSAGE_LENGTH=1024 -DSNITCH_MAX_NESTED_SECTIONS=8 -DSNITCH_MAX_TEST_CASES=5000 -DSNITCH_MAX_TEST_NAME_LENGTH=1024 -DSNITCH_MAX_UNIQUE_TAGS=1024 -DSNITCH_WITH_EXCEPTIONS=1 -DSNITCH_WITH_SHORTHAND_MACROS=1 -DSNITCH_WITH_TIMINGS=1 -I${PWD}/include -I${SYS_INCLUDE_DIR} -I${PWD}/build/out/this/test/nosan/_deps/snitch-src/include -I${PWD}/build/out/this/test/nosan/_deps/snitch-build -Wall -Wextra -Werror -std=${STD_FLAG} -o CMakeFiles/wtr.test_watcher.test_concurrent_watch_targets.dir${PWD}/src/test_watcher/test_concurrent_watch_targets/main.cpp.o -c ${PWD}/src/test_watcher/test_concurrent_watch_targets/main.cpp\",
  \"file\": \"${PWD}/src/test_watcher/test_concurrent_watch_targets/main.cpp\"
},
{
  \"directory\": \"${PWD}/build/out/this/test/nosan\",
  \"command\": \"${COMPILER_CXX} -DSNITCH_DEFAULT_WITH_COLOR=1 -DSNITCH_DEFINE_MAIN=1 -DSNITCH_MAX_COMMAND_LINE_ARGS=1024 -DSNITCH_MAX_EXPR_LENGTH=1024 -DSNITCH_MAX_MESSAGE_LENGTH=1024 -DSNITCH_MAX_NESTED_SECTIONS=8 -DSNITCH_MAX_TEST_CASES=5000 -DSNITCH_MAX_TEST_NAME_LENGTH=1024 -DSNITCH_MAX_UNIQUE_TAGS=1024 -DSNITCH_WITH_EXCEPTIONS=1 -DSNITCH_WITH_SHORTHAND_MACROS=1 -DSNITCH_WITH_TIMINGS=1 -I${PWD}/include -I${SYS_INCLUDE_DIR} -I${PWD}/build/out/this/test/nosan/_deps/snitch-src/include -I${PWD}/build/out/this/test/nosan/_deps/snitch-build -Wall -Wextra -Werror -std=${STD_FLAG} -o CMakeFiles/wtr.test_watcher.test_new_directories.dir${PWD}/src/test_watcher/test_new_directories/main.cpp.o -c ${PWD}/src/test_watcher/test_new_directories/main.cpp\",
  \"file\": \"${PWD}/src/test_watcher/test_new_directories/main.cpp\"
},
{
  \"directory\": \"${PWD}/build/out/this/test/nosan\",
  \"command\": \"${COMPILER_CXX} -DSNITCH_DEFAULT_WITH_COLOR=1 -DSNITCH_DEFINE_MAIN=1 -DSNITCH_MAX_COMMAND_LINE_ARGS=1024 -DSNITCH_MAX_EXPR_LENGTH=1024 -DSNITCH_MAX_MESSAGE_LENGTH=1024 -DSNITCH_MAX_NESTED_SECTIONS=8 -DSNITCH_MAX_TEST_CASES=5000 -DSNITCH_MAX_TEST_NAME_LENGTH=1024 -DSNITCH_MAX_UNIQUE_TAGS=1024 -DSNITCH_WITH_EXCEPTIONS=1 -DSNITCH_WITH_SHORTHAND_MACROS=1 -DSNITCH_WITH_TIMINGS=1 -I${PWD}/include -I${SYS_INCLUDE_DIR} -I${PWD}/build/out/this/test/nosan/_deps/snitch-src/include -I${PWD}/build/out/this/test/nosan/_deps/snitch-build -Wall -Wextra -Werror -std=${STD_FLAG} -o CMakeFiles/wtr.test_watcher.test_simple.dir${PWD}/src/test_watcher/test_simple/main.cpp.o -c ${PWD}/src/test_watcher/test_simple/main.cpp\",
  \"file\": \"${PWD}/src/test_watcher/test_simple/main.cpp\"
},
{
  \"directory\": \"${PWD}/build/out/this/test/nosan\",
  \"command\": \"${COMPILER_CXX} -DSNITCH_DEFAULT_WITH_COLOR=1 -DSNITCH_DEFINE_MAIN=1 -DSNITCH_MAX_COMMAND_LINE_ARGS=1024 -DSNITCH_MAX_EXPR_LENGTH=1024 -DSNITCH_MAX_MESSAGE_LENGTH=1024 -DSNITCH_MAX_NESTED_SECTIONS=8 -DSNITCH_MAX_TEST_CASES=5000 -DSNITCH_MAX_TEST_NAME_LENGTH=1024 -DSNITCH_MAX_UNIQUE_TAGS=1024 -DSNITCH_WITH_EXCEPTIONS=1 -DSNITCH_WITH_SHORTHAND_MACROS=1 -DSNITCH_WITH_TIMINGS=1 -I${PWD}/include -I${SYS_INCLUDE_DIR} -I${PWD}/build/out/this/test/nosan/_deps/snitch-src/include -I${PWD}/build/out/this/test/nosan/_deps/snitch-build -Wall -Wextra -Werror -std=${STD_FLAG} -o CMakeFiles/wtr.test_watcher.test_directory_events.dir${PWD}/src/test_watcher/test_directory_events/main.cpp.o -c ${PWD}/src/test_watcher/test_directory_events/main.cpp\",
  \"file\": \"${PWD}/src/test_watcher/test_directory_events/main.cpp\"
},
{
  \"directory\": \"${PWD}/build/out/this/test/nosan\",
  \"command\": \"${COMPILER_CXX} -DSNITCH_DEFAULT_WITH_COLOR=1 -DSNITCH_DEFINE_MAIN=1 -DSNITCH_MAX_COMMAND_LINE_ARGS=1024 -DSNITCH_MAX_EXPR_LENGTH=1024 -DSNITCH_MAX_MESSAGE_LENGTH=1024 -DSNITCH_MAX_NESTED_SECTIONS=8 -DSNITCH_MAX_TEST_CASES=5000 -DSNITCH_MAX_TEST_NAME_LENGTH=1024 -DSNITCH_MAX_UNIQUE_TAGS=1024 -DSNITCH_WITH_EXCEPTIONS=1 -DSNITCH_WITH_SHORTHAND_MACROS=1 -DSNITCH_WITH_TIMINGS=1 -I${PWD}/include -I${SYS_INCLUDE_DIR} -I${PWD}/build/out/this/test/nosan/_deps/snitch-src/include -I${PWD}/build/out/this/test/nosan/_deps/snitch-build -Wall -Wextra -Werror -std=${STD_FLAG} -o CMakeFiles/wtr.test_watcher.test_regular_file_events.dir${PWD}/src/test_watcher/test_regular_file_events/main.cpp.o -c ${PWD}/src/test_watcher/test_regular_file_events/main.cpp\",
  \"file\": \"${PWD}/src/test_watcher/test_regular_file_events/main.cpp\"
},
{
  \"directory\": \"${PWD}/build/out/this/test/nosan/_deps/snitch-build\",
  \"command\": \"${COMPILER_CXX} -DSNITCH_DEFAULT_WITH_COLOR=1 -DSNITCH_DEFINE_MAIN=1 -DSNITCH_MAX_COMMAND_LINE_ARGS=1024 -DSNITCH_MAX_EXPR_LENGTH=1024 -DSNITCH_MAX_MESSAGE_LENGTH=1024 -DSNITCH_MAX_NESTED_SECTIONS=8 -DSNITCH_MAX_TEST_CASES=5000 -DSNITCH_MAX_TEST_NAME_LENGTH=1024 -DSNITCH_MAX_UNIQUE_TAGS=1024 -DSNITCH_WITH_EXCEPTIONS=1 -DSNITCH_WITH_SHORTHAND_MACROS=1 -DSNITCH_WITH_TIMINGS=1 -I${PWD}/build/out/this/test/nosan/_deps/snitch-src/include -I${PWD}/build/out/this/test/nosan/_deps/snitch-build -std=${STD_FLAG} -o CMakeFiles/snitch.dir/src/snitch.cpp.o -c ${PWD}/build/out/this/test/nosan/_deps/snitch-src/src/snitch.cpp\",
  \"file\": \"${PWD}/build/out/this/test/nosan/_deps/snitch-src/src/snitch.cpp\"
},
{
  \"directory\": \"${PWD}/build/out/this/release\",
  \"command\": \"${COMPILER_CXX} -O3 -Wall -Wextra -Werror -std=${STD_FLAG} -I${PWD}/include -I${SYS_INCLUDE_DIR} -o CMakeFiles/wtr.watcher.dir${PWD}/src/watcher/main.cpp.o -c ${PWD}/src/watcher/main.cpp\",
  \"file\": \"${PWD}/src/watcher/main.cpp\"
}
]"

cd "$LAST_DIR" || exit 1
