#!/usr/bin/env bash
if [ "$#" -lt 1 ]; then
    echo "Usage: $0 <build-directory-name> [args-passed-to-meson-setup]..."
    exit 1
fi

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
LIBXKBCOMMON_DIR="$SCRIPT_DIR/libxkbcommon"
BUILD_DIR_NAME="$LIBXKBCOMMON_DIR/$1"
STATIC_LIB_PATH="$BUILD_DIR_NAME/libxkbcommon.a"
SETUP_ARGS=${@:2}

pushd "$LIBXKBCOMMON_DIR" >/dev/null
    meson setup "$BUILD_DIR_NAME" \
        --cross-file "$SCRIPT_DIR/cross-esp32s3.txt" \
        -Denable-x11=false \
        -Denable-wayland=false \
        -Denable-xkbregistry=false \
        -Denable-docs=false \
        -Denable-tools=false \
        -Dxkb-config-root=/usr/share/X11/xkb \
        -Dx-locale-root=/usr/share/X11/locale \
        $SETUP_ARGS
    meson compile -C "$BUILD_DIR_NAME" xkbcommon
    $SCRIPT_DIR/redefine-syms.sh "__xkbc_" "$STATIC_LIB_PATH" "$STATIC_LIB_PATH"
popd >/dev/null

GREEN='\033[0;32m'
RESET='\033[0m'
echo -e "${GREEN}Successfully compiled libxkbcommon. Path to static library:\n$STATIC_LIB_PATH${RESET}"
