#!/bin/bash
# StarPU --- Runtime system for heterogeneous multicore architectures.
#
# Copyright (C) 2010-2022  Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
#
# StarPU is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or (at
# your option) any later version.
#
# StarPU is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See the GNU Lesser General Public License in COPYING.LGPL for more details.
#
PROGNAME=$0

set -e

usage()
{
    echo "Show the configuration used by StarPU."
    echo ""
    echo "Usage: $PROGNAME <parameter name>"
    echo ""
    echo ""
    echo "  The starpu_config utility shows all the configuration parameters used when installing StarPU"
    echo ""
    echo "Options:"
    echo "	-h, --help          display this help and exit"
    echo "	-v, --version       output version information and exit"
    echo "	-d                  only shows define parameters"
    echo "	-u                  only shows undefined parameters"
    echo ""
    echo "	if parameters are given, only configuration parameters with the given name are displayed"
    echo ""
    echo "Report bugs to <starpu-devel@inria.fr>"
    exit 0
}

if [ "$1" = "-v" ] || [ "$1" = "--version" ] ; then
    echo "$PROGNAME (StarPU) 1.4.7"
    exit 0
fi

if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
    usage
fi

prefix=$(realpath /opt/local)
if test -d $prefix
then
    starpu_datarootdir=$(realpath ${prefix}/share)
    config_file="$starpu_datarootdir/starpu/starpu_config.cfg"
    if test ! -f "$config_file"
    then
	config_file=$(realpath $(dirname $0))/starpu_config.cfg
    fi
else
    config_file=$(realpath $(dirname $0))/starpu_config.cfg
fi
if test ! -f "$config_file"
then
    echo "Configuration file unavailable"
    exit 1
fi

echo "processing $config_file"

if test "$1" == "-d"
then
    grep 'define' $config_file
elif test "$1" == "-u"
then
     grep 'undef' $config_file
elif test "$1"
then
    for x in $*
    do
	grep $x $config_file
    done
else
    sort $config_file
fi

