#!/bin/sh

RUN_AFTER_BUILD=0
CMAKE_ARGS=""
BINARY_NAME="Main"
BUILD_DIR="build"

for arg in "$@"; do
  case "$arg" in
    --clean)
      echo "Cleaning build directory..."
      rm -rf build/*
      ;;
    --debug)
      echo "Enabling debug build..."
      CMAKE_ARGS="-DCMAKE_BUILD_TYPE=Debug"
      ;;
    --run)
      RUN_AFTER_BUILD=1
      ;;
    *)
      echo "Unknown argument: $arg"
      echo "Usage: $0 [--clean] [--debug] [--run]"
      exit 1
      ;;
  esac
done

mkdir -p "$BUILD_DIR"

cmake -B "$BUILD_DIR" -S . -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ $CMAKE_ARGS

cmake --build "$BUILD_DIR" -- -j"$(nproc)"

if [ "$RUN_AFTER_BUILD" -eq 1 ]; then
  if [ -x "build/$BINARY_NAME" ]; then
    echo "Running ./$BINARY_NAME..."
    "./build/$BINARY_NAME"
  else
    echo "Error: Binary 'build/$BINARY_NAME' not found or not executable."
    exit 1
  fi
fi
