#!/bin/bash

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

libdir=/usr/lib64/ltilib
includedir=/usr/include/ltilib

# Check for LAPACK
lapack_libs=-llapack
if test -n "$lapack_libs" ; then
LIBS="-llapack -lopenblas -lf2c -lraw1394 -ldc1394 -lfftw3f -lpthread -lm -lz -ljpeg -lpng   -L/usr/lib64/gcc/x86_64-alt-linux/4.7.2 -L/usr/lib64/gcc/x86_64-alt-linux/4.7.2/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib64/gcc/x86_64-alt-linux/4.7.2/../../.. -lraw1394 -ldc1394 -lfftw3f -lpthread -lgfortran -lm -lz -ljpeg -lpng -lquadmath"
else
LIBS="-lraw1394 -ldc1394 -lfftw3f -lpthread -lm -lz -ljpeg -lpng " 
fi

usage()
{
	cat <<EOF
Usage: lti-config [OPTIONS] [LIBRARIES]
Options:
	[--prefix[=DIR]]
	[--exec-prefix[=DIR]]
	[--version]
	[--libs]
	[--cxxflags]
Libraries:
	nogtk
	debug
    profile
EOF
	exit $1
}

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

lib_gtk=yes
postfix=

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 2.0.0
      ;;
    --cxxflags)
      echo_cxxflags=yes
      ;;
    --libs)
      echo_libs=yes
      ;;
    nogtk)
      lib_gtk=no
      ;;
    debug)
      debug_libs=yes
      ;;
    profile)
      profile_libs=yes
      ;;
    *)
      usage 1 1>&2
      ;;
  esac
  shift
done

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

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

if test "$echo_cxxflags" = "yes"; then
      if test "$lib_gtk" = "yes"; then
        gtkcxxflags=`pkg-config --cflags gthread-2.0; pkg-config --cflags gtk+-2.0`
      else
        gtkcxxflags=-D_REENTRANT
      fi

      if test "$debug_libs" =  "yes"; then
        debugflags="-D_DEBUG"
      else
        debugflags="-DNDEBUG"
      fi

      echo "$gtkcxxflags -I${prefix}/include/ltilib -D_GNU_SOURCE -fpic $debugflags"
fi

if test "$echo_libs" = "yes"; then
      if test "$lib_gtk" = "yes"; then
        gtklibs=`pkg-config --libs gthread-2.0; pkg-config --libs gtk+-2.0`
        postfix=
      else
        gtklibs=-lpthread
	postfix=nv
      fi

      if test "$debug_libs" =  "yes"; then
        postfix=${postfix}d
      elif test "$profile_libs" =  "yes"; then
        postfix=${postfix}p
      else
        postfix=${postfix}r
      fi
      echo "-L${libdir} -llti${postfix} $LIBS $gtklibs"
fi      

