#!/bin/bash
#
# fastjet-config.  Generated from fastjet-config.in by configure.
#
# This is the base script for retrieving all information
# regarding compiling and linking programs using FastJet.
# Run ./fastjet-config without arguments for usage details
#########################################################

# the list of plugins is dynamic so we need the following
# line to deal with the static lib link
installationdir=/usr
prefix=/usr
exec_prefix=${prefix}

# print a usage message and exit
# exit code passed as argument:
#   0 if it is a normal call
#   1 if it is due to a misusage.
usage()
{
if [ "xyes" == "xyes" ] ; then
  cat 1>&2 <<EOF

This is FastJet 3.0.6 configuration tool.
Usage:
  fastjet-config [--help] [--version] [--prefix] [--cxxflags] [--libs]
      [--shared[=yes|no]] [--plugins[=yes|no]] [--rpath[=yes|no]] [--runpath]
      [--list-plugins] [--config]

The arguments can be either queries (one must be present):

  --help       prints this message and exits
  --version    prints FastJet version and exits
  --prefix     gets the FastJet installation directory
  --cxxflags   returns the compilation flags to be used with C++ programs
  --libs       returns the flags to pass to the linker

or flags (optional):

  --shared     controls whether you want to use the static or shared lib
               (default=yes)

  --plugins    controls whether you also want to link the FastJet plugins
               (default=no)

  --rpath      adds a -rpath argument at link-time that points to the
               directory where FastJet libraries are installed. This
               avoid having to set LD_LIBRARY_PATH at runtime when
               using shared libs in a non standard location (but may
               cause the program to inadvertently pick up other shared
               libraries that happen to be in the FastJet installation
               directory). 
               (default=yes)

  --runpath    at link-time, adds info about the directory where FastJet
               libraries are installed to the runpath (ELF systens
               only). This avoids having to set LD_LIBRARY_PATH at
               runtime when using shared libs in a non standard
               location but gives priority to an existing LD_LIBRARY_PATH.

  --list-plugins  list all the available plugins

  --config     shows a summary of how FastJet was configured

EOF
else
  cat 1>&2 <<EOF

This is FastJet 3.0.6 configuration tool.
Usage:
  fastjet-config [--help] [--version] [--prefix] [--cxxflags] [--libs]
      [--plugins[=yes|no]] [--list-plugins] [--config]

The arguments can be either queries (one must be present):

  --help       prints this message and exits
  --version    prints FastJet version and exits
  --prefix     get the FastJet installation directory
  --cxxflags   returns the compilation flags to be used with C++ programs
  --libs       returns the flags to pass to the linker

or flags (optional):

  --plugins    controls whether you also want to link the FastJet plugins
               (default=no)

  --list-plugins  list all the available plugins

  --config     shows a summary of how FastJet was configured

EOF
fi
  exit $1
}


# first deal with the case where no argument is passed
[ $# -gt 0 ] || usage 1


# tools to parse options
########################

# option_name _string
# Returns NAME if _string is of the form: --NAME[=...]
option_name()
{
    echo "$1" | sed 's/^--//;s/=.*//' | tr '-' '_'
}

# option_value _string
# Returns FOO if _string is of the form: --option=FOO
option_value()
{
    echo "$1" | sed 's/^[^=]*=//'
}

# is_in_list _arg _arg_list
# return true if the argument _arg is in the list _arg_list
# and false otherwise
is_in_list()
{
    arg_match="$1"
    shift
    for arg_match_i do
        [ "x$arg_match_i" != "x$arg_match" ] || return 0
    done
    false
}


# useful utilities
##################

# wite error messages and exit
write_error()
{
    echo "Error: $1"
    echo "Use fastjet-config --help for more information"
    exit 1
}


# browse the argument list
# This is done the following way:
#  - at first pass, we check if the --help argument is set. If yes, 
#    print usage and exit.
#    we also and make sure that there is no interference between the
#    arguments (e.g. --cflags --libs is wrong)
#  - we then check for extra arguments and return the requested info
#####################################################################
# useful lists of arguments
arg_query_list="version prefix list_plugins config help" # cxxflags libs
arg_yesno_list="shared plugins rpath"

# default behaviour for parameters
plugins_included="no"
use_shared="yes"
add_rpath="no"
add_runpath="no"

# no query found initially
found_query="no"
found_flags="no"
found_libs="no"

# browse arguments
for arg do
    case "$arg" in
	--help|-h)
	    usage 0
	    ;;
	--*=*)
	    arg_name=`option_name $arg`
	    arg_value=`option_value $arg`
	    # check the validity of the parmeter value
	    if ! is_in_list $arg_value yes no ; then
		write_error "$arg: parameter value must be yes or no"
	    fi
            # set the parameter value
	    case $arg_name in
		plugins)
		    plugins_included="$arg_value"
		    ;;
		shared)
		    use_shared="$arg_value"
		    if [ "x$arg_value" == "xno" ] ; then
			add_rpath="no"
		    fi
		    ;;
		rpath)
		    if test "xyes" = "xyes" ; then
			add_rpath="$arg_value"
		    else
			write_error "--rpath is only available together with shared libraries"
		    fi
		    ;;
		*)
		    write_error "$arg: unrecognised argument"
		    ;;
	    esac
	    ;;
	--cxxflags)
	    # we've found a query, make sure we don't already have one
	    # except if it is --libs
	    if [[ "x$found_query" != "xno" && "x$found_query" != "xlibs" ]]; then
		write_error "--cxxflags cannot be used with --$found_query"
	    fi

	    # update found_query 
	    # note: for the "big case" later, don't overwrite it if libs are already asked for
	    found_flags="yes"
	    if [ "x$found_query" != "xlibs" ]; then
		found_query="cxxflags"
	    fi	    
	    ;;
	--libs)
	    # we've found a query, make sure we don't already have one
	    # except if it is --cxxflags
	    if [[ "x$found_query" != "xno" && "x$found_query" != "xcxxflags" ]]; then
		write_error "--libs cannot be used with --$found_query"
	    fi

	    # update found_query 
	    found_libs="yes"
	    found_query="libs"
	    ;;
	--*)
	    arg_name=`option_name $arg`
	    if is_in_list $arg_name $arg_query_list ; then
		# we've found a query, make sure we don't already have one
		if [ "x$found_flags" != "xno" ] ; then
		    write_error "--$arg_name cannot be used with --cxxflags"
		fi
		if [ "x$found_libs" != "xno" ] ; then
		    write_error "--$arg_name cannot be used with --libs"
		fi
		if [ "x$found_query" != "xno" ] ; then
		    write_error "You can only make one query at a time"
		fi
		found_query="$arg_name"
	    else
		if is_in_list $arg_name $arg_yesno_list ; then
		    # we've found a parameter, set it to "yes"
		    case $arg_name in
			plugins)
			    plugins_included="yes"
			    ;;
			shared)
			    use_shared="yes"
			    ;;
			rpath)
			    if test "xyes" = "xyes" ; then
				add_rpath="yes"
			    else
				write_error "--rpath is only available together with shared libraries"
			    fi
			    ;;
			*)
			    write_error "$arg: unrecognised argument"
			    ;;
		    esac
		else
		    case $arg_name in
			runpath)
			    if test "xyes" = "xyes" ; then
				if test "xyes" = "xyes" ; then
				    add_runpath="yes"
				    add_rpath="no"
				else
				    write_error "--runpath is not available on this platform"
				fi
			    else
				write_error "--runpath is only available together with shared libraries"
			    fi
			    ;;
			*)
			    write_error "$arg: unrecognised argument"
			    ;;
		    esac
		fi
	    fi
	    ;;
	*)
	    write_error "$arg is not a valid argument"
	    ;;
    esac
done


# now deal with the output
case $found_query in
    no)
	write_error "you must at least specify one query in '$arg_query_list'"
	;;
    version)
	echo 3.0.6
	;;
    prefix)
	echo /usr
	;;
    cxxflags)
	echo -I${prefix}/include 
	;;
    libs)
	libs_string="  -lm "
#       since we use the system default (use shared lib if available, static 
#       otherwise), we only need to worry if shared available and static
#       explicitely asked
	if test "x$use_shared" = "xno" && test "xyes" = "xyes" ; then
	    libs_string=$libs_string" ${prefix}/lib/x86_64-linux-gnu/libfastjettools.a ${prefix}/lib/x86_64-linux-gnu/libfastjet.a"
	else
	    libs_string=$libs_string" -L${prefix}/lib/x86_64-linux-gnu -lfastjettools -lfastjet"
	fi
	if test "x$add_rpath" = "xyes" ; then
	    libs_string="-Wl,-rpath,${prefix}/lib/x86_64-linux-gnu "$libs_string
        else
            # GPS 2009-05-29: remove any left over -Wl, e.g. related to CGAL.
            libs_string=`echo $libs_string | sed 's/-Wl,-rpath[^ ]*//'`
	fi
	if test "x$add_runpath" = "xyes" ; then
	    libs_string="-Wl,--enable-new-dtags -Wl,-rpath,${prefix}/lib/x86_64-linux-gnu "$libs_string
	fi
	if test "x$plugins_included" = "xyes" ; then
	    if test "x$use_shared" = "xno" && test "xyes" = "xyes" ; then
		libs_string=$libs_string"  ${installationdir}/lib/libfastjetplugins.a    -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/9/../../.. -lgfortran -lm -lquadmath"
	    else
		libs_string=$libs_string"  -lfastjetplugins    -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/9/../../.. -lgfortran -lm -lquadmath"
	    fi

	fi
	if [ "x$found_flags" = "xyes" ] ; then
	    echo -I${prefix}/include  $libs_string
	else
	    echo $libs_string
	fi
	;;
    list_plugins)
	echo "Available plugins: "
	echo -n "  "
	echo  SISCone PxCone D0RunIICone NestedDefs TrackJet CMSIterativeCone EECambridge Jade D0RunICone GridJet | sed -e "s/ /\\`printf '\n\r  '`/g"
	;;
    config)
	echo "This is FastJet version 3.0.6"
	echo ""
	echo "Configuration invocation was"
	echo ""
	echo "  ./configure  '--build=x86_64-linux-gnu' '--prefix=/usr' '--includedir=${prefix}/include' '--mandir=${prefix}/share/man' '--infodir=${prefix}/share/info' '--sysconfdir=/etc' '--localstatedir=/var' '--disable-silent-rules' '--libdir=${prefix}/lib/x86_64-linux-gnu' '--libexecdir=${prefix}/lib/x86_64-linux-gnu' '--disable-maintainer-mode' '--disable-dependency-tracking' '--enable-allplugins' 'build_alias=x86_64-linux-gnu' 'CFLAGS=-g -O2 -fdebug-prefix-map=/build/fastjet-I8XUpC/fastjet-3.0.6+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security' 'LDFLAGS=-Wl,-Bsymbolic-functions -Wl,-z,relro' 'CPPFLAGS=-Wdate-time -D_FORTIFY_SOURCE=2' 'CXXFLAGS=-g -O2 -fdebug-prefix-map=/build/fastjet-I8XUpC/fastjet-3.0.6+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security' 'FCFLAGS=-g -O2 -fdebug-prefix-map=/build/fastjet-I8XUpC/fastjet-3.0.6+dfsg=. -fstack-protector-strong'"
	echo ""
	printf "Configuration summary:\n----------------------\n  Installation directory     /usr\n  Shared libraries           yes\n  Static libraries           yes\n  Debug flag                 yes\n  CGAL support               no\n  Plugins: EECambridge       yes\n           Jade              yes\n           NestedDefs        yes\n           SISCone           yes\n           CDFCones          \n           D0RunICone        yes\n           D0RunIICone       yes\n           ATLASCone         \n           CMSIterativeCone  yes\n           PxCone            yes\n           TrackJet          yes\n           GridJet           yes\n  Monolithic plugins lib     yes\n"
	;;
esac
