#!/bin/sh
#
# plplot-config -- Utility to help building projects against plplot
#
# Copyright (C) 2004  Maurice LeBrun
# Copyright (C) 2004  Alan W. Irwin
#
# This file is part of PLplot.
#
# PLplot is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PLplot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with PLplot; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA


#----------------------------------------------------------------------------
# Spits out cflags

output_cflags () {
    OUT="$OUT @INCLUDE_DIR_CMD@"
    if test "$with_tcl" = "yes" -a "$enable_tcl" = "yes" ; then
        OUT="$OUT @TCLINCCMD@ @TKINCCMD@"
    fi
}

#----------------------------------------------------------------------------
# Spits out libs

output_libs () {
    # Set up library spec
    plplot_libspec="-L${prefix}/lib -lplplot@LIB_TAG@"
    LINKER=@CC@
    if test "$with_cxx" = "yes" -a "$enable_cxx" = "yes" ; then
	plplot_libspec="-L${prefix}/lib -lplplotcxx@LIB_TAG@"
	LINKER=@CXX@
    fi
    if test "$with_f77" = "yes" -a "$enable_f77" = "yes" ; then
	plplot_libspec="-L${prefix}/lib -lplplotf77@LIB_TAG@"
	LINKER=@F77@
    fi
    if test "$with_tcl" = "yes" -a "$enable_tcl" = "yes" ; then
	plplot_libspec="-L${prefix}/lib -lplplottcltk@LIB_TAG@"
	LINKER=@CC@
    fi

    # Snarf libs from plplot_libtool output & process
    process_libtool_output \
     `plplot_libtool -n --mode=link ${LINKER} ${plplot_libspec} -o OUTPUT`
}

#----------------------------------------------------------------------------
# Throw away anything we don't want from plplot_libtool output

process_libtool_output () {
    for i in $*; do
	case $i in
	    -L*) emit_lib_output $*; return;;
	    * )  shift;;
	esac
    done
}

#----------------------------------------------------------------------------
# Output filtered linker args
# Filter out /usr/lib and all but one $prefix/lib.
# Todo: should filter out all duplicates.

emit_lib_output () {
    libs=""
    if test ${prefix}/lib != /usr/lib ; then
	libs=-L${prefix}/lib
    fi
    for i in $* ; do
	if test $i != -L${prefix}/lib ; then
	    libs="$libs $i"
	fi
    done
    OUT="$OUT $libs"
}

#----------------------------------------------------------------------------
# Spits out usage message & exits

usage () {
	cat <<EOF
Usage: plplot-config [OPTIONS]
Options:
	[--prefix[=DIR]]
	[--version]
	[--libs]
	[--cflags]
        [--with-c++]
        [--with-f77]
        [--with-tcl]
	[--help]

Note: --with-c++, --with-f77, and --with-tcl are mutually exclusive
EOF
	exit $1
}

#----------------------------------------------------------------------------
# Set up config/control variables

prefix=@prefix@
version=@VERSION@
enable_cxx=@enable_cxx@
enable_f77=@enable_f77@
enable_tcl=@enable_tcl@

if test $# -eq 0; then
    usage 1 1>&2
fi

# Loop over command line args
# Don't abort for unrecognized arg to allow for future extension!
while test $# -gt 0; do
    case "$1" in
	-*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
	*) optarg= ;;
    esac

    case $1 in
	--prefix=*)
	    prefix=$optarg
	    ;;
	--prefix)
	    echo $prefix
	    ;;
	--version)
	    echo $version
	    ;;
	--cflags)
	    echo_cflags=yes
	    ;;
	--libs)
	    echo_libs=yes
	    ;;
	--with-c++)
	    if test "$with_f77" = "yes" -o "$with_tcl" = "yes"; then
	      usage 1 1>&2
	    fi
	    with_cxx=yes
	    ;;
	--with-f77)
	    if test "$with_cxx" = "yes" -o "$with_tcl" = "yes"; then
	      usage 1 1>&2
	    fi
	    with_f77=yes
	    ;;
	--with-tcl)
	    if test "$with_f77" = "yes" -o "$with_cxx" = "yes"; then
	      usage 1 1>&2
	    fi
	    with_tcl=yes
	    ;;
	--help)
	    usage 1 1>&2
	    ;;
    esac
    shift
done

OUT=""
if test "$echo_cflags" = "yes"; then
    output_cflags
fi

if test "$echo_libs" = "yes"; then
    output_libs
fi
echo $OUT
