#!/bin/sh

# -------------------------------------------------------------------------
# Location of wx:
# -------------------------------------------------------------------------

update_prefixes()
{
    includedir="@includedir@"
    libdir="@libdir@"
}
prefix="@prefix@"
exec_prefix="@exec_prefix@"
update_prefixes

srcdir="@top_srcdir@"
builddir="@top_builddir_wxconfig@"


# -------------------------------------------------------------------------
# Wrapper script:
# -------------------------------------------------------------------------

exec_prefix_set=no

# return the absolute path prepending builddir to it if needed
makeabs()
{
    path=$1
    # TODO: this only works under Unix and even there it could be
    #       enhanced to remove ".." and "."
    if [ `echo $path | sed 's/^\(.\).*/\1/'` != "/" ]; then
        if [ $path = "." ]; then
            path=$builddir
        else
            path="$builddir/$path"
        fi
    fi

    echo $path
}

# these determine wx-config script to use:
m_toolkit='.*'
m_univ='\(univ\)\?'
m_unicode='\(unicode\|ansi\)'
m_debug='\(debug\|release\)'
m_version='[0-9]\+\.[0-9]\+'
m_host=''

# lists all wx-config scripts that match criteria specified above
list_wx_config_scripts()
{
    mask="^${m_toolkit}${m_univ}-${m_unicode}-${m_debug}-${m_version}${m_host}$"

    # if wx-config was called via wx$TOOLCHAIN_NAME-config symlink,
    # try to extract the mask from it:
    myname=`basename $0`
    if test $myname != wx-config ; then
        toolchain=`echo $myname | sed 's/wx\(.*\)-config/\1/'`
        if test -f ${libdir}/wx/config/${toolchain} ; then
            mask=$toolchain
        fi
    fi

    if test -d ${libdir}/wx/config ; then
        (cd ${libdir}/wx/config/ && ls -1 | grep "$mask" 2>/dev/null)
    fi
}

# find first wx-config script that matches criteria
find_wx_config_script()
{
    all_scripts=`list_wx_config_scripts`
    # unless wxBase was explicitly requested, try to find a GUI version:
    if test "$m_toolkit" != "base" ; then
        script=`echo $all_scripts | tr ' ' '\n' | grep -v '^base' | head -n 1`
        if test "x$script" != "x" ; then
            echo ${libdir}/wx/config/${script}
	    exit 0
        fi
    fi
   
    # otherwise (or if no GUI script was found), use alphabetically 1st script:
    script=`echo $all_scripts | head -n 1`
    if test -z "$script" ; then
        echo "no suitable wx-config script found" >&2
        exit 1
    fi
    echo ${libdir}/wx/config/${script}
}


# handle options:
 
# arguments to pass to the real wx-config script:
args=""

for i in $*; do
    case "$i" in
        -*=*) optarg=`echo "$i" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
        *) optarg= ;;
    esac

    case $i in
        --inplace)
            prefix=`makeabs $srcdir`
            exec_prefix=`makeabs $builddir`
            exec_prefix_set=yes
            update_prefixes
            args="$args --inplace"
            ;;
        --prefix=*)
            prefix=$optarg
            if test $exec_prefix_set = no ; then
                exec_prefix=$optarg
            fi
            update_prefixes
            ;;
        --exec-prefix=*)
            exec_prefix=$optarg
            exec_prefix_set=yes
            update_prefixes
            ;;

        --list)
            # list available wx versions:
            list_wx_config_scripts
            exit
            ;;

        --toolkit=*)
            m_toolkit=$optarg
            ;;
        --version=*)
            m_version=$optarg
            ;;
        --unicode=*)
            if test "x$optarg" = "xyes" ; then
                m_unicode="unicode"
            else
                m_unicode="ansi"
            fi
            ;;
        --unicode)
            m_unicode="unicode"
            ;;
        --debug=*)
            if test "x$optarg" = "xyes" ; then
                m_debug="debug"
            else
                m_debug="release"
            fi
            ;;
        --debug)
            m_debug="debug"
            ;;
        --universal=*)
            if test "x$optarg" = "xyes" ; then
                m_univ="univ"
            else
                m_univ=""
            fi
            ;;
        --universal)
            m_univ="univ"
            ;;
        --host=*)
            m_host="-$optarg"
            ;;
        *)
            args="$args $i"
            ;;
    esac
done

args="--prefix=$prefix --exec-prefix=$exec_prefix $args"

script=`find_wx_config_script`;
if test "x$script" != "x" ; then
    $script $args
    exit $?
else
    exit 1
fi