#!/bin/bash

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


otsb_setarr otsb_commands patch configure build test install



function load_cpan_conf () {

    test "$cpan_conf_loaded" && return

    cpan_vars=$( HOME="$@MST_SIMUL_ROOT_VAR@" perl -MCPAN -le 'print qq[$_='"'"'$CPAN::Config->{$_}'"';"'] for keys %$CPAN::Config' | grep -v 'loaded ok')

    eval $cpan_vars

    cpan_conf_loaded=1

}

otsb_cmd_configure () {


    load_cpan_conf

    if [ -f Makefile.PL ]; then

	perl Makefile.PL $makepl_arg

    elif [ -f Build.PL }; then

	perl Build.PL $mbuildpl_arg

    else

	die "$0: don't know how to build this Perl module"

    fi


}

otsb_cmd_build () {


    if [ -f Makefile.PL ]; then

	make

    elif [ -f Build.PL }; then

	./Build

    else

	die "$0: don't know how to build this Perl module"

    fi


}

otsb_cmd_test () {


    load_cpan_conf

    if [ -f Makefile.PL ]; then

	make test

    elif [ -f Build.PL }; then

	./Build test

    else

	die "$0: don't know how to build this Perl module"

    fi


}


otsb_cmd_install () {


    load_cpan_conf

    if [ -f Makefile.PL ]; then

	make install

    elif [ -f Build.PL }; then

	./Build install

    else

	die "$0: don't know how to build this Perl module"

    fi


}


otsb_init

otsb_options "$@"

otsb_set_build_variables

otsb_run_cmds
