#!/bin/bsh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# cmdtext720 src/cmdtext/usr/bin/spell/spell.sh 1.9 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1989,1991 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)49      1.9  src/cmdtext/usr/bin/spell/spell.sh, cmdtext, cmdtext720 3/30/07 03:55:41
#
# COMPONENT_NAME: (CMDTEXT) Text Formatting Services
#
# FUNCTIONS:
#
# ORIGINS: 3,10,27
#
# (C) COPYRIGHT International Business Machines Corp. 1989, 1991
# All Rights Reserved
# Licensed Materials - Property of IBM
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#	spell program
# Variables from environment (else defaulted):
#   D_SPELL dictionary, S_SPELL stop, H_SPELL history, P_SPELL program
# Local variables:
#   B flags
#   F input files
#   V data for -v
#   D -l/-i handling for forming deroff arguments
#   M non-null if multiple -i/-l flags
#   LOCAL file containing locally acceptable words (via + argument)
#   POSTPROC post-processing commands (for LOCAL list and history)

DSPMSG=/usr/bin/dspmsg
SORT=/usr/bin/sort

H_SPELL=${H_SPELL-/usr/lib/spell/spellhist}
P_SPELL=${P_SPELL-/usr/lib/spell/spellprog}
# 
#   Only deroff V5.5 and up know about -u flag.
DEROFF="deroff -u"
V=/dev/null
F=
B=
D=
M=

# Defect 91958 - environment variable LOCAL interfering with spell
LOCAL=

next="F="
trap "/usr/bin/rm -f /tmp/spell.$$; exit" 0 1 2 13 15
for A in $*
do
	case $A in
	-v)     if [ -x /bin/pdp11 ] && /bin/pdp11
		then
			$DSPMSG spell.cat 1 "-v option not supported on pdp11" >&2
# NOTE: PDP-11?
			EXIT_SPELL=exit
		else    B="$B -v"
			V=/tmp/spell.$$
		fi ;;
	-a)	: ;;
	-b) 	D_SPELL=${D_SPELL-/usr/lib/spell/hlistb}
		B="$B -b" ;;
	-x)     B="$B -x" ;;
	+*)	if [ "$FIRSTPLUS" = "+" ]
		then
			$DSPMSG spell.cat 2 "multiple + options in spell, all but the last are ignored" >&2
		fi;
		FIRSTPLUS="$FIRSTPLUS"+
		if  LOCAL=`/usr/bin/expr $A : '+\(.*\)' 2>/dev/null`;
		then if test ! -r $LOCAL;
		     then
			$DSPMSG spell.cat 3 "spell cannot read %s" $LOCAL >&2
			EXIT_SPELL=exit
		     fi
		else
			$DSPMSG spell.cat 4 "spell cannot identify local spell file" >&2
			EXIT_SPELL=exit
		fi ;;
	-l|-i)  M="$D"
		D="$A" ;;
	-d)	# alternate dictionary (not american or british)
		next="D_SPELL="
		;;
	-s)	# alternate stoplist (must be hashed!)
		next="S_SPELL="
		;;
	-h)	# alternate histlist
		next="H_SPELL="
		;;
	-*)	# bad option
		$DSPMSG spell.cat 5 "spell: unknown flag '%s'" $A >&2
		$DSPMSG spell.cat 6 "usage: spell [-v] [-a|-b] [-x] [-l] [-i] [+hlist] [-d hlist] [-s hstop] [-h sphist] [file ...]" >&2
		exit 1
		;;
	*)	# option arguments or filenames
		eval $next"$A"
		next="F=$F@"
		;;
	esac
	done
${EXIT_SPELL-:}
IFS="${IFS}@"

if [ -n "$M" ]
then
	$DSPMSG spell.cat 7 "multiple -i/-l options in spell, all but the last are ignored" >&2
fi

# -l flag to spell means no special flags to deroff (follow ALL .so/.nx)
# default (no -l or -i flag to spell) means don't include /usr/lib*: deroff -l
# -i flag to spell (follow NO .so/.nx) is passed through to deroff
# NOTE: This is confusing, because it's backwards from deroff,
#	but is as documented.
case "$D" in
	-l)     D="" ;;
	"")     D="-l" ;;
esac

POSTPROC=
if [ -n "$LOCAL" ]
then
	POSTPROC=" | $SORT | /usr/bin/comm -23 - $LOCAL"
fi

if [ -w "$H_SPELL" ]
then
	POSTPROC="$POSTPROC | /usr/bin/tee -a $H_SPELL; who am i >>$H_SPELL 2>/dev/null"
fi

eval "$DEROFF -w $D $F |  $SORT -u +0 |  \
 $P_SPELL ${S_SPELL-/usr/lib/spell/hstop} 1 | \
 $P_SPELL ${D_SPELL-/usr/lib/spell/hlista} $V $B \
 $POSTPROC "
case $V in
/dev/null)	exit
esac
/usr/bin/sed '/^\./d' $V | $SORT -u +1f +0
## sed '/^\./d' $V | sort -u +0