#!/bin/sh
#
#	$NetBSD: wtf,v 1.13 2005/07/01 14:21:49 peter Exp $
#
# Public domain
#

usage() {
	echo "usage: `basename $0` [-f dbfile] [is] <acronym>"
	exit 1
}

acronyms=${ACRONYMDB:-`ls /usr/share/misc/acronyms*`}

args=`getopt f: $*`
if [ $? -ne 0 ]; then
	usage
fi
set -- $args
while [ $# -gt 0 ]; do
	case "$1" in
		-f)
			acronyms=$2; shift
			;;
		--)
			shift; break
			;;
	esac
	shift
done

if [ "$1" = "is" ] ; then
	shift
fi

if [ $# -lt 1 ] ; then
	usage
fi

for f in $acronyms
do
	if [ ! -f $f ]; then
		echo "`basename $0`: cannot open acronyms database file \`$f'" 1>&2
		exit 1
	fi
done

rv=0
while [ $# -gt 0 ] ; do
	# Search acronyms list first
	target=`echo $1 | tr '[a-z]' '[A-Z]'`
	ans=`cat $acronyms | fgrep -h $target 2>/dev/null \
	     | sed -ne "\|^$target[[:space:]]|s|^$target[[:space:]]*||p"`
	if [ "$ans" != "" ] ; then
		echo "$target: $ans"
		shift ; continue
	fi

	# Try whatis(1) next
	ans=`whatis $1 2>/dev/null`
	if [ $? -eq 0 ] ; then
		echo "$ans" | sort | uniq
		shift ; continue
	fi

	# Try pkg_info(1) next
	ans=`pkg_info -qc $1 2> /dev/null`
	if [ $? -eq 0 ] ; then
		echo "$1: $ans"
		shift ; continue
	fi

	# Give up!
	echo "`basename $0`, I don't know what $1 means!" 1>&2
	rv=1
	
	shift
done
exit $rv
