#!/bin/sh
###############################################################################
# This file is part of "Patrick's Programming Library", Version 7 (PPL7).
# Web: http://www.pfp.de/ppl/
#
# $Author$
# $Revision$
# $Date$
# $Id$
#
###############################################################################
#
# Copyright (c) 2013, Patrick Fedick <patrick@pfp.de>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#   1. Redistributions of source code must retain the above copyright notice,
#      this list of conditions and the following disclaimer. 
#   2. Redistributions in binary form must reproduce the above copyright notice,
#      this list of conditions and the following disclaimer in the documentation
#      and/or other materials provided with the distribution. 
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
###############################################################################

DEFS="@DEFS@"
CFLAGS="@CFLAGS@ @PTHREAD_CFLAGS@"
CPPFLAGS="@CPPFLAGS@"
LDFLAGS="@LDFLAGS@"

LIBS_CORE="@LIBS@ @PTHREAD_CFLAGS@ @PTHREAD_LIBS@ @ZLIB_LIBS@ @BZ2_LIBS@ @PCRE_LIBS@ @ICONV_LIBS@"
LIBS_CRYPTO="@LIBMCRYPT_LIBS@"
LIBS_GFX="@JPEG_LIBS@ @PNG_LIBS@ @TIFF_LIBS@ @SDL2_LIBS@ @FT2_LIBS@ @IMLIB_LIBS@"
LIBS_INET="@LIBCURL@ @LIBLDNS_LIBS@ @LIBMICROHTTPD_LIBS@ @LIBIDN_LIBS@ @OPENSSL_LDFLAGS@ @OPENSSL_LIBS@ @WINDOWS_WINSOCK_LIBS@"
LIBS_AUDIO="@LIBCDIO_LDFLAGS@ @MPG123_LIBS@ @LAME_LIBS@"
LIBS_DB="@MYSQL_LDFLAGS@ @POSTGRESQL_LDFLAGS@ @SQLITE3_LDFLAGS@"
LIBS_TK=""

LIBNAME="libppl7"
LIBDEBUG="libppl7-debug"
VERSION="@PACKAGE_VERSION@"
SRCDIR=@SRCDIR@


prefix=@prefix@
exec_prefix=@exec_prefix@
TARGETLIB="@libdir@"
TARGETINCLUDE="@includedir@"


usage()
{
	cat <<EOF
Usage: ppl-config [OPTIONS] [LIBRARIES]
Options:
	[--prefix[=DIR]]
	[--exec-prefix[=DIR]]
	[--version]
	[--libs]
	[--cflags]
	[--ppllib]
	[--features [wanted features, eg: --features zlib bzip2 pcre]]
	[--archive]
Libraries:
	release | debug	
EOF
	exit $1
}

RET=0
check_feature()
{
	NAME=$1
	DEFINE=`echo "$2" | sed "s/,.*//"`
	WANTED=$3
	if [ -n "$WANTED" ] ; then
		for FEATURE in $WANTED; do
			if [ "$FEATURE" = "$NAME" ] ; then
				if [ "$DEFINE" = "yes" ] ; then
					echo "OK, have feature:       $NAME"
				else
					echo "ERROR, missing feature: $NAME"
					RET=1
				fi
			fi
		done
	else
		if [ "$DEFINE" = "yes" ] ; then
			echo $NAME
		fi
	fi
}


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

lib_ppl=yes
basename=libppl7
libname=""

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
      ;;
    --cflags)
      echo_cflags=yes
      ;;
    --libs)
      echo_libs=yes
      ;;
    --archive)
      echo_archive=yes
      ;;
    --ppllib)
      echo_ppllib=yes
      ;;
    release)
      lib_ppl=yes
      basename=libppl7
      libname=-lppl7
      objects=release
      ;;
    coverage)
      lib_ppl=yes
      basename=libppl7
      libname=-lppl7
      objects=coverage
      ;;    
    debug)
      lib_ppl_debug=yes
      basename=libppl7-debug
      libname=-lppl7-debug
      objects=debug
      ;;
    source)
      usesource=yes
      ;;
    --features)
      listfeatures=yes
      shift
      features_wanted=$*
      break
      ;;
    inet)
      libname="$libname -lppl7-inet"
      LIBS="$LIBS $LIBS_INET"
      ;;
    core)
      libname="$libname -lppl7-core"
      LIBS="$LIBS $LIBS_CORE"
      ;;
    crypto)
      libname="$libname -lppl7-crypto"
      LIBS="$LIBS $LIBS_CRYPTO"
      ;;
    gfx)
      libname="$libname -lppl7-gfx"
      LIBS="$LIBS $LIBS_GFX"
      ;;
    db)
      libname="$libname -lppl7-db"
      LIBS="$LIBS $LIBS_DB"
      ;;
    tk)
      libname="$libname -lppl7-tk"
      LIBS="$LIBS $LIBS_TK"
      ;;
    audio)
      libname="$libname -lppl7-audio"
      LIBS="$LIBS $LIBS_AUDIO"
      ;;
      
    *)
      usage 1 1>&2
      ;;
  esac
  shift
done

if test -z "$LIBS" ; then
   LIBS="$LIBS_INET $LIBS_CORE $LIBS_CRYPTO $LIBS_GFX $LIBS_DB $LIBS_AUDIO $LIBS_TK"
fi
LIBS="$LIBS -lstdc++"

if test -z "$libname" ; then
   libname="-lppl7"
fi

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

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

if test "$listfeatures" = "yes"; then
	RET=0
	check_feature "zlib" "@report_have_zlib@" "$features_wanted"
	check_feature "bzip2" "@report_have_bzip2@" "$features_wanted"
	check_feature "png" "@report_have_png@" "$features_wanted"
	check_feature "jpeg" "@report_have_jpeg@" "$features_wanted"
	check_feature "freetype2" "@report_have_freetype2@" "$features_wanted"
	check_feature "sdl2" "@report_have_sdl2@" "$features_wanted"
	check_feature "lame" "@report_have_lame@" "$features_wanted"
	check_feature "mad" "@report_have_libmad@" "$features_wanted"
	check_feature "curl" "@report_have_curl@" "$features_wanted"
	check_feature "openssl" "@report_have_openssl@" "$features_wanted"
	check_feature "pcre" "@report_have_pcre@" "$features_wanted"
	check_feature "iconv" "@report_have_iconv@" "$features_wanted"
	check_feature "mcrypt" "@report_have_libmcrypt@" "$features_wanted"
	check_feature "mysql" "@report_have_mysql@" "$features_wanted"
	check_feature "postgres" "@report_have_postgres@" "$features_wanted"
	check_feature "freetds" "@report_have_freetds@" "$features_wanted"
	check_feature "sybase" "@report_have_freetds@" "$features_wanted"
	check_feature "sqlite3" "@report_have_sqlite3@" "$features_wanted"
	check_feature "nasm" "@report_have_nasm@" "$features_wanted"
	check_feature "pthreads" "@report_have_pthreads@" "$features_wanted"
	check_feature "ldns" "@report_have_ldns@" "$features_wanted"
	check_feature "mpg123" "@report_have_mpg123@" "$features_wanted"
	check_feature "ogg" "@report_have_libogg@" "$features_wanted"
	check_feature "shout" "@report_have_libshout@" "$features_wanted"
	check_feature "imlib" "@report_have_libimlib@" "$features_wanted"
	check_feature "tiff" "@report_have_libtiff@" "$features_wanted"
	check_feature "libmicrohttpd" "@report_have_libmicrohttpd@" "$features_wanted"
	check_feature "idn" "@report_have_libidn@" "$features_wanted"
	check_feature "bind" "@report_have_libbind@" "$features_wanted"	
	check_feature "libcdio" "@report_have_libcdio@" "$features_wanted"
	exit $RET
fi

PPLLIB="$prefix/lib/$basename.a"
if test "$usesource" != "yes"; then
	ARCHIVE="$PPLLIB $LIBS"
	LIBS="-L$TARGETLIB $libname $LIBS"
fi


if test "$echo_cflags" = "yes"; then
  	src=""
	if test "$usesource" = "yes"; then
		src="-I$SRCDIR/include"
	fi
    echo "$src $CFLAGS -I$TARGETINCLUDE"
fi

if test "$echo_libs" = "yes"; then
      my_glib_libs=
      libdirs=
      for i in $LIBS ; do
          if test -z "$my_glib_libs" ; then
            my_glib_libs="$i"
          else
            my_glib_libs="$my_glib_libs $i"
          fi
      done
      	src=""
		if test "$usesource" = "yes"; then
			src="$SRCDIR/$objects/$basename.a"
		fi

      echo "@NOPIE@ $src $libdirs $my_glib_libs" 
fi      

if test "$echo_archive" = "yes"; then
      my_glib_libs=
      libdirs=
      for i in $ARCHIVE ; do
          if test -z "$my_glib_libs" ; then
            my_glib_libs="$i"
          else
            my_glib_libs="$my_glib_libs $i"
          fi
      done
      	src=""
		if test "$usesource" = "yes"; then
			src="$SRCDIR/$objects/$basename.a"
		fi

      echo "$src $libdirs $my_glib_libs"
fi      


if test "$echo_ppllib" = "yes"; then
  echo $PPLLIB
fi

