#!/bin/sh
# adapted from `gtk-config' by way of `c2hs-config'

prefix=/usr
exec_prefix=${prefix}

usage()
{
	cat <<EOF
Usage: gtk+hs-config [OPTIONS] [LIBRARIES]
Options:
	[--system=HS]
	[--prefix]
	[--version]
	[--libs]
	[--cflags]
	[--package-conf]
Libraries:
	gtkhs
EOF
	if test "" = "yes"; then
	  echo "	gtkglarea"
	fi
	if test "" = "yes"; then
	  echo "	gtkembedmoz"
	fi
	if test "" = "yes"; then
	  echo "	libglade"
	fi
	if test "" = "yes"; then
	  echo "	iH"
	fi
	exit $1
}

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

# default library and default system
#
lib_gtkhs=yes
lib_gtkglarea=no
lib_libglade=no
lib_iH=no
sys=ghc6

while test $# -gt 0; do
  case "$1" in
  -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) optarg= ;;
  esac

  case $1 in
    --prefix)
      echo_prefix=yes
      ;;
    --version)
      echo 0.15.2
      exit 0
      ;;
    --cflags)
      echo_cflags=yes
      ;;
    --libs)
      echo_libs=yes
      ;;
    --package-conf)
      echo_package_conf=yes
      ;;
    gtkhs)
      lib_gtkhs=yes
      ;;
    gtkglarea)
      if test x = xyes; then
        lib_gtkglarea=yes
      else
        usage 1 1>&2
      fi
      ;;
    libglade)
      if test x = xyes; then
        lib_libglade=yes
      else
        usage 1 1>&2
      fi
      ;;
    iH)
      if test x = xyes; then
        lib_iH=yes
      else
        usage 1 1>&2
      fi
      ;;
    *)
      usage 1 1>&2
      ;;
  esac
  shift
done

libdir=${exec_prefix}/lib/gtk+hs-0.15.2
bindir=${exec_prefix}/bin
has_pkg=yes
pkg_conf=$libdir/$sys/gtkhs.conf
pkg_opts="-package-conf /usr/lib/c2hs-0.11.5/ghc6/c2hs.conf -package c2hs"
pkg_added=

if test "$lib_gtkglarea" = "yes"; then
  gtkglarea_package="-package gtkglarea"
# FIXME:
#	gtkglarea_cflags="-i$libdir/$sys/import/gtkglarea"
#	gtkglarea_ldflags="-lgtkglHS"
fi

if test "$lib_libglade" = "yes"; then
# FIXME: doesn't set package correctly.
	libglade_cflags="-i$libdir/$sys/import/libglade"
	libglade_ldflags="-lgladeHS"
fi

if test "$lib_iH" = "yes"; then
  iH_package="-package iH"
# FIXME:
#	iH_cflags="-i$libdir/$sys/import/ih"
#	iH_ldflags="-liH"
fi

# FIXME: A lot of stuff below here is dependent on the formatting of $pkg_conf,
#	 which is very bad.  (And some stuff is still hardcoded.)

if test "$echo_prefix" = "yes"; then
	echo $prefix
fi
if test "$has_pkg" = "yes"; then
  if test "$echo_cflags" = "yes" -o "$echo_libs" = "yes"; then
    echo -n "$pkg_opts "
    if test "$pkg_added" != "yes"; then
      echo -n "-package-conf $pkg_conf "
    fi
    # FIXME: hack to make libglade work, since it's not properly packaged:
    if test "$echo_cflags" = "yes"; then
      echo -n "$libglade_cflags "
    fi
    if test "$echo_libs" = "yes"; then
      echo -n "$libglade_ldflags "
    fi
    echo "-package gtkhs"\
	 $gtkglarea_package $iH_package
  fi
else
  if test "$echo_cflags" = "yes"; then
    import_dirs=`grep -A 2 "import_dirs" $pkg_conf | sed -e 's/^.*"\(.*\)".*/\1/'`
    include_dirs=`grep -A 1 "include_dirs" $pkg_conf | sed -e 's/[^"]*"\([^"]*\)"[^"]*/\1 /g'`
    c_includes=`grep "c_includes" $pkg_conf | sed -e 's/[^"]*"\([^"]*\)"[^"]*/\1 /g'`
    cflags="-package c2hs -D_REENTRANT"
    for dir in $import_dirs; do
      echo -n "-i$dir "
    done
    for dir in $include_dirs; do
      echo -n "-I$dir "
    done
    for incl in $c_includes; do
      echo -n "-#include\"$incl\" "
    done
    echo $cflags $libglade_cflags
  fi
  if test "$echo_libs" = "yes"; then
    lib_dirs=`grep -A 1 "library_dirs" $pkg_conf | sed -e 's/[^"]*"\([^"]*\)"[^"]*/\1 /g'`
    extra_libraries=`grep "extra_libraries" $pkg_conf | sed -e 's/[^"]*"\([^"]*\)"[^"]*/\1 /g'`
    libsp=""
    libsa=""
    if test "$lib_gtkhs" = "yes"; then
      libsp="-lgtkHS -lgdkHS -lglibHS"
    fi
    for dir in $lib_dirs; do
      echo -n "-L$dir "
    done
    for lib in $extra_libraries; do
      echo -n "-l$lib "
    done
    echo $libsa $libsp -package c2hs $libglade_ldflags
  fi
fi
if test "$echo_package_conf" = "yes"; then
  head -19 $pkg_conf | tail -18
fi
