1#!/bin/sh
2#
3#	$NetBSD: wtf,v 1.25 2018/08/16 13:31:04 maya Exp $
4#
5# Public domain
6#
7
8PROGNAME="$(basename "$0")"
9offensive=false
10
11usage() {
12	echo "usage: $PROGNAME [-o] [-f dbfile] [is] term ..."
13	exit 1
14}
15
16while getopts f:o f
17do
18	case "$f" in
19	o)	offensive=true
20		;;
21	f)
22		acronyms="$OPTARG $acronyms"
23		;;
24	*)
25		usage
26		;;
27	esac
28done
29
30shift "$(expr "$OPTIND" - 1)"
31
32if [ "$1" = "is" ]; then
33	if [ $# -gt 1 ] ; then
34		shift
35	fi
36fi
37
38if [ -z "$1" ]; then
39	usage
40fi
41
42if [ -z "$acronyms" ]; then
43	if [ -z "$ACRONYMDB" ]; then
44		for f in /usr/share/misc/acronyms*; do
45			case $f in
46			*-o)
47				if $offensive; then
48					acronyms="$acronyms $f"
49				fi
50				;;
51			*)
52				acronyms="$acronyms $f"
53				;;
54			esac
55		done
56	else
57		acronyms="$ACRONYMDB"
58	fi
59fi
60
61if [ -z "$acronyms" ]; then
62	echo "$PROGNAME: acronym database not found!" >&2
63	exit 1
64fi
65
66
67for f in $acronyms; do
68	if [ ! -f "$f" ]; then
69		echo "$PROGNAME: cannot open acronym database file \`$f'" >&2
70		exit 1
71	fi
72done
73
74rv=0
75for i; do
76	# Search acronym list first
77  target="$(echo "$i" | tr '[:lower:]' '[:upper:]')"
78	ans="$(fgrep -h "$target" $acronyms 2>/dev/null \
79	     | sed -ne "\|^$target[[:space:]]|s|^$target[[:space:]]*||p")"
80	if [ -n "$ans" ] ; then
81		echo "$target: $ans"
82		continue
83	fi
84
85	# Try whatis(1) next
86	ans="$(whatis "$i" 2>/dev/null)"
87	if [ $? -eq 0 ]; then
88		echo "$ans" | sort -u
89		continue
90	fi
91
92	# Try pkg_info(1) next
93	ans="$(pkg_info -qc "$i" 2> /dev/null)"
94	if [ $? -eq 0 ]; then
95		echo "$i: $ans"
96		continue
97	fi
98
99	# Try pkg-info(1) next
100	ans="$(pkg info -qI "$i" 2> /dev/null)"
101	if [ $? -eq 0 ]; then
102		echo "$i: $ans"
103		continue
104	fi
105
106	# If called from pkgsrc package directory,
107	# try querying pkgsrc's help facility next
108	if [ -f ../../mk/bsd.pkg.mk ]; then
109		ans="$(make help topic="$i")"
110		if [ "$ans" != "No help found for $i." ]; then
111			echo "$i: $ans"
112			continue
113		fi
114	fi
115
116	# Give up!
117	echo "$PROGNAME: I don't know what \`$i' means!" 1>&2
118	rv=1
119done
120exit $rv
121