#!/bin/sh

##########################################################################
#   Script description:
#       Print the value of a port/package make variable.
#
#   Arguments:
#       [-p prefix]   Defaults to prefix of active ports/pkgsrc tree
#
#   History:
#   Date        Name        Modification
#   2013-01-21  Jason Bacon Begin
##########################################################################

usage()
{
    printf "Usage: $0 [-p prefix] category/${port_or_package} variable-name\n"
    exit 1
}


##########################################################################
#   Main
##########################################################################

if which bmake > /dev/null 2>&1; then
    make=bmake
    port_or_package=package
    base_dir=`auto-pkgsrc-dir`
else
    make=make
    port_or_package=port
    if [ $(auto-ostype) = FreeBSD ]; then
	if [ -z $PORTSDIR ]; then
	    base_dir=/usr/ports
	else
	    base_dir=$PORTSDIR
	fi
    else
	base_dir=`auto-pkgsrc-dir`
    fi
fi

case $# in
2)
    ;;
4)
    if [ $1 = '-p' ]; then
	base_dir=$2
	shift
	shift
    else
	usage
    fi
    ;;
*)
    usage
    ;;
esac
port_dir=$1
variable=$2

if [ ! -d $base_dir/$port ]; then
    printf "No such ${port_or_package}: $base_dir/$port\n"
    exit 2
fi

cd $base_dir/$port_dir
# FIXME: Is this still relevant?
# Prevent HAVE_GNOME from adding -gnome to PKGNAME
${make} WITHOUT_GNOME=yes -v $variable
