#!/bin/sh
#
# Prepare an SM macro file defining all unambiguous abbreviations of
# mongo keywords. With the -sm flag, include SM keywords.
#
# Usage: abbrev [ -sm ]
#
SM_keys='apropos chdir contour ctype do edit else foreach history if image
	key levels macro minmax notation old overload page print prompt
	quit range restore return row save set shade sort spline standard
	termtype user verbose version'
mongo_compatible="ecolumn pcolumn xcolumn ycolumn"
extra_keys="$mongo_compatible"
#
while [ "$1" != "" ]; do
	case $1 in
	  -sm|-SM)
		extra_keys="$extra_keys $SM_keys";;
	  *)
		Unknown argument $1;;
	esac
	shift
done
#
if true; then
cat - << END_OF_KEYS
	$extra_keys
	angle aspect axis box connect cursor data define delete device dot
	draw erase errorbar expand format grid help histogram label limits
	lines list location ltype lweight points ptype putlabel read relocate
	ticksize window write xlabel ylabel
END_OF_KEYS
fi | tr ' \011' '\012' | \
	sed -n -e '/^$/d' -e '{
		s/ *[^ ]*++//
		h
		y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
		G
		s/\([^ \n]*\)\n\(.*\)/\2 \1/p
		g
	}' | awk '{
			for(i = 2;i < length($1);i++) {
				printf("%s %s\n",$2,substr($1,0,i));
			}
	}' | \
	sort +1 | uniq -1 -u | \
	awk	'BEGIN {
			printf "#\n"
			printf "# macros for all abbrevations of SM keywords\n"
			printf "#\n"
		}
		{
			printf "%-16s%s\n",$2,$1
		}'
