#!/bin/bash

# don't load this multiple times
if [[ "${_mst_otsb_loaded:=0}" && "$_mst_otsb_loaded" -eq 1 ]]; then
    return 0
else
    _mst_otsb_loaded=1
fi

if ots_build=$(pkg-config --libs ots-build); then
    . $ots_build
else
    echo >&2 "unable to load ots-build library"
    exit 1
fi


otsb_is_set _otsb_datadir \
    || _otsb_datadir=$(pkg-config --variable datadir ots-build)

otsb_source $_otsb_datadir/set-package.sh


otsb_assert_has_command sysarch
eval $(sysarch -sh)

mst_otsb_init_before () {

    otsb_set @MST_ROOT_VAR@ @MST_ROOT@
    otsb_set mst_root ${@MST_ROOT_VAR@}
    otsb_set ots_root ${@MST_ROOT_VAR@}/ots
    otsb_set ots_pkgs ${ots_root}/pkgs

    otsb_add_dumpvar				\
	mst_root
}

mst_otsb_options_before () {

    case $OS in

        SunOS )

            otsb_set make $(command -v gmake)
            otsbv_set patch-cmd /usr/local/bin/patch
            otsb_set tar gtar
            ;;

        * )
            otsb_set make $(command -v make)
            otsb_set tar  $(command -v tar)
            ;;

    esac

    otsb_default_value_of_sysplat="$platform_os_generic"
    otsb_default_value_of_mst_dev_config="true"

    otsb_add_option scalar ots-root 'top-level directory containing installed OTS []'
    otsb_add_option scalar ots-pkgs 'directory into which OTS packages will be installed []'
    otsb_add_option scalar sysplat  "system platform name []"
    otsb_add_option scalar make     "name of the make command []"
    otsb_add_option scalar tar      "name of the tar  command []"
    otsb_add_option bool    mst-dev-config "use mst-dev-config to get compilers and their args []"

    mst_otsb_load_buildd
}

# This is run by mst_otsb_options_before in an options_after hook
# loads all files in lexical order matching $otsbi_import_root/build.d/*.sh
mst_otsb_load_buildd () {
    # we depend upon ots-options loading the internal options via otsb_internal_options,
    # which loads otsbi_*
    local plugind="$otsbi_import_root/build.d"

    if [ -d "$plugind" ]; then
        local -a plugins=()
        mapfile -d '' -t plugins < <( find "$plugind" -name '*.sh' -print0 | sort -z )
        otsb_source "${plugins[@]}"
    fi
}                         

mst_otsb_options_after () {
    otsb_set sysplat "$(otsb_default_value sysplat)"

    if otsb_is_true mst_dev_config && otsb_has_command mst-dev-config ; then
        eval $(mst-dev-config --fmt bash --override --tools)
    fi
}

mst_otsb_set_build_variables_before () {
    otsb_set prefix $ots_root
    otsb_set exec_prefix "$prefix/$sysplat"
}

mst_otsb_set_archive () {

    local file

    for file in                                         \
        $otsbv_import_root/latest-${package_name}       \
        $otsbv_import_root/latest
    do
        if [ -L "$file" ]; then
            local filename=$(readlink "$file")
            otsbv_set archive "$filename"
            break
        fi

    done
}

# these either depend upon things set by otsb_set_build_variables
# or don't affect anything there
mst_otsb_set_build_variables_after () {

    otsb_set pkg_config_dir      "${exec_prefix}/lib/pkgconfig"

    otsb_set ots_prefix          "${ots_root}"
    otsb_set ots_exec_prefix     "${ots_prefix}/$sysplat"

    otsb_set ots_bindir          "${ots_exec_prefix}/bin"
    otsb_set ots_libdir          "${ots_exec_prefix}/lib"
    otsb_set ots_incdir          "${ots_prefix}/include/${package_name}"
    otsb_set ots_datarootdir     "${ots_prefix}/share"
    otsb_set ots_mandir          "${ots_datarootdir}/man"
    otsb_set ots_docdir          "${ots_datarootdir}/doc/${package_name}"
    otsb_set ots_infodir         "${ots_datarootdir}/info"
    otsb_set ots_datadir         "${ots_datarootdir}"
    otsb_set ots_pkgdatadir      "${ots_datadir}/${package_name}"

    otsb_set ots_pkg_prefix      "${ots_pkgs}/${package_name}-${package_version}"
    otsb_set ots_pkg_exec_prefix "${ots_pkg_prefix}/$sysplat"
    otsb_set ots_pkg_bindir      "${ots_pkg_exec_prefix}/bin"
    otsb_set ots_pkg_libdir      "${ots_pkg_exec_prefix}/lib"
    otsb_set ots_pkg_config_dir  "${ots_pkg_libdir}/pkgconfig"
    otsb_set ots_pkg_incdir      "${ots_pkg_prefix}/include/${package_name}"

    otsb_set ots_pkg_datarootdir "${ots_pkg_prefix}/share"
    otsb_set ots_pkg_mandir      "${ots_pkg_datarootdir}/man"
    otsb_set ots_pkg_docdir      "${ots_pkg_datarootdir}/doc/${package_name}"
    otsb_set ots_pkg_infodir     "${ots_pkg_datarootdir}/info"
    otsb_set ots_pkg_datadir     "${ots_pkg_datarootdir}"
    otsb_set ots_pkg_pkgdatadir  "${ots_pkg_datadir}/${package_name}"

    otsb_add_dumpvar                            \
        ots_prefix                              \
        ots_exec_prefix                         \
        ots_bindir                              \
        ots_libdir                              \
        ots_incdir                              \
        ots_datarootdir                         \
        ots_mandir                              \
        ots_docdir                              \
        ots_infodir                             \
        ots_datadir                             \
        ots_pkgdatadir                          \
        ots_pkg_prefix                          \
        ots_pkg_exec_prefix                     \
        ots_pkg_config_dir                      \
        ots_pkg_bindir                          \
        ots_pkg_libdir                          \
        ots_pkg_incdir                          \
        ots_pkg_datarootdir                     \
        ots_pkg_mandir                          \
        ots_pkg_docdir                          \
        ots_pkg_infodir                         \
        ots_pkg_datadir                         \
        ots_pkg_pkgdatadir

    export MAKE="$make"
    export TAR="$tar"

    mst_otsb_set_archive
}


mst_otsb_mk-pkg-config-lib () {

    local name
    local desc
    local libs
    local _package_name
    local _cflags
    local _include_dir
    local _prefix
    local _exec_prefix
    local _use_ots_prefix=1
    local _prefix_is_exec_prefix=0
    local _rpath=0
    local _pkg=0

    OPTIND=1
    while getopts :a:c:d:e:l:i:n:N:p:u:s:r opt
    do

        case $opt in

            a ) _pkg="$OPTARG" ;;
            c ) _cflags="$OPTARG" ;;
            d ) desc="$OPTARG" ;;
            e ) _exec_prefix="$OPTARG" ;;
            l ) libs="$OPTARG" ;;
            i ) _includedir="$OPTARG" ;;
            n ) name="$OPTARG" ;;
            N ) _package_name="$OPTARG" ;;
            p ) _prefix="$OPTARG" ;;
            u ) _use_ots_prefix="$OPTARG" ;;
            r ) _rpath=1 ;;
            s ) _prefix_is_exec_prefix="$OPTARG";;
            : ) otsb_die "$FUNCNAME: unknown option or missing argument: -$OPTARG" ;;

        esac
    done

    [[ -z "$name" || -z "$desc" ]] &&
    otsb_die "$FUNCNAME: must specify -n and -d"

    otsb_set _package_name "$package_name"

    if otsb_is_true $_use_pkg_dir ; then

        if otsb_is_false $_prefix_is_exec_prefix ; then
            otsb_set _prefix "$prefix"
        else
            otsb_set _prefix "$exec_prefix"
        fi

        otsb_set _exec_prefix "$exec_prefix"

    else

        if otsb_is_false $_prefix_is_exec_prefix ; then
            otsb_set _prefix "$ots_prefix"
        else
            otsb_set _prefix "$ots_exec_prefix"
        fi

        otsb_set _exec_prefix "$ots_exec_prefix"
    fi

    # old bash messes up local -a and setting of values w/ spaces
    local -a _config_opts
    _config_opts=(                                              \
        --prefix="$_prefix"                                     \
        --exec_prefix="$_exec_prefix"                           \
        --name "$name"                                          \
        --desc "$desc"                                          \
        --pversion "$package_version"                           \
        --output="${ots_pkg_config_dir}/${_package_name}.pc"    \
        )

    if otsb_is_true $_pkg ; then
        _config_opts+=( --pkg=$name )
    fi

    if otsb_is_true $_rpath ; then
        _config_opts+=( --rpath )
    fi

    if  [ -n "$libs" ] ; then
        _config_opts+=( --libs="$libs"  )
    fi

    if  [ -n "$_includedir" ] ; then
        _config_opts+=( --includedir="$_includedir" )
    fi

    if  [ -n "$_cflags" ] ; then
        _config_opts+=( --cflags="$_cflags" )
    fi

    mkdir -p "$ots_pkg_config_dir"
    mkpkg-config "${_config_opts[@]}"
}

otsb_hook_push_fn_back init_before            mst_otsb_init_before
otsb_hook_push_fn_back options_before         mst_otsb_options_before
otsb_hook_push_fn_back options_after          mst_otsb_options_after
otsb_hook_push_fn_back build_variables_before mst_otsb_set_build_variables_before
otsb_hook_push_fn_back build_variables_after  mst_otsb_set_build_variables_after
