#!/bin/sh

# Build CoolProp's macOS shared library for use by LuaCoolProp.
#
# The CoolProp checkout and build directories live next to this project, so this
# script does not change any LuaCoolProp source files. Set COOLPROP_MACOS_ARCH to
# override the target architecture, for example:
#
#   COOLPROP_MACOS_ARCH=x86_64 scripts/build-coolprop-macos.sh
#   COOLPROP_MACOS_ARCH='arm64;x86_64' scripts/build-coolprop-macos.sh

set -eu

fail()
{
    printf 'error: %s\n' "$*" >&2
    exit 1
}

need_command()
{
    command -v "$1" >/dev/null 2>&1 || fail "required command not found: $1"
}

need_command git
need_command cmake
need_command ninja
need_command uname
need_command dirname
need_command tr

[ "$(uname -s)" = Darwin ] || fail "this script can only build a macOS .dylib"

# Resolve locations from the script rather than the caller's working directory.
SCRIPT_DIR=$(CDPATH= cd -P "$(dirname "$0")" && pwd) \
    || fail "could not determine the script directory"
PROJECT_DIR=$(CDPATH= cd -P "$SCRIPT_DIR/.." && pwd) \
    || fail "could not determine the project directory"
PARENT_DIR=$(CDPATH= cd -P "$PROJECT_DIR/.." && pwd) \
    || fail "could not determine the parent directory"
COOLPROP_SOURCE_DIR=$PARENT_DIR/coolprop-src

# uname reports x86_64 under Rosetta, so also check whether this process is
# translated before choosing the Apple Silicon default.
if [ -n "${COOLPROP_MACOS_ARCH:-}" ]; then
    MACOS_ARCH=$COOLPROP_MACOS_ARCH
else
    HOST_ARCH=$(uname -m)
    MACOS_ARCH=$HOST_ARCH
    if [ "$HOST_ARCH" = x86_64 ] && command -v sysctl >/dev/null 2>&1; then
        if [ "$(sysctl -in sysctl.proc_translated 2>/dev/null || printf '0')" = 1 ]; then
            MACOS_ARCH=arm64
        fi
    fi
fi

# Make the CMake architecture list safe to use as part of a directory name.
ARCH_TAG=$(printf '%s' "$MACOS_ARCH" | tr -c 'A-Za-z0-9_.-' '_')
[ -n "$ARCH_TAG" ] || fail "COOLPROP_MACOS_ARCH must not be empty"
COOLPROP_BUILD_DIR=$PARENT_DIR/coolprop-build-macos-$ARCH_TAG

if [ ! -e "$COOLPROP_SOURCE_DIR" ]; then
    printf 'Cloning CoolProp into %s\n' "$COOLPROP_SOURCE_DIR"
    git clone https://github.com/CoolProp/CoolProp.git "$COOLPROP_SOURCE_DIR" \
        || fail "failed to clone CoolProp"
elif ! git -C "$COOLPROP_SOURCE_DIR" rev-parse --is-inside-work-tree \
    >/dev/null 2>&1; then
    fail "$COOLPROP_SOURCE_DIR exists but is not a Git checkout"
else
    printf 'Using existing CoolProp checkout at %s\n' "$COOLPROP_SOURCE_DIR"
fi

printf 'Initializing and updating CoolProp submodules\n'
git -C "$COOLPROP_SOURCE_DIR" submodule update --init --recursive \
    || fail "failed to initialize or update CoolProp submodules"

printf 'Configuring CoolProp for macOS architecture: %s\n' "$MACOS_ARCH"
cmake -S "$COOLPROP_SOURCE_DIR" -B "$COOLPROP_BUILD_DIR" -G Ninja \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_OSX_ARCHITECTURES="$MACOS_ARCH" \
    -DCOOLPROP_SHARED_LIBRARY=ON \
    -DCOOLPROP_STATIC_LIBRARY=OFF \
    -DCOOLPROP_OBJECT_LIBRARY=OFF \
    || fail "CoolProp CMake configuration failed"

printf 'Building the CoolProp shared library\n'
cmake --build "$COOLPROP_BUILD_DIR" --target CoolProp \
    || fail "CoolProp build failed"

COOLPROP_LIBRARY=$COOLPROP_BUILD_DIR/libCoolProp.dylib
[ -f "$COOLPROP_LIBRARY" ] \
    || fail "build completed, but $COOLPROP_LIBRARY was not found"

# Keep this as the final line so callers can easily capture the resulting path.
printf '%s\n' "$COOLPROP_LIBRARY"
