#!/bin/bash

# Generated by cmake - rerun to reconfigure

dry_run_flag=false
clobber=false
testing=@ENABLE_TESTING@

orig_path=@CMAKE_BINARY_DIR@/reconfigure
self_path=$(cd $(dirname $0) && pwd)/$(basename $0)

# Parse arguments
while [[ $# -gt 0 ]]; do
    case "$1" in
        -n|--dry-run)
            dry_run_flag=true
            ;;
        --print-source-dir)
            echo @CMAKE_SOURCE_DIR@
            exit 0
            ;;
        --print-build-dir)
            echo @CMAKE_BINARY_DIR@
            exit 0
            ;;
        --enable-testing)
            testing=1
            ;;
        --disable-testing)
            testing=0
            ;;
        --clobber)
            clobber=true
            ;;
        --)
            break;
            ;;
        *)
            echo "error: xo-reconfigure: unexpected argument [$1]"
            echo "usage: xo-reconfigure"
            echo "       xo-reconfigure [-n|--dry-run]"
            echo "                      [--print-source-dir|--print-build-dir]"
            echo "                      [--clobber]"
            echo "                      [--enable-testing|--disable-testing]"
            echo "                      -- CMAKE_ARGS"
            echo ""
            echo "1. clobber ignores --enable-testing/--disable-testing flags"
            echo "   use to cleanse build directory when stale build artifacts"
            echo "   are confusing cmake"
            exit 1
    esac

    shift
done

# Build the cmake command
CMAKE_CMD=(
    cmake
    -B @CMAKE_BINARY_DIR@
    -S @CMAKE_SOURCE_DIR@
    -DCMAKE_BUILD_TYPE=@CMAKE_BUILD_TYPE@
    -DCMAKE_INSTALL_PREFIX=@CMAKE_INSTALL_PREFIX@
    -DCMAKE_INSTALL_DOCDIR=@CMAKE_INSTALL_DOCDIR@
    -DCMAKE_MODULE_PATH=@CMAKE_MODULE_PATH@
    -DCMAKE_PREFIX_PATH=@CMAKE_PREFIX_PATH@
    -DCMAKE_CXX_STANDARD=@CMAKE_CXX_STANDARD@
    -DXO_CMAKE_CONFIG_EXECUTABLE=@XO_CMAKE_CONFIG_EXECUTABLE@
    -DENABLE_TESTING=$testing
    -DXO_ENABLE_DOCS=@XO_ENABLE_DOCS@
    -DXO_ENABLE_ASM=@XO_ENABLE_ASM@
    -DXO_ENABLE_EXAMPLES=@XO_ENABLE_EXAMPLES@
    -DXO_ENABLE_VULKAN=@XO_ENABLE_VULKAN@
    -DXO_ENABLE_OPENGL=@XO_ENABLE_OPENGL@

    "${@}"
)

if [[ ${clobber} == true ]]; then
    if [[ ${dry_run_flag} == true ]]; then
        2>&1 echo "would clobber directory [@CMAKE_BINARY_DIR@], then run reconfigure:"
        2>&1 echo
    else
        if [[ ${orig_path} == ${self_path} ]]; then
            # must copy self out of doomed build directory before we clobber

            # 1. rescue reconfigure script from build directory
            tmpfile=$(mktemp /tmp/reconfigure-XXX)
            cp ${self_path} ${tmpfile}

            # 2. copy needs to be executable
            chmod +x ${tmpfile}

            # 2. run reconfigure from non-build directory
            exec ${tmpfile} --clobber
        else
            # running outside build directory

            2>&1 echo "removing build directory [@CMAKE_BINARY_DIR@]"
            rm -rf @CMAKE_BINARY_DIR@

            2>&1 echo "run cmake:"
            2>&1 echo "${CMAKE_CMD[@]}"
            2>&1 echo

            # run reconfigure
            "${CMAKE_CMD[@]}"

            if [[ $(realpath ${self_path}) == /tmp/* ]]; then
                # since we're in /tmp, delete ourselves as final step
                rm -f ${self_path}
            fi

            # rehearse cmake command for visibility
            2>&1 echo ""
            2>&1 echo "cmake command was:"
            2>&1 echo "${CMAKE_CMD[@]}"
        fi
    fi
else
    if [[ ${dry_run_flag} = true ]]; then
        # Print the command with proper quoting for copy-paste
        printf '%q ' "${CMAKE_CMD[@]}" | sed -e 's: -D: \\\n    -D:g'
        printf '\n'
    else
        # Execute the command
        "${CMAKE_CMD[@]}"
    fi
fi
