## =====================================================================================================================
##  fennec, a free and open source game engine
##  Copyright © 2025 - 2026  Medusa Slockbower
##
##  This program is free software: you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation, either version 3 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program.  If not, see <https://www.gnu.org/licenses/>.
## =====================================================================================================================


## Help ================================================================================================================

Help()
{
  echo "Bash script for running the fennec test suite. By default, the build script executes in debug mode."
  echo
  echo "GNU long options:"
  echo "--help (-h)            = Print this help info."
  echo "--debug (-d)           = Build in debug mode, provides full debugging information to use with a debugger."
  echo "--release (-r)         = Build in release mode, provides full optimizations, eliminating debug info."
  echo "--relwithdebinfo (-i)  = Build in release mode with extra debug info, provides partial optimizations."
  echo "--minsizerel (-m)      = Build in release mode but optimizing for minimum binary size."
  echo "--all (-a)             = Build all profiles."
}


## Build Profiles ======================================================================================================

Debug()
{
  mkdir -p build/debug
  cd ./build/debug || exit
  cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -S ../.. -B .
  cmake --build . --target fennec-test
  cd ../../bin/debug/ || exit
  valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --log-file=valgrind.txt ./fennec-test
  cd ../..
}

Release()
{
  mkdir -p build/release
  cd ./build/release || exit
  cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -S ../.. -B .
  cmake --build . --target fennec-test
  cd ../../bin/release/ || exit
  valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --log-file=valgrind.txt ./fennec-test
  cd ../..
}

RelWithDebInfo()
{
  mkdir -p build/relwithdebinfo
  cd ./build/relwithdebinfo || exit
  cmake -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -S ../.. -B .
  cmake --build . --target fennec-test
  cd ../../bin/relwithdebinfo/ || exit
  valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --log-file=valgrind.txt ./fennec-test
  cd ../..
}

MinSizeRel()
{
  mkdir -p build/minsizerel
  cd ./build/minsizerel || exit
  cmake -G Ninja -DCMAKE_BUILD_TYPE=MinSizeRel -S ../.. -B .
  cmake --build . --target fennec-test
  cd ../../bin/minsizerel/ || exit
  valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --log-file=valgrind.txt ./fennec-test
  cd ../..
}

All()
{
  Debug
  Release
  RelWithDebInfo
  MinSizeRel
}


# Main Program =========================================================================================================

if [[ $# -eq 0 ]] ; then
  Debug
  exit 0
fi

while [ "${1:-}" != '' ]; do
  case "$1" in
    '--debug' | '-d')
      Debug
      exit;;

    '--release' | '-r')
      Release
      exit;;

    '--relwithdebinfo' | '-i')
      RelWithDebInfo
      exit;;

    '--minsizerel' | '-m')
      MinSizeRel
      exit;;

    '--all' | '-a')
      All
      exit;;

    *)
      echo "Error: Invalid option"
      exit;;
  esac
done