#!/bin/sh
#
# plplot-config -- Utility to help building projects against plplot
#
# Copyright (C) 2004  Maurice LeBrun
# Copyright (C) 2004, 2005  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


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

output_cflags () {
    OUT="$OUT -I/usr/local/include/plplot"
    if test "$with_tcl" = "yes" -a "$enable_tcl" = "yes" ; then
        OUT="$OUT -I/usr/local/include/tcl8.4 "
    fi
    if test "$with_gcw" = "yes" -a "$enable_gcw" = "yes" ; then
        OUT="$OUT "
    fi
}

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

output_libs () {
    # Set up library spec
    plplot_libspec="-L${prefix}/lib -lplplotd"
    LINKER=cc
    if test "$with_cxx" = "yes" -a "$enable_cxx" = "yes" ; then
	plplot_libspec="-L${prefix}/lib -lplplotcxxd"
	LINKER=c++
    fi
    if test "$with_f77" = "yes" -a "$enable_f77" = "yes" ; then
	plplot_libspec="-L${prefix}/lib -lplplotf77d"
	LINKER=f77
    fi
    if test "$with_f95" = "yes" -a "$enable_f95" = "yes" ; then
	plplot_libspec="-L${prefix}/lib -lplplotf95d"
	LINKER=f77
    fi
    if test "$with_tcl" = "yes" -a "$enable_tcl" = "yes" ; then
	plplot_libspec="-L${prefix}/lib -lplplottcltkd"
	LINKER=cc
    fi
    if test "$with_gcw" = "yes" -a "$enable_gcw" = "yes" ; then
	plplot_libspec="-L${prefix}/lib -lplplotgnome2d"
	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-f95]
        [--with-tcl]
        [--with-gcw]
	[--help]

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

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

prefix=/usr/local
version=5.6.1
enable_cxx=yes
enable_f77=yes
enable_f95=no
enable_tcl=yes
enable_gcw=no

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_f95" = "yes" -o "$with_tcl" = "yes" -o "$with_gcw" = "yes"; then
	      usage 1 1>&2
	    fi
	    with_cxx=yes
	    ;;
	--with-f77)
	    if test "$with_f95" = "yes" -o "$with_cxx" = "yes" -o "$with_tcl" = "yes" -o "$with_gcw" = "yes"; then
	      usage 1 1>&2
	    fi
	    with_f77=yes
	    ;;
	--with-f95)
	    if test "$with_f77" = "yes" -o "$with_cxx" = "yes" -o "$with_tcl" = "yes" -o "$with_gcw" = "yes"; then
	      usage 1 1>&2
	    fi
	    with_f95=yes
	    ;;
	--with-tcl)
	    if test "$with_f77" = "yes" -o "$with_f95" = "yes" -o "$with_cxx" = "yes" -o "$with_gcw" = "yes"; then
	      usage 1 1>&2
	    fi
	    with_tcl=yes
	    ;;
	--with-gcw)
	    if test "$with_f77" = "yes" -o "$with_f95" = "yes" -o "$with_cxx" = "yes" -o "$with_tcl" = "yes"; then
	      usage 1 1>&2
	    fi
	    with_gcw=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
