#!/bin/sh
# adapted from `gtk-config'

prefix=/usr/local
exec_prefix=${prefix}

usage()
{
	cat <<EOF
Usage: c2hs-config [OPTIONS] [LIBRARIES]
Options:
	[--prefix]
	[--version]
	[--libs]
	[--cflags]
	[--c2hs]
	[--package-conf]
Libraries:
EOF
	exit $1
}

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

# default library and default system
#
lib_c2hs=yes
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.12.0 [compiled for GHC 6.2.1]
      exit 0
      ;;
    --cflags)
      echo_cflags=yes
      ;;
    --libs)
      echo_libs=yes
      ;;
    --c2hs)
      echo_c2hs=yes
      ;;
    --package-conf)
      echo_package_conf=yes
      ;;
    *)
      usage 1 1>&2
      ;;
  esac
  shift
done

libdir=${exec_prefix}/lib/c2hs-0.12.0
bindir=${exec_prefix}/bin
has_pkg=yes
pkg_conf=$libdir/$sys/c2hs.conf
pkg_added=

if test "$echo_prefix" = "yes"; then
  echo $prefix
fi
if test x$has_pkg = xyes; then
  if test "$echo_cflags" = "yes" -o "$echo_libs" = "yes"; then
    if test "$pkg_added" != "yes"; then
      echo -n ""
    fi
    echo "-package c2hs"
  fi
else
  if test "$echo_cflags" = "yes"; then
    import_dir=`grep "import_dirs" $pkg_conf | sed -e 's/^.*\["\(.*\)"\].*/\1/'`
    cflags="-ffi"
    echo -i$import_dir $cflags
  fi
  if test "$echo_libs" = "yes"; then
    lib_dir=`grep "library_dirs" $pkg_conf | sed -e 's/^.*\["\(.*\)"\].*/\1/'`
    libsp=""
    libsa=""
    if test "$lib_c2hs" = "yes"; then
      libsp="$libsp -lc2hs"
    fi
    echo -L$lib_dir $libsa $libsp
  fi
fi
if test "$echo_c2hs" = "yes"; then
  echo $bindir/c2hs
fi
if test "$echo_package_conf" = "yes"; then
  head -15 $pkg_conf | tail -14
fi
