# Generate names from files
# Requires two data files, prenames & surnames
#
# For MKS toolkit on PCs:
# AWK=/bin32/awk
#
# For UNIX:
# AWK=/bin/nawk
#
# For OpenNT on PCs:
AWK=gawk
gender=none
number=1
dict=prenames
pdict=prenames
sdict=surnames
progname=`basename $0`

function help
{
        print >$2 "$progname: a program to randomly generate names"
        print >&2 "Usage: $progname [-g a|f|m|n] [-n num ] file ..."
        print >&2 "       -g a|f|m|n    Gender (androgynous, female, male, none"
        print >&2 "       -h            Print this message"
        print >&2 "       -n num        Generate num names"
        exit 2
}

while getopts g:hn: opt
do
        case "$opt" in
                g)      gender="$OPTARG";;
                h)      help;;
                n)      number="$OPTARG";;
                [?])    print >&2 "Usage: $progname [-g a|f|m|n] [-n num ] file ..."
                        exit 1 ;;
        esac
done

shift $OPTIND-1

case "$gender" in
        a)      pdict=names.and
                sed -n -e "/	A/p" $dict > $pdict;;
        f)      pdict=names.fem
                sed -n -e " /	[FA]/p" $dict > $pdict;;
        m)      pdict=names.men
                sed -n -e "/	[MA]/p" $dict > $pdict;;
        n)      pdict=$dict;;
esac

count=1
name=""
while [ $count -le $number ]
do
        first=$($AWK -f name.awk < $pdict)
        last=$($AWK -f name.awk < $sdict)
#
# This is required on OpenNT because the random number function doesn't
# change numbers fast enough; comment out on other UNIX systems.
#
	if [ "$first $last" != "$name" ]
	then
        	count=$count+1
		name="$first $last"
        	print $first $last
	fi
done

exit 0
