#!/bin/sh

# Get the absolute path of the project root (where this script lives)
# This ensures the script works even if called from elsewhere
ROOT_DIR=$(cd "$(dirname "$0")" && pwd)
BUILD_DIR="$ROOT_DIR/build"

usage() {
    echo "Usage: $0 {debug|release|test}"
    exit 1
}

# Check if an argument was provided
if [ -z "$1" ]; then
    usage
fi

TYPE="$1"

case "$TYPE" in
    debug|release|test)
        TARGET_DIR="$BUILD_DIR/$TYPE"
        
        # Create directory structure if it doesn't exist
        mkdir -p "$TARGET_DIR"
        
        echo "--- Entering $TYPE build mode ---"
        cd "$TARGET_DIR" || exit 1
        
        # Set CMake build type based on argument
        # We capitalize the first letter to match CMake conventions
        if [ "$TYPE" = "debug" ]; then
            CMAKE_TYPE="Debug"
        elif [ "$TYPE" = "release" ]; then
            CMAKE_TYPE="Release"
        else
            CMAKE_TYPE="Debug" # Tests usually run in debug mode
        fi

        # Run CMake and Make
        cmake -DCMAKE_BUILD_TYPE="$CMAKE_TYPE" ../..
        
        # If it's the test build, we target 'test_suite', otherwise 'all'
        if [ "$TYPE" = "test" ]; then
            make test_suite
        else
            make
        fi
        ;;
    *)
        usage
        ;;
esac
