#	@(#)allocchar	3.9 (motonori/WIDE) 16 Jun 1997
#
#	script for allocation of new class/macro character
#
#	allocating type to be set in _ALLOC_TYPE: macro class
#	allocating mode to be set in _ALLOC_MODE: normal extend
#	allocating direction to be set in _ALLOC_DIR: up down
#	preffered character to be set in _ALLOC_PREF: <a char> or NULL
#	usage information to be set in _ALLOC_USAGE: any comment
#
#	allocated character is returned in _ALLOC_CHAR.
#	_ALLOC_CHAR is NULL if allocation failed.
#	a warning message is also output.

if [ "$_ALLOC_TYPE" = "" ]
then
	echo "allocchar: _ALLOC_TYPE is not set." 1>&2
	_ALLOC_TYPE=pair
fi
if [ "$_ALLOC_MODE" = "" ]
then
	echo "allocchar: _ALLOC_MODE is not set." 1>&2
	_ALLOC_MODE=normal
fi
if [ "$_ALLOC_DIR" = "" ]
then
	echo "allocchar: _ALLOC_DIR is not set." 1>&2
	_ALLOC_DIR=down
fi

_ALLOC_INVALID=_ERROR_
_ALLOC_CHAR=$_ALLOC_INVALID


if [ "$_ALLOC_MODE" = normal ]
then

	: ${_ALLOC_CLASS_N_FREE=ABCDEFGHIJKLMNOPQRSTUVWYZ}
	: ${_ALLOC_MACRO_N_FREE=ABCDEFGHIJKLMNOPQRSTUVWYZ}

	_ALLOC_CLASS_FREE=$_ALLOC_CLASS_N_FREE
	_ALLOC_MACRO_FREE=$_ALLOC_MACRO_N_FREE

	case "$_ALLOC_TYPE" in
	class)
		_ALLOC_FREE=$_ALLOC_CLASS_FREE
		;;
	macro)
		_ALLOC_FREE=$_ALLOC_MACRO_FREE
		;;
	pair)
		_ALLOC_FREE=`echo $_ALLOC_CLASS_FREE$_ALLOC_MACRO_FREE | awk '
			/A.*A/ {str=str"A"}
			/B.*B/ {str=str"B"}
			/C.*C/ {str=str"C"}
			/D.*D/ {str=str"D"}
			/E.*E/ {str=str"E"}
			/F.*F/ {str=str"F"}
			/G.*G/ {str=str"G"}
			/H.*H/ {str=str"H"}
			/I.*I/ {str=str"I"}
			/J.*J/ {str=str"J"}
			/K.*K/ {str=str"K"}
			/L.*L/ {str=str"L"}
			/M.*M/ {str=str"M"}
			/N.*N/ {str=str"N"}
			/O.*O/ {str=str"O"}
			/P.*P/ {str=str"P"}
			/Q.*Q/ {str=str"Q"}
			/R.*R/ {str=str"R"}
			/S.*S/ {str=str"S"}
			/T.*T/ {str=str"T"}
			/U.*U/ {str=str"U"}
			/V.*V/ {str=str"V"}
			/W.*W/ {str=str"W"}
			/X.*X/ {str=str"X"}
			/Y.*Y/ {str=str"Y"}
			/Z.*Z/ {str=str"Z"}
			/a.*a/ {str=str"a"}
			/b.*b/ {str=str"b"}
			/c.*c/ {str=str"c"}
			/d.*d/ {str=str"d"}
			/e.*e/ {str=str"e"}
			/f.*f/ {str=str"f"}
			/g.*g/ {str=str"g"}
			/h.*h/ {str=str"h"}
			/i.*i/ {str=str"i"}
			/j.*j/ {str=str"j"}
			/k.*k/ {str=str"k"}
			/l.*l/ {str=str"l"}
			/m.*m/ {str=str"m"}
			/n.*n/ {str=str"n"}
			/o.*o/ {str=str"o"}
			/p.*p/ {str=str"p"}
			/q.*q/ {str=str"q"}
			/r.*r/ {str=str"r"}
			/s.*s/ {str=str"s"}
			/t.*t/ {str=str"t"}
			/u.*u/ {str=str"u"}
			/v.*v/ {str=str"v"}
			/w.*w/ {str=str"w"}
			/x.*x/ {str=str"x"}
			/y.*y/ {str=str"y"}
			/z.*z/ {str=str"z"}
			END {print str}'`
	#	_ALLOC_DIR=up
		;;
	*)
		echo "allocchar: bad type: $_ALLOC_TYPE." 1>&2
		;;
	esac

	if [ "$_ALLOC_PREF" ]
	then
		if expr $_ALLOC_FREE : ".*$_ALLOC_PREF" > /dev/null;
		then
			_ALLOC_CHAR=$_ALLOC_PREF
		fi
	fi

	if [ "$_ALLOC_CHAR" = "$_ALLOC_INVALID" -a "$_ALLOC_FREE" != "" ]
	then
		case "$_ALLOC_DIR" in
		up)
			_ALLOC_CHAR=`expr $_ALLOC_FREE : '\(.\)'`
			;;
		down)
			_ALLOC_CHAR=`expr $_ALLOC_FREE : '.*\(.\)'`
			;;
		*)
			echo "allocchar: bad direction: $_ALLOC_DIR." 1>&2
			;;
		esac
	fi

	if [ "$_ALLOC_CHAR" = "$_ALLOC_INVALID" ]
	then
		echo "allocchar: can not allocate new character." 1>&2
	fi

	case "$_ALLOC_TYPE" in
	class)
	    _ALLOC_CLASS_FREE=`echo $_ALLOC_CLASS_FREE | sed "s/$_ALLOC_CHAR//"`
		;;
	macro)
	    _ALLOC_MACRO_FREE=`echo $_ALLOC_MACRO_FREE | sed "s/$_ALLOC_CHAR//"`
		;;
	pair)
	    _ALLOC_CLASS_FREE=`echo $_ALLOC_CLASS_FREE | sed "s/$_ALLOC_CHAR//"`
	    _ALLOC_MACRO_FREE=`echo $_ALLOC_MACRO_FREE | sed "s/$_ALLOC_CHAR//"`
		;;
	*)	;;
	esac

	_ALLOC_CLASS_N_FREE=$_ALLOC_CLASS_FREE
	_ALLOC_MACRO_N_FREE=$_ALLOC_MACRO_FREE

else

	: ${_ALLOC_E_COUNT=0}
	_ALLOC_EXT_H='{Ext_'
	_ALLOC_EXT_T='}'

	_ALLOC_E_COUNT=`expr $_ALLOC_E_COUNT + 1`
	_ALLOC_CHAR=`echo $_ALLOC_E_COUNT | tr '[0-9]' '[A-Z]'`
	_ALLOC_CHAR=$_ALLOC_EXT_H$_ALLOC_CHAR$_ALLOC_EXT_T

fi


if [ "$CHAR_ALLOCATION_LIST" = "" ]
then
        CHAR_ALLOCATION_LIST='\
# class/macro characters allocation information\
#  macro X: MX sendmail flag (define "." if true) [allocchar]\
#  class X: fake domain list and special terminators [allocchar]'
fi

CHAR_ALLOCATION_LIST="$CHAR_ALLOCATION_LIST\\
#  $_ALLOC_TYPE $_ALLOC_CHAR: $_ALLOC_USAGE"

# _ALLOC_TYPE=
# _ALLOC_MODE=
# _ALLOC_DIR=
# _ALLOC_USAGE=
_ALLOC_PREF=

# for debug
# echo $_ALLOC_CHAR
