#! /bin/sh

prefix="/usr"

TEMPLATE_DIR="/usr/share/libfwbuilder"

# libfwbuilder neeed glib, pthread, sigc++, xml2, xslt
GLIB_CFLAGS="-I/usr/include/glib-1.2 -I/usr/lib/glib/include -D_REENTRANT"
GLIB_LIBS="-L/usr/lib -lgthread -lglib -lpthread"

# PTHREAD_LIB="@PTHREAD_LIB@"
RANLIB="ranlib"
VERSION="0.10.4"

SIGC_CFLAGS="-I/usr/lib/sigc++-1.0/include -I/usr/include/sigc++-1.0"
SIGC_LIBS="-lsigc -lpthread"

XML_CFLAGS="-I/usr/include/libxml2/libxml -I/usr/include/libxml2"
XML_LIBS="-lxml2"

XSLT_CFLAGS="-I/usr/include/libxml2/libxml -I/usr/include/libxml2"
XSLT_LIBS="-lxslt -lxml2 -lm"

LIBS=" -lsnmp /usr/lib/libresolv.a"
CPPFLAGS=" -I/usr/include"

usage()
{
    cat <<EOF
Usage: libfwbuilder-config [OPTION]...

Known values for OPTION are:

  --prefix=DIR		change LIBFWBUILDER prefix [default $prefix]
  --libs		print library linking information
  --staticlibs          print static linking information
  --cflags		print pre-processor and compiler flags
  --templatedir		print the path to template directory
  --libpath             print the path to the library directory
  --help		display this help and exit
  --version		output version information
EOF

    exit $1
}

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

cflags=false
libs=false
static=false

exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

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
	exec_prefix=${prefix}
	includedir=${prefix}/include
	libdir=${exec_prefix}/lib
	;;

    --prefix)
	echo $prefix
	;;

    --templatedir)
	echo ${TEMPLATE_DIR}
	;;

    --libpath)
	echo ${libdir}
	;;

    --version)
	echo ${VERSION}
	exit 0
	;;

    --help)
	usage 0
	;;

    --cflags)
       	cflags=true
       	;;

    --libs)
       	libs=true
       	;;

    --staticlibs)
        libs=true
	static=true
	;;

    *)
	usage
	exit 1
	;;
    esac
    shift
done


the_libs="$the_libs -L$libdir  ${LIBS} ${GLIB_LIBS} ${XSLT_LIBS} ${XML_LIBS} ${SIGC_LIBS}"
the_flags="$the_flags -I$includedir ${CPPFLAGS} ${GLIB_CFLAGS} ${XML_CFLAGS} ${XSLT_CFLAGS} ${SIGC_CFLAGS}"

if $cflags; then
    all_flags="$the_flags"
fi

if $libs; then
    all_flags="$all_flags $services $the_libs"
fi

if test -z "$all_flags" || test "x$all_flags" = "x "; then
    exit 1
fi


# Straight out any possible duplicates, but be careful to
# get `-lfoo -lbar -lbaz' for `-lfoo -lbaz -lbar -lbaz'
other_flags=
rev_libs=
stat_lib=
for i in $all_flags; do
    case "$i" in
    -L/usr/lib) ;;
    -I/usr/include) ;;
    *.a) stat_libs="$stat_libs $i" ;;
    -l*) rev_libs="$i $rev_libs" ;;
    *)
	case " $other_flags " in
	*\ $i\ *) ;;				# already there
	*) other_flags="$other_flags $i" ;;	# add it to output
	esac ;;
    esac
done

ord_libs=
for i in $rev_libs; do
    case " $ord_libs " in
    *\ $i\ *) ;;			# already there
    *) ord_libs="$i $ord_libs" ;;	# add it to output in reverse order
    esac
done

if $libs; then
  if $static; then
    ord_libs="$libdir/libfwbuilder.a $ord_libs"
  else
    ord_libs="-lfwbuilder $ord_libs"
  fi
fi

echo $other_flags $ord_libs $stat_libs


exit 0
