#!/bin/sh
#
# configure - Configuration script for libAC v3.0
#
# 2026-11-04: 3.1.0: default compiler is cc, since FreeBSD has no gcc; the
# Makefile now reads config.mk.
#
# Copyright (C) 2025-2026 by Robert Cousins.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# This script detects the build environment and generates config.mk
# for use by the Makefile.
#
# Usage: ./configure [options]
#
# Options:
#   --prefix=PREFIX        Installation prefix [/usr/local]
#   --enable-debug         Enable debug build
#   --disable-shared       Don't build shared library
#   --help                 Display this help
#

# Default values
PREFIX="/usr/local"
ENABLE_DEBUG=no
ENABLE_SHARED=yes
CC="${CC:-cc}"

# Parse command line options
for arg in "$@"; do
    case "$arg" in
        --prefix=*)
            PREFIX="${arg#--prefix=}"
            ;;
        --enable-debug)
            ENABLE_DEBUG=yes
            ;;
        --disable-shared)
            ENABLE_SHARED=no
            ;;
        --help)
            echo "Usage: ./configure [options]"
            echo ""
            echo "Options:"
            echo "  --prefix=PREFIX        Installation prefix [/usr/local]"
            echo "  --enable-debug         Enable debug build"
            echo "  --disable-shared       Don't build shared library"
            echo "  --help                 Display this help"
            exit 0
            ;;
        *)
            echo "Unknown option: $arg"
            echo "Try './configure --help' for more information."
            exit 1
            ;;
    esac
done

echo "Configuring libAC v3.1.0..."
echo ""

# Detect operating system
OS=$(uname -s)
echo "Detected operating system: $OS"

# Detect architecture
ARCH=$(uname -m)
echo "Detected architecture: $ARCH"

# Check for required tools
echo ""
echo "Checking for required tools..."

check_tool() {
    tool=$1
    if command -v "$tool" >/dev/null 2>&1; then
        echo "  $tool: found"
        return 0
    else
        echo "  $tool: NOT FOUND"
        return 1
    fi
}

HAVE_CC=no
if check_tool "$CC"; then
    HAVE_CC=yes
fi

HAVE_AR=no
if check_tool ar; then
    HAVE_AR=yes
fi

HAVE_RANLIB=no
if check_tool ranlib; then
    HAVE_RANLIB=yes
fi

if [ "$HAVE_CC" = "no" ] || [ "$HAVE_AR" = "no" ]; then
    echo ""
    echo "ERROR: Required tools not found"
    echo "Please install a C compiler and binutils"
    exit 1
fi

# Check for optional tools
echo ""
echo "Checking for optional tools..."

HAVE_PTHREADS=yes
cat > conftest.c <<EOF
#include <pthread.h>
int main(void) { pthread_t t; return 0; }
EOF

if $CC -pthread conftest.c -o conftest >/dev/null 2>&1; then
    echo "  pthreads: available"
    HAVE_PTHREADS=yes
else
    echo "  pthreads: not available (thread-safe features disabled)"
    HAVE_PTHREADS=no
fi
rm -f conftest.c conftest conftest.o

# Platform-specific settings
case "$OS" in
    Linux)
        SHARED_EXT="so"
        SHARED_FLAGS="-shared -Wl,-soname,libAC.so.3"
        INSTALL_LDCONFIG=yes
        ;;
    Darwin)
        SHARED_EXT="dylib"
        SHARED_FLAGS="-dynamiclib -install_name $PREFIX/lib/libAC.3.dylib"
        INSTALL_LDCONFIG=no
        ;;
    FreeBSD)
        SHARED_EXT="so"
        SHARED_FLAGS="-shared"
        INSTALL_LDCONFIG=no
        ;;
    *)
        echo "WARNING: Unknown operating system $OS"
        echo "Assuming Linux-like environment"
        SHARED_EXT="so"
        SHARED_FLAGS="-shared"
        INSTALL_LDCONFIG=no
        ;;
esac

# Set compiler flags
CFLAGS="-Wall -Wextra -O2 -I. -fPIC"
if [ "$ENABLE_DEBUG" = "yes" ]; then
    CFLAGS="$CFLAGS -g -DDEBUG"
else
    CFLAGS="$CFLAGS -DNDEBUG"
fi

# Generate config.mk
echo ""
echo "Generating config.mk..."

cat > config.mk <<EOF
# config.mk - Generated by configure script
# DO NOT EDIT - Re-run ./configure to change settings

# Installation prefix
PREFIX = $PREFIX

# Compiler and tools
CC = $CC
AR = ar
RANLIB = ranlib

# Compiler flags
CFLAGS = $CFLAGS

# Shared library settings
SHARED_EXT = $SHARED_EXT
SHARED_FLAGS = $SHARED_FLAGS
ENABLE_SHARED = $ENABLE_SHARED

# Optional features
HAVE_PTHREADS = $HAVE_PTHREADS

# Platform
OS = $OS
ARCH = $ARCH

# Installation
INSTALL_LDCONFIG = $INSTALL_LDCONFIG
EOF

# Summary
echo ""
echo "Configuration summary:"
echo "  Installation prefix: $PREFIX"
echo "  C compiler: $CC"
echo "  Debug mode: $ENABLE_DEBUG"
echo "  Shared library: $ENABLE_SHARED"
echo "  Thread support: $HAVE_PTHREADS"
echo "  Platform: $OS ($ARCH)"
echo ""
echo "Configuration complete. Now run 'make' to build."
echo ""

exit 0
