#! /bin/sh
# 
# efltk-config
#
# original version by James Dean Palmer, adapted by Vincent Penne
#
# "$Id: efltk-config.in 969 2003-03-13 11:29:55Z laza2000 $"
#

MAJOR_VERSION=@FL_MAJOR_VERSION@
MINOR_VERSION=@FL_MINOR_VERSION@
PATCH_VERSION=@FL_PATCH_VERSION@
VERSION="$MAJOR_VERSION.$MINOR_VERSION.$PATCH_VERSION"

### BEGIN efltk-config

# Calculate the prefix from the location of *this* file
prefix=`echo "$0" | sed 's/\/bin\/efltk-config//
s/\/efltk-config//'`

#prefix=@prefix@
exec_prefix=@exec_prefix@
exec_prefix_set=no
bindir=@bindir@
includedir=@includedir@
libdir=@libdir@
srcdir=@srcdir@

# compiler names
CXX="@CXX@"
CC="@CC@"

# flags for C++ compiler:
CFLAGS="@CFLAGS@"
CXXFLAGS="@CXXFLAGS@"

# program to make the archive:
LIBNAME="@LIBNAME@"
LIBCOMMAND="@LIBCOMMAND@"
RANLIB="@RANLIB@"
DSOLIBNAME="@DSOLIBNAME@"
DSOCOMMAND="@DSOCOMMAND@"

# flags for the linker
LD_PLUGIN_FLAGS="@LD_PLUGIN_FLAGS@"

# libraries to link with:
IMAGELIBS="@LIBPNG@ @LIBJPEG@"

# programs to make archive and build DSOs
RANLIB="@RANLIB@"
DSOCOMMAND="@DSOCOMMAND@"

usage ()
{
    echo "Usage: efltk-config [OPTIONS]
Options:
	[--prefix[=DIR]]  return/set where FLTK is installed
	[--exec-prefix[=DIR]]
	[--version]

options telling what we are doing :
	[--use-gl]        use GL
	[--use-xml]       use XML extension library
	[--use-odbc]      use ODBC database driver
	[--use-mysql]     use MySQL database driver
	[--use-images]    use extra image formats (PNG, Jpeg)
        [--multithread]   build a multithreaded program
	[--build-plugin]  build a plugin

options telling what information we request
	[--cflags]        return flags to compile C using FLTK
	[--cxxflags]      return flags to compile C++ using FLTK
	[--ldflags]       return flags to link against FLTK
	[--ldstaticflags] return flags to link against static FLTK library
                                          even if there are DSOs installed
	[--libs]          return FLTK libraries full path for dependencies

options compiling and linking an application:
	[-g]              compile the program with debugging information
	[--compile program.cpp]
        [--output program] if output is not set, basename is used as output

"
    exit $1
}

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

no_plugins=no
compile=
debug=
output=

# Parse command line options
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
      if test $exec_prefix_set = no ; then
        exec_prefix=$optarg
      fi
      ;;
    --prefix)
      echo_prefix=yes
      ;;
    --exec-prefix=*)
      exec_prefix=$optarg
      exec_prefix_set=yes
      ;;
    --exec-prefix)
      echo_exec_prefix=yes
      ;;
    --version)
      echo $VERSION
      ;;
    --use-gl)
      use_gl=yes
      ;;
    --use-images)
      use_images=yes
      ;;
    --use-xml)
      use_xml=yes
      ;;
    --use-odbc)
      use_odbc=yes
      ;;
    --use-mysql)
      use_mysql=yes
      ;;
    --multithread)
      use_threads=yes
      ;;
    --build-plugin)
      build_plugin=yes
      ;;
    --cflags)
      echo_cflags=yes
      ;;
    --cxxflags)
      echo_cxxflags=yes
      ;;
    --ldflags)
      echo_ldflags=yes
      ;;
    --ldstaticflags)
      echo_ldstaticflags=yes
      ;;
    --libs)
      echo_libs=yes
      ;;
    --compile)
      compile=$2
      shift
      ;;
    -g)
      debug=-g3
      ;;
    -o)
      output=$2
      shift
      ;;
    *)
      echo_help=yes
      ;;
  esac
  shift
done

# Calculate needed libraries
LDLIBS=""
LDSTATIC=""
LIBS=""

if test "$use_gl" = "yes"; then
    LDLIBS="$LDLIBS -lefltk_gl @GLLIB@"
    LDSTATIC="$LDSTATIC $prefix/lib/libefltk_gl.a @GLLIB@"
    LIBS="$LIBS $prefix/lib/libefltk_gl.a"
fi

if test "$use_images" = "yes"; then
    LDLIBS="$LDLIBS -lefltk_images $IMAGELIBS"
    LDSTATIC="$LDSTATIC $prefix/lib/libefltk_images.a $IMAGELIBS"
    LIBS="$LIBS $prefix/lib/libefltk_images.a"
fi

if test "$use_xml" = "yes"; then
    LDLIBS="$LDLIBS -lefltk_xml"
    LDSTATIC="$LDSTATIC $prefix/lib/libefltk_xml.a"
    LIBS="$LIBS $prefix/lib/libefltk_xml.a"
fi

if test "$use_odbc" = "yes"; then
    LDLIBS="$LDLIBS -lefltk_odbc @UNIXODBCLIB@"
    LDSTATIC="$LDSTATIC $prefix/lib/libefltk_odbc.a @UNIXODBCLIB@"
    LIBS="$LIBS $prefix/lib/libefltk_odbc.a"
fi

if test "$use_mysql" = "yes"; then
    LDLIBS="$LDLIBS -lefltk_mysql @MYSQLLIBS@"
    LDSTATIC="$LDSTATIC $prefix/lib/libefltk_mysql.a @MYSQLLIBS@"
    LIBS="$LIBS $prefix/lib/libefltk_mysql.a"
    CXXFLAGS="$CXXFLAGS @MYSQLFLAGS@"
fi

if test "$build_plugin" = "yes"; then
    LDLIBS="$LDLIBS $LD_PLUGIN_FLAGS"
    LDSTATIC="$LDSTATIC $LD_PLUGIN_FLAGS"
fi

LDLIBS="$LDLIBS -lefltk @LDFLAGS@ @LDLIBS@"
LDSTATIC="$LDSTATIC $prefix/lib/libefltk.a @LDFLAGS@ @LDLIBS@"
LIBS="$LIBS $prefix/lib/libefltk.a"

if test "$use_threads" = "yes"; then
    LDLIBS="$LDLIBS @THREADSLIBS@"
    LDSTATIC="$LDSTATIC @THREADSLIBS@"
fi

# Answer to user requests
if test -n "$echo_help"; then usage 1
fi

if test -n "$compile"; then

    if test -n "$output"; then
	prog=$output
    else
    case $compile in
        *.cxx)
            prog=`basename $compile .cxx`
	    ;;
        *.cpp)
            prog=`basename $compile .cpp`
	    ;;
        *.cc)
            prog=`basename $compile .cc`
	    ;;
        *.C)
            prog=`basename $compile .C`
	    ;;
	*)
	    echo "ERROR: Unknown/bad C++ source file extension on \"$compile\"!"
	    exit 1
	    ;;
    esac
    fi
	    
    post=$prog

    echo $CXX $CXXFLAGS $debug -o $prog $compile $LDLIBS
    $CXX $CXXFLAGS $debug -o $prog $compile $LDLIBS
fi

if test "$echo_prefix" = "yes"; then
        echo $prefix
fi

if test "$echo_exec_prefix" = "yes"; then
        echo $exec_prefix
fi

if test "$echo_cflags" = "yes"; then
    if test "$includedir" = "/usr/include"; then
	includes=
    else
      includes=-I`echo "$0" | sed 's/\/bin\/efltk-config/\/include/
s/\/efltk-config//'`
    fi
      echo $includes $CFLAGS
fi

if test "$echo_cxxflags" = "yes"; then
    if test "$includedir" = "/usr/include"; then
	includes=
    else
      includes=-I`echo "$0" | sed 's/\/bin\/efltk-config/\/include/
s/\/efltk-config//'`
    fi
      echo $includes $CXXFLAGS
fi

if test "$echo_ldflags" = "yes"; then
      my_libs=
      libdirs=-L${exec_prefix}/lib
      for i in $LDLIBS ; do
        if test $i != -L${exec_prefix}/lib ; then
          if test -z "$my_libs" ; then
            my_libs="$i"
          else
            my_libs="$my_libs $i"
          fi
        fi
      done
      echo $libdirs $my_libs
fi

if test "$echo_ldstaticflags" = "yes"; then
      echo $LDSTATIC
fi

if test "$echo_libs" = "yes"; then
      echo $LIBS
fi

# END of efltk-config
