#!/bin/sh

set -eu

target=$(mktemp)

# Dependencies
DEPS="Qt6Core Qt6Gui Qt6Test Qt6Widgets"

# Use some of the tests by upstream for autopkgtest
TESTS="test/convert test/realtime test/transpose"

echo "We need to create the rosegardenprivate_export header first"
if sed 's|#cmakedefine01 ROSEGARDENPRIVATE_STATIC_BUILD|#define ROSEGARDENPRIVATE_STATIC_BUILD 1|' src/rosegardenprivate_export.h.cmake > src/rosegardenprivate_export.h; then
   echo "src/rosegardenprivate_export.h is ready"
else
   echo "Something went wrong, please examine the rosegardenprivate_export.h.cmake file"
fi

for TEST in $TESTS
do
  echo "Create the moc file for $TEST.cpp"
  /usr/lib/qt6/libexec/moc $TEST.cpp -o $TEST.moc $(pkgconf --cflags Qt6Core Qt6Gui Qt6Test Qt6Widgets)
  echo "Compile and capture output of $TEST.cpp"
  if g++ $TEST.cpp -Isrc/ $(pkgconf --cflags Qt6Core Qt6Gui Qt6Test Qt6Widgets) $(pkgconf --libs Qt6Core Qt6Gui Qt6Test Qt6Widgets) -o "$target" 2>&1; then
     echo "Compilation: OK — binary written to $target"
     echo "Binary size: $(stat -c%s "$target") bytes"
  else
     echo "Compilation: FAILED"
     exit 1
  fi

  echo "Run binary"
  if "$target"; then
     echo "Run: OK"
  else
     echo "Run: FAILED"
     exit 1
  fi
done

exit 0
