#!/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 [--base-dir dir] [--flavor flavor] category/${port_or_package} variable-name\n"
    exit 1
}


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

if auto-using-pkgsrc && [ -e $(auto-pkgsrc-prefix)/bin/bmake ]; 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

while [ $(echo $1 | cut -c 1) = '-' ]; do
    if [ $1 = '--base-dir' ]; then
	base_dir=$2
	shift
	shift
    elif [ $1 = '--flavor' ]; then
	flavor="FLAVOR=$2"
	shift
	shift
    else
	usage
    fi
done

case $# in
2)
    ;;
*)
    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} $flavor WITHOUT_GNOME=yes -v $variable
