#!/bin/sh
#
# Dummy pkg-config for cross development.  We act a bridge to the XXXX-config
# scripts that are assumed to be in the same directory as ourselves.
#
# Help output from the original program.  We only implement the functions
# required to get autoconf and friends to work.
#
#Usage: pkg-config [OPTION...]
#  --version                                      output version of pkg-config
#  --modversion                                   output version for package
#  --atleast-pkgconfig-version=VERSION            require given version of
#                                                 pkg-config
#  --libs                                         output all linker flags
#  --static                                       output linker flags for
#                                                 static linking
#  --short-errors                                 print short errors
#  --libs-only-l                                  output -l flags
#  --libs-only-other                              output other libs (e.g.
#                                                 -pthread)
#  --libs-only-L                                  output -L flags
#  --cflags                                       output all pre-processor and
#                                                 compiler flags
#  --cflags-only-I                                output -I flags
#  --cflags-only-other                            output cflags not covered by
#                                                 the cflags-only-I option
#  --variable=VARIABLENAME                        get the value of a variable
#  --define-variable=VARIABLENAME=VARIABLEVALUE   set the value of a variable
#  --exists                                       return 0 if the module(s)
#                                                 exist
#  --uninstalled                                  return 0 if the uninstalled
#                                                 version of one or more
#                                                 module(s) or their
#                                                 dependencies will be used
#  --atleast-version=VERSION                      return 0 if the module is at
#                                                 least version VERSION
#  --exact-version=VERSION                        return 0 if the module is at
#                                                 exactly version VERSION
#  --max-version=VERSION                          return 0 if the module is at
#                                                 no newer than version VERSION
#  --list-all                                     list all known packages
#  --debug                                        show verbose debug information
#  --print-errors                                 show verbose information
#                                                 about missing or conflicting
#                                                 packages
#  --silence-errors                               show verbose information
#                                                 about missing or conflicting
#                                                 packages
#  --errors-to-stdout                             print errors from
#                                                 --print-errors to stdout not
#                                                 stderr
#
#Help options
#  -?, --help                                     Show this help message
#  --usage                                        Display brief usage message

confbindir=$(dirname $0)
libs_done=0
exit_code=0

do_exists=""
do_version=""
do_cflags=""
do_libs=""
do_print_errors=""


logit()
{
	# echo "$*" >> /tmp/pkg-config-dummy.log
	: NOOP
}

unsupported_option()
{
	logit "Unsupported option $1"
	echo "${1}: unknown option"
	exit_code=2
}

ignored_option()
{
	logit "Ignoring option $1"
}


process_library()
{
	local lib="$1"
	local oper="$2"
	local checkver="$3"
	local cmd="$confbindir/$lib-config"
	local ver

	((libs_done++))

	if [ ! -x "$cmd" ]
	then
	    logit "Package $lib not found"
	    if [ -z "$do_exists" -o -n "$do_print_errors" ]
	    then
		echo "Package $lib was not found in the pkg-config search path."
		echo "Perhaps you should add a $lib-config command to the"
		echo "$confbindir directory."
		echo "No package '$lib' found"
	    fi
	    exit_code=1
	    return
	fi

	if [ -n "$oper" ]
	then
	    ver=$($cmd --version)
	    if strverscmp $ver "$oper" $checkver
	    then
		: Its a good one
	    else
		exit_code=$?
		if [ -n "$do_print_errors" ]
		then
		    echo "Requested \'$lib $oper $checkver\' but version of $lib is $ver"
		fi
	    fi
	fi

	[ -n "$do_version" ] && $cmd --version
	[ -n "$do_cflags" ]  && $cmd --cflags
	[ -n "$do_libs" ]    && $cmd --libs
}


# Main

logit "args: $*"
while [ $# -gt 0 ]
do
    case "X$1" in
    X--version)
	echo "0.00 # Dummy script"
	logit "Result: 0"
	exit 0
	;;
    X--atleast-pkgconfig-version)
	logit "Result: 0"
	exit 0
	;;
    X--modversion)
	do_version=y
	;;
    X--libs)
	do_libs=y
	;;
    X--cflags)
	do_cflags=y
	;;
    X--exists)
	do_exists=y
	;;
    X--print-errors)
	do_print_errors=y
	;;
    X--list-all|\
    X--debug|\
    X--short-errors|\
    X--silence-errors|\
    X--errors-to-stdout)
	ignored_option "$1"
	;;
    X-*)
	unsupported_option "$1"
	;;
    X*)
	case "X$2" in
	X\>=|X\<=|X\>|X\<|X==)
	    process_library "$1" "$2" "$3"
	    shift
	    shift
	    ;;
	X)
	    # "s deliberatly missing as we are sometimes passed "lib <= ver"
	    process_library $1
	    ;;
	*)
	    logit "Unknown version operator $oper"
	    exit_code=3
	    ;;
	esac
	;;
    esac
    shift
done

if [ $exit_code == 0 -a $libs_done == 0 ]
then
    logit "no libs found"
    echo "Must specify package names on the command line"
    exit_code=1
fi

logit "Result: $exit_code"
exit $exit_code
