#!/bin/sh
# @(#)$Mu: tools/findawk,v 1.15 $
##
## findawk
##	Attempts to find an awk that behaves like nawk (the search
##	just looks for mawk, gawk, nawk, and awk in various places
##	and takes the first one that exists and appears to understand
##	the -v switch.
##
## Copyright (C) 1996  Eric A. Howe
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##
##   Authors:	Matthew D. Francey
##		Eric A. Howe (mu@echo-on.net)
##

check() {
	awk=$1
	out=`${awk} -v x=x 'BEGIN {print x; exit;}' 2>&1`
	[ $? -eq 0 -a "${out}" = 'x' ] && return 0 || return 1
}

me=`basename $0`
path="${PATH}:/usr/local/bin:/usr/bin:/bin"
dirs=`echo ${path} | tr ':' ' '`
awks="mawk gawk nawk awk"

##
## we need an awk that groks -v, i.e. we really want an awk that is nawk
##
for a in ${awks}; do
	for d in ${dirs}; do
		check ${d}/${a} && echo ${d}/${a} && exit 0
	done
done

##
## we're toast!
##
echo "${me} : no usuable awk found!"
exit 1
