#!/usr/bin/awk -f
# mkl
# genera el fichero de una letra dada
# debe usarse:
# ./mkl <letra-anterior> <letra-actual> <letra-siguiente> \
#       < <letra-actual>.txt > <letra-actual>.texinfo
#
# $Id: mkl,v 1.5 2000/11/17 04:11:07 cballard Exp $
#
# Copyright (C) 2000 Csar Ballardini
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# 
function letra_anterior(l)  { return substr(abecedario, index(abecedario,l)-1, 1); }
function letra_siguiente(l) { return substr(abecedario, index(abecedario,l)+1, 1); }
function procesa_argv()\
{
	letra=toupper(ARGV[1]);
	ARGC=1;

	if (letra == "0") { ANT="TOP" ; ACT="0"; SIG="A"; }
	if (letra == "Z") { ANT="Y" ; ACT="Z"; SIG="TOP"; }
	if ( (letra >= "A") && (letra < "Z") ) {ANT=letra_anterior(letra); ACT=letra; SIG=letra_siguiente(letra); }
}
function cabecera(actual, siguiente, anterior)\
{
	printf("@c -*-coding:iso-8859-1.unix; texinfo-*-\n"\
	"@node    %s,             %s,            %s,      top\n"\
	"@comment node-name,     next,           previous, up\n"\
	"@unnumbered %s\n"\
	"@iftex\n"\
	"@begindoublecolumns\n"\
	"@end iftex\n"\
	"\n"\
	"@itemize\n", actual, siguiente, anterior, actual);
}

function item(ingles, castellano, diccionario, glosario)\
{
	hay_dic= length(diccionario);
	hay_glo= length(glosario);

	printf("@item @b{%s}\n", ingles);
	if ( (hay_dic) || (hay_glo) ) {
		printf("@enumerate\n");
		printf("@item\n%s\n", castellano);

		if (hay_dic)
			printf("@item%s\n", diccionario);

		if (hay_glo)
			printf("@item%s\n", glosario);

		printf("@end enumerate\n");
	} else
		printf("%s\n", castellano);

	printf("@sp 1\n");
}

function pie()\
{

	print "\n@end itemize\n\n"\
	"@iftex\n"\
	"@enddoublecolumns\n"\
	"@end iftex\n"\
	"@c Generado automticamente por mkl\n"\
	"@c Csar Ballardini @emailcballard{}\n\n";
}

BEGIN { 
# si alguien sabe como obtener el cdigo de un caracter en AWK,
# por favor dgamelo as no hago esta chanchada con substr() y abecedario
	abecedario="0ABCDEFGHIJKLMNOPQRSTUVWXYZ"

	procesa_argv();
	cabecera( ACT, SIG, ANT);
}
/^@@@@ING/ { ING=substr($0,8); }
/^@@@@CAS/ { CAS=substr($0,8); }
/^@@@@DIC[ ]*Vocablo definido nicamente para el Diccio/ { next; }
/^@@@@DIC/ { DIC=DIC"\n"substr($0,8); }
/^@@@@GLO/ { GLO=GLO"\n"substr($0,8); }
/^@@@@FIN/ { 
		item( ING, CAS, DIC, GLO );
		ING=""; CAS=""; DIC=""; GLO="";
         }
END { pie(); }
# EOF mkl
