#!/bin/sh

prefix="/usr/local"
exec_prefix="${prefix}"
libdir="${exec_prefix}/lib"

TEMPLATE_DIR="/usr/local/share/libfwbuilder"

# libfwbuilder neeeds pthread, xml2, xslt
PTHREAD_CFLAGS="-pthread"
PTHREAD_LIBS="-pthread"

RANLIB="@RANLIB@"

VERSION="4.1.2"
XML_VERSION="17"

XML_CFLAGS="-I/usr/local/include/libxml2 -I/usr/local/include"
XML_LIBS="-L/usr/local/lib -lxml2 -lz -L/usr/local/lib -liconv -lm"

XSLT_CFLAGS=""
XSLT_LIBS=" -lxslt"

LIBSNMP_LIBS="-L/usr/local/lib -lnetsnmp -lcrypto"
LIB_RESOLV=""

LIBS="-lz -L/usr/local/lib -lnetsnmp -lcrypto -L/usr/local/lib "
CPPFLAGS="-I/usr/local/include -O2 -pipe"

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

Options:

  --libs		print library linking information
  --staticlibs          print static linking information
  --includepath		print path to additional include directories without "-I"
  --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
  --xml_version		output data format version information

Libraries:

    fwbuilder
    fwcompiler

EOF

    exit $1
}

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

cflags=false
includepath=false
libs=false
static=false


LIBFWBUILDER_LIBDIR="-L${libdir}"


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
	;;

    --templatedir)
	echo ${TEMPLATE_DIR}
	;;

    --libpath)
	echo ${libdir}
	;;

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

    --xml_version)
	echo ${XML_VERSION}
	exit 0
	;;

    --help)
	usage 0
	;;

    --cflags)
       	cflags=true
       	;;

    --includepath)
       	includepath=true
       	;;

    --libs)
       	libs=true
       	;;

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

    fwbuilder)
        lib_fwbuilder=yes
        ;;

    fwcompiler)
        lib_fwcompiler=yes
        ;;

    *)
	usage
	exit 1
	;;
    esac
    shift
done

TP=`echo $prefix | sed 's/\///g'`
if test "$TP" != "usr" ; then
  includedir="-I${prefix}/include/"
fi

includedir="${includedir} -I${prefix}/include/fwb-4"


the_libs="$the_libs ${LIBFWBUILDER_LIBDIR}  ${LIBS} ${PTHREAD_LIBS} ${XSLT_LIBS} ${XML_LIBS} ${LIBSNMP_LIBS} ${LIB_RESOLV}"
the_flags="$the_flags ${includedir} ${CPPFLAGS} ${PTHREAD_CFLAGS} ${XML_CFLAGS} ${XSLT_CFLAGS} "

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

if $includepath; then
    set -- $the_flags
    while test -n "$1"; do
        if echo "$1" | grep -E -- "^-I" >/dev/null 2>&1; then
            I=`echo "$1" | sed 's/-I//'`
            INCLUDEPATH="$INCLUDEPATH $I"
        fi
        shift
    done
    all_flags="$INCLUDEPATH"
fi

if $libs; then
    all_flags="$all_flags $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

    fwb_libs="$libdir/libfwbuilder.a"

    if test "$lib_fwcompiler" = "yes"; then
      fwb_libs="$libdir/libfwcompiler.a ${fwb_libs}"
    fi

    ord_libs="${fwb_libs}  $ord_libs"

  else

    fwb_libs="-lfwbuilder"

    if test "$lib_fwcompiler" = "yes"; then
      fwb_libs="-lfwcompiler ${fwb_libs}"
    fi

    ord_libs="${fwb_libs}  $ord_libs"

  fi
fi

echo $other_flags $ord_libs $stat_libs


exit 0
