#!/bin/sh
#####
# NAME
#	coconut-config
#
# DESCRIPTION
#	This script prints the information about coconut library.
# 
# USAGE
#	coconut-config [options]
#
# NOTE
#	This script is written based on `gtk-config' distribute with
#	gtk+-1.2.9 package.
#

prefix=/usr/pkg
exec_prefix=${prefix}
exec_prefix_set=no

objc_cflags=""
objc_libs="-dl -lobjc -lpthread"

usage()
{
	cat <<EOF
Usage: coconut-config [OPTIONS] [LIBRARIES]
Options:
	[--prefix[=DIR]]
	[--exec-prefix[=DIR]]
	[--version]
	[--libs]
	[--cflags]
	[--with-gtk]
Libraries:
	coconut
EOF
	exit $1
}

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

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 0.3.0
      ;;
    --cflags)
      echo_cflags=yes
      ;;
    --libs)
      echo_libs=yes
      ;;
    --with-gtk)
      with_gtk=yes
      ;;
    *)
      usage 1 1>&2
      ;;
  esac
  shift
done

# for gtk-2.0
if test "$with_gtk" = "yes" ; then
	gtk_cflags=`pkg-config --cflags gtk+-2.0`
	gtk_libs=`pkg-config --libs gtk+-2.0`
else
	gtk_cflags="" ;
	gtk_libs="" ;
fi

if test -z "$gtk_cflags" ; then
	glib_cflags=`pkg-config --cflags glib-2.0`
	coconut_gtk_lib=""
else
	glib_cflags="" 
	coconut_gtk_lib="-lcoconut_gtk"
fi

if test -z "$gtk_libs" ; then
	glib_libs=`pkg-config --libs glib-2.0`
else
	glib_libs=""
fi

xml_cflags=`xml2-config --cflags`
xml_libs=`xml2-config --libs`

popt_cflags="-I/usr/pkg/include"
popt_libs="-lpopt"

added_cflags="$objc_cflags $gtk_cflags $glib_cflags $xml_cflags $popt_cflags"
added_libs="$objc_libs $gtk_libs $glib_libs $xml_libs $popt_libs"

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
      echo -I${prefix}/include $added_cflags 
fi

if test "$echo_libs" = "yes"; then
      my_coconut_libs=
      libdirs=-L${exec_prefix}/lib
      for i in $added_libs ; do
        if test $i != -L${exec_prefix}/lib ; then
          if test -z "$my_coconut_libs" ; then
            my_coconut_libs="$i"
          else
            my_coconut_libs="$my_coconut_libs $i"
          fi
        fi
      done

      echo $libdirs $coconut_gtk_lib -lcoconut $my_coconut_libs 
fi      

exit 0 ;

