#!/bin/sh
export LC_ALL=C

# we only support --mode=... (or -c/-b) as the first argument of ./build
mode=
case "${1-}" in
    --mode=config|-c) mode=config; shift;;
     --mode=build|-b) mode=build; shift;;
          --mode=all) mode=; shift;;
            --mode=*) >&2 echo "Error: valid modes: all, config, build"; exit 1
esac
export build_mode="$mode"  # allow hooks to see this too

cd "$(dirname "$0")"
config_process_projects_cli=yes
. scripts/core-config || exit 1
. pkg/pkg-utils.sh

hook_or_die "$config_build_pre" build-pre
hook_or_die "$config_build_internal_pre"

[ "${PKG_CONFIG_PATH-}" ] && PKG_CONFIG_PATH=":$PKG_CONFIG_PATH"
export PKG_CONFIG_PATH="$config_local_prefix/lib/pkgconfig${PKG_CONFIG_PATH-}"

# some projects don't have .pc file (e.g. shaderc), and otherwise it doesn't
# hurt to have those FLAGS (except on windows if our prefix path has spaces,
# but this already breaks in general elsewhere).
export CFLAGS="-I$config_local_prefix/include ${CFLAGS-}"
export CXXFLAGS="-I$config_local_prefix/include ${CXXFLAGS-}"
export LDFLAGS="-L$config_local_prefix/lib ${LDFLAGS-}"

do_config_or_die() {
    local p=$1; shift
    local opts; opts=$(var_val config_${p}_opts_config)

    if pkg_has $p; then
        (pkg_load $p && pkg_configure $opts "$@") || err_msg_die "failed to configure $p"
    elif [ -e scripts/${p}-config ]; then
        scripts/${p}-config $opts "$@" || err_msg_die "failed to configure $p"
    else
        echo "No config (empty)"
    fi
}

do_build_or_die() {
    local p=$1; shift
    local opts; opts=$(var_val config_${p}_opts_build)

    if pkg_has $p; then
        (pkg_load $p && pkg_build $opts "$@") || err_msg_die "failed to build $p"
    elif [ -e scripts/${p}-build ]; then
        scripts/${p}-build $opts "$@" || err_msg_die "failed to build $p"
    else
        echo "No build (empty)"
    fi
}

# It's impractical to parse/separate mixed configure and build arguments at the
# same invocation, so by default (both configure and build), we pass the
# additional arguments only to build - and configure only reads its values from
# ${proj}_options. If we run in mode=configure then there's no issue and
# configure does get the additional cli args.
# mode=build is required too, or else if we previously used mode=config then
# the following build will run configure again without args.
for p in $config_projects; do
    f=./${p}_options
    if [ -e $f ]; then
        warn_msg "ignoring $f - set config_${p}_opts_config at user.conf instead"
    fi
    case "$build_mode" in
        config) highlight_msg "($config_projects) configure-only $p"
                do_config_or_die $p "$@"
                ;;
         build) highlight_msg "($config_projects) build-only $p"
                do_build_or_die  $p "$@"
                ;;
             *) highlight_msg "($config_projects) building $p"
                do_config_or_die $p  # no CLI args
                do_build_or_die  $p "$@"
    esac
done

hook_or_die "$config_build_post" build-post
success_msg "built: $config_projects"
