#!/bin/sh
# configure — CMake wrapper for SFST
#
# Provides the familiar ./configure && make && sudo make install workflow.
# All actual work is done by CMake; this script just sets up a build directory
# and forwards the most common options.
#
# Usage:
#   ./configure [OPTIONS]
#   make -j$(nproc)
#   sudo make install

set -e

# ── Defaults ──────────────────────────────────────────────────────────────────
PREFIX=/usr/local
BUILD_DIR=build
READLINE=ON
BUILD_TYPE=Release
JOBS=$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 1)
EXTRA_CMAKE_ARGS=""

# ── Help ──────────────────────────────────────────────────────────────────────
usage() {
    cat <<EOF
Usage: ./configure [OPTIONS]

Options:
  --prefix=PREFIX         Install prefix (default: /usr/local)
  --build-dir=DIR         CMake build directory (default: build)
  --without-readline      Disable readline support in fst-mor
  --with-shared-lib       Build the libsfst shared library
  --regenerate-parser     Re-run bison/flex (requires bison ≥ 3 and flex)
  --debug                 Build with debug symbols instead of -O3
  --jobs=N                Parallel jobs for make (default: $(nproc 2>/dev/null || echo 1))
  --cmake-args=ARGS       Extra arguments forwarded verbatim to cmake
  --help, -h              Show this message and exit

After running configure, build and install with:
  make -j\$JOBS
  sudo make install

Or use CMake directly for more control:
  mkdir build && cd build
  cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local
  cmake --build . -j\$(nproc)
  sudo cmake --install .
EOF
}

# ── Argument parsing ──────────────────────────────────────────────────────────
for arg; do
    case $arg in
        --prefix=*)             PREFIX=${arg#--prefix=}             ;;
        --build-dir=*)          BUILD_DIR=${arg#--build-dir=}       ;;
        --without-readline)     READLINE=OFF                        ;;
        --with-shared-lib)      EXTRA_CMAKE_ARGS="$EXTRA_CMAKE_ARGS -DBUILD_SHARED_LIBRARY=ON" ;;
        --regenerate-parser)    EXTRA_CMAKE_ARGS="$EXTRA_CMAKE_ARGS -DREGENERATE_PARSER=ON"    ;;
        --debug)                BUILD_TYPE=Debug                    ;;
        --jobs=*)               JOBS=${arg#--jobs=}                 ;;
        --cmake-args=*)         EXTRA_CMAKE_ARGS="$EXTRA_CMAKE_ARGS ${arg#--cmake-args=}" ;;
        --help|-h)              usage; exit 0                       ;;
        *)
            echo "configure: unknown option: $arg" >&2
            echo "Run './configure --help' for a list of options." >&2
            exit 1
            ;;
    esac
done

# ── Locate CMake ──────────────────────────────────────────────────────────────
if ! command -v cmake >/dev/null 2>&1; then
    echo "configure: cmake not found in PATH." >&2
    echo "Install cmake (e.g. 'sudo apt install cmake' or 'sudo dnf install cmake')." >&2
    exit 1
fi

CMAKE_VERSION=$(cmake --version | head -1 | sed 's/cmake version //')
echo "configure: using cmake $CMAKE_VERSION"

# ── Create build directory ────────────────────────────────────────────────────
mkdir -p "$BUILD_DIR"
SRCDIR=$(cd "$(dirname "$0")" && pwd)

# ── Run cmake ─────────────────────────────────────────────────────────────────
echo "configure: running cmake in $BUILD_DIR ..."
# shellcheck disable=SC2086
cmake "$SRCDIR" \
    -B "$BUILD_DIR" \
    -DCMAKE_INSTALL_PREFIX="$PREFIX" \
    -DCMAKE_BUILD_TYPE="$BUILD_TYPE" \
    -DWITH_READLINE="$READLINE" \
    $EXTRA_CMAKE_ARGS

# ── Write a tiny Makefile wrapper ─────────────────────────────────────────────
cat > Makefile <<MAKEFILE_EOF
# Auto-generated by ./configure — do not edit.
# Source: $SRCDIR
BUILD_DIR = $BUILD_DIR
JOBS      = $JOBS

.PHONY: all install clean distclean

all:
	cmake --build \$(BUILD_DIR) -j\$(JOBS)

install:
	cmake --install \$(BUILD_DIR)

clean:
	cmake --build \$(BUILD_DIR) --target clean

distclean:
	rm -rf \$(BUILD_DIR) Makefile
MAKEFILE_EOF

echo ""
echo "configure: done."
echo ""
echo "  Build:   make -j$JOBS"
echo "  Install: sudo make install"
echo "  Prefix:  $PREFIX"
echo ""
