#!/bin/bash
set -eu

# -----------------------------------------------------------------
# Test compilation of various CGE CastleWindow backends.
# Assumes $CASTLE_ENGINE_PATH is set.
# Current directory doesn't matter.
# -----------------------------------------------------------------

TEST_PROJECT=examples/viewport_and_scenes/view_3d_model_advanced/
TEST_LPI="${TEST_PROJECT}"view_3d_model_advanced.lpi

# Run the command in parameters, reporting it to console too.
do_run ()
{
  echo '---------------------------------------------------------'
  echo 'Running:'
  echo "$@"
  "$@"
}

# Test the LCL backend.
# This requires a bit different code than standard do_test_one_backend implementation.
# Parameters: OS CPU
do_test_one_backend_lcl ()
{
  LAZBUILD_OPTIONS="--os=$1 --cpu=$2"
  lazbuild $LAZBUILD_OPTIONS packages/alternative_castle_window_based_on_lcl.lpk

  # Depend on alternative_castle_window_based_on_lcl from TEST_LPI
  sed -e 's|<PackageName Value="castle_window"/>|<PackageName Value="alternative_castle_window_based_on_lcl"/>|' \
    --in-place "${TEST_LPI}"
  do_run lazbuild $LAZBUILD_OPTIONS "${TEST_LPI}"

  # Restore original view_3d_model_advanced.lpi
  sed -e 's|<PackageName Value="alternative_castle_window_based_on_lcl"/>|<PackageName Value="castle_window"/>|' \
    --in-place "${TEST_LPI}"
}

# Test a single backend.
# Parameters: OS CPU backend-name
do_test_one_backend ()
{
  if [ "$3" = 'CASTLE_WINDOW_FORM' ]; then
    do_test_one_backend_lcl $1 $2
  else
    # Cleans the project, to remove compilation outputs with defined CASTLE_WINDOW_*
    do_run "${BUILD_TOOL}" --project="${TEST_PROJECT}" clean
    do_run "${BUILD_TOOL}" --project="${TEST_PROJECT}" compile --os=$1 --cpu=$2 --mode=debug --compiler-option=-d$3
    do_run "${BUILD_TOOL}" --project="${TEST_PROJECT}" clean
    do_run "${BUILD_TOOL}" --project="${TEST_PROJECT}" compile --os=$1 --cpu=$2 --mode=release --compiler-option=-d$3
  fi
}

# Test all CastleWindow backends.
do_test_all_backends ()
{
  do_test_one_backend linux x86_64 CASTLE_WINDOW_GTK_2
  do_test_one_backend linux x86_64 CASTLE_WINDOW_XLIB
  do_test_one_backend linux x86_64 CASTLE_WINDOW_TEMPLATE
  # Note that CASTLE_WINDOW_LIBRARY backend will not really work
  # with a standalone application, but it should compile OK.
  do_test_one_backend linux x86_64 CASTLE_WINDOW_LIBRARY
  do_test_one_backend linux x86_64 CASTLE_WINDOW_FORM

  do_test_one_backend win32 i386 CASTLE_WINDOW_WINAPI
  # For now we don't support CASTLE_WINDOW_GTK_2 on non-Unix,
  # see "TODO: WindowXID is only with X" in implementation.
  # This could be fixed if ever needed -- but in practice there's no point using
  # GTK backend e.g. on Windows, since we have WinAPI backend.
  # do_test_one_backend win32 i386 CASTLE_WINDOW_GTK_2
  do_test_one_backend win32 i386 CASTLE_WINDOW_TEMPLATE
  do_test_one_backend win32 i386 CASTLE_WINDOW_LIBRARY
  do_test_one_backend win32 i386 CASTLE_WINDOW_FORM

  do_test_one_backend win64 x86_64 CASTLE_WINDOW_WINAPI
  # See above for CASTLE_WINDOW_GTK_2 on non-Unix notes.
  # do_test_one_backend win64 x86_64 CASTLE_WINDOW_GTK_2
  do_test_one_backend win64 x86_64 CASTLE_WINDOW_TEMPLATE
  do_test_one_backend win64 x86_64 CASTLE_WINDOW_LIBRARY
  do_test_one_backend win64 x86_64 CASTLE_WINDOW_FORM
}

cd "${CASTLE_ENGINE_PATH}"

# make sure no leftover compilation remains with some specific CASTLE_WINDOW_*
make clean

./tools/build-tool/castle-engine_compile.sh
BUILD_TOOL="${CASTLE_ENGINE_PATH}/tools/build-tool/castle-engine"

do_test_all_backends
