#
# Vspell (visual spell) shell procedure. Sep. 1, 1985
# Brian Downs, AT&T Technologies, Inc.
#
if
        [ ${#} -eq 0 ]
then
        echo "Usage: ${0} [-c] file_name (only 1 file name allowed)"
        exit
fi
set -- `getopt c $*`
if
        [ $? != 0 ]
then
        echo "Usage: ${0} [-c] file_name (only 1 file name allowed)"
        exit 1
fi
FLAG=Y
for i in $*
do
        case $i in
        -c) FLAG=N
            shift;;
        --) shift;break;;
        esac
done
if
        [ ${#} -eq 0 ]
then
        echo "Usage: ${0} [-c] file_name (only 1 file name allowed)"
        exit
fi
if
        test ! -f ${1}
then
        echo "Can't read or locate the file ${1}"
        exit 1
fi
#if
#        [ "$LOCALDICT" = "" ]
#then
#        echo "vspell: LOCALDICT must be set"
#        exit
#fi
: ${LOCALDICT:=$HOME/.localdict}
export LOCALDICT
echo "Please wait while file ${1} is checked for spelling errors... \c"
trap 'if [ -s /tmp/inspp$$ ] ; then rm /tmp/inspp$$;fi;exit' 0 1 2 3 15 9 10 
if
        test -r "$LOCALDICT" -a -w "$LOCALDICT"
then
        sort -o $LOCALDICT -u $LOCALDICT
else
        cat /dev/null > $LOCALDICT
	if
	        test ! -r $LOCALDICT
	then
	        exit 0
	fi
fi
spell ${1} | sort >/tmp/insp$$
if
        test -s /tmp/insp$$
then
        ISBSNM=VS.`basename ${1}`
        comm -23 /tmp/insp$$ $LOCALDICT >/tmp/inspp$$
        rm /tmp/insp$$
        if
                test ! -s /tmp/inspp$$
        then
                echo "No spelling errors detected in ${1}"
                test -f /tmp/inspp$$ && rm /tmp/inspp$$
                exit
        fi
	if 
		u3b || vax
	then
        	fgrep -n -f /tmp/inspp$$ ${1} | cut -d':' -f1 |
                inspell ${1} /tmp/inspp$$ ${ISBSNM} ${FLAG}
	        rtn=${?} # get exit status of inspell
	else
	# The UNIX PC's fgrep does not support the '-f' option, so
	# we use sed here. (BWD)
	# This awk script didn't work because it is too SLOW.
	#           awk 'BEGIN{wcnt=0;scndflag=0}
	#                FILENAME=="/tmp/inspp'$$'" {word[wcnt++]=$0;next}
	#                {if(scndflag==0){NR=1;scndflag=1}
	#                 for(i=0;i<wcnt;i++)
	#                        if(index($0,word[i])){print NR;break}
	#                }' /tmp/inspp$$ ${1} |
	# The following sed script didn't work since it complained
	# of too many commands when the number of misspellings
	# was large.
	#	sed 's/\(.*\)/\/\1\/{/
	#	     	a\
	#	     	=\
	#	     	n\
	#	     	}' /tmp/inspp$$  >/tmp/insp$$
	#	sed -n -f /tmp/insp$$ $1 |
                inspell ${1} /tmp/inspp$$ ${ISBSNM} ${FLAG} `wc -l ${1}`
	        rtn=${?} # get exit status of inspell
	fi
        sort -o $LOCALDICT -u $LOCALDICT
        case ${rtn} in
                0)
                if
                        [ -f ${ISBSNM} ]
                then 
                        rm ${ISBSNM}
                fi
                ;;
                1)
                echo Corrected file is in ${ISBSNM}
                echo "Do you want ${ISBSNM} copied over ${1}? (y/n) \c"
                read ans
                if
                        test "$ans" = "y"
                then
                        mv ${ISBSNM} ${1}
                else
                        rm ${ISBSNM}
                        echo ${ISBSNM} has been deleted.
                fi
                ;;
        esac
else
        echo No spelling errors detected in ${1}
fi
