/*

	by Alexander Rawass, a_rawass@informatik.uni-kl.de
*/

#include <stdio.h>
#include <exec/types.h>

#include "/ar_defs.h"

#define	LMAX 200

int	count,fromcount;
FILE	*omfh;
FILE	*mainfh;
char	buf[LMAX];
char	from[LMAX];
char	node[LMAX];
char	previous[LMAX];
char	up[LMAX];
char	file[LMAX];
char	next[LMAX];
char	dummy[LMAX];
int	buflen;
BOOL	header_skipped=FALSE;
BOOL	komma;
int	lines;
BOOL	is_menu;
char	menu[LMAX];
BOOL	is_note;
char	note[LMAX];

VOID	stripM(buf)
STRPTR	buf;
{
	int	i;

	for(i=0;buf[i]!='\n' && buf[i]!=0;i++){
		if(buf[i]==0x0d){
			buf[i]=0;
		}
	}
	buf[i]=0;
}

VOID	nospace(buf)
STRPTR	buf;
{
int	i;

	for(i=0;buf[i]!=EOS;i++){
		if(buf[i]==' '){
			buf[i]='_';
		}
	}
}

int	main(argc,argv)
int	argc;
STRPTR	argv[];
{
int	i,j,k;
int	lasti;
int	a,b,c,d;

	if(argc!=3){
		printf("Usage: %s <infoview-file> <amigaguide-file>\n",argv[0]);
		exit(NULL);
	}
	
	count=0;
	fromcount=0;

	omfh=fopen(argv[1],"r");
	mainfh=fopen(argv[2],"w");

	if(!(omfh && mainfh)){
		printf("ERROR: could not open files\n");
		exit(4);
	}
	
	strcpy(from,"----");

	fprintf(mainfh,"@node main \042%s\042\n\n",argv[1]);
	
	fgets(buf,LMAX,omfh);
	while(!feof(omfh)){
		lines++;
		stripM(buf);
		if(buf[0]==0x1f){
/*			if(count>0){
				fprintf(mainfh,"@endnode\n");
			}
*/
				fprintf(mainfh,"@endnode\n");
			count++;
		}
		else if(STRNEQU("File:",buf,strlen("File:"))){
		header_skipped=TRUE;
		up[0]=EOS;
		node[0]=EOS;
		previous[0]=EOS;
		i=0;
		buflen=strlen(buf);
while(i<buflen){
				for(a=i;buf[a]!=' ' && buf[a]!='\t' && buf[a]!=',';a++)	;
				if(buf[a]==','){
					komma=TRUE;
				}
				buf[a]=EOS;
				/* in buf[i:a] steht nun Node:, File:, Next: etc. */
				
/*				for(b=a+1;buf[b]==' ';b++)	;	*/
				b=a+1;
				/* buf[b] ist der anfang des inhalts des keywords */				
				
/*				for(c=b;buf[c]!=' ' && buf[c]!='\t' && buf[c]!=',' && c<buflen;c++)	;	*/
				for(c=b;buf[c]!='\t' && buf[c]!=',' && c<buflen;c++)	;
				buf[c]=EOS;
				/* buf[b:c] ist der inhalt des keywords */
				
				for(d=c+1;c<buflen && (buf[d]==' ' || buf[d]=='\t' || buf[d]==',');d++)	;
				/* mit buf[d] gehts wieder in den loop */
				
				nospace(&(buf[b]));
				if(STRNEQU(&(buf[i]),"File:",strlen("File:"))){
					strcpy(file,&(buf[b]));
				}
				else if(STRNEQU(&(buf[i]),"Node:",strlen("Node:"))){
					strcpy(node,&(buf[b]));
					printf("Node: %s\n",node);
				}
				else if(STRNEQU(&(buf[i]),"Up:",strlen("Up:"))){
					strcpy(up,&(buf[b]));
				}
				else if(STRNEQU(&(buf[i]),"Previous:",strlen("Previous:"))){
					strcpy(previous,&(buf[b]));
				}
				else if(STRNEQU(&(buf[i]),"Prev:",strlen("Prev:"))){
					strcpy(previous,&(buf[b]));
				}
				else if(STRNEQU(&(buf[i]),"Next:",strlen("Next:"))){
					strcpy(next,&(buf[b]));
				}
				else{
					printf("WARNING: unknown command %s line %d\n",buf[i],lines);
				}
				
				i=d;
			}
				
			fprintf(mainfh,"@node %s-%s \042%s-%s\042\n",file,node,file,node);
			if(up[0]!=EOS){
				fprintf(mainfh,"Up: @{\042%-12s\042 LINK %s-%s}     ",up,file,up);
			}
			if(next[0]!=EOS){
				fprintf(mainfh,"Next: @{\042%-12s\042 LINK %s-%s}     ",next,file,next);
			}
			if(previous[0]!=EOS){
				fprintf(mainfh,"Prev: @{\042%-12s\042 LINK %s-%s}     ",previous,file,previous);
			}
			fprintf(mainfh,"\n\n");
		}
		else if(STRNEQU(buf,"* ",2) && !(STRNEQU("* Menu:",buf,strlen("* Menu:")))){
			/* maybe a menu? */
			is_menu=FALSE;
			for(i=1;buf[i]!=EOS && is_menu==FALSE;i++){
				if(buf[i]==':' /* && buf[i-1]==':' */){
					is_menu=TRUE;
					buf[i]=EOS;
					nospace(&(buf[2]));
					strcpy(menu,&(buf[2]));
					if(buf[i+1]==':'){
						buf[i+1]=EOS;
					}
				}
			}
			if(is_menu){
				fprintf(mainfh,"  @{\042%s\042 LINK %s-%s}",menu,file,menu);
				fprintf(mainfh,"  %s\n",&(buf[i+1]));
			}
			else{
				fprintf(mainfh,"%s\n",buf);
			}
		}
		else{
			is_note=FALSE;
			for(i=0;buf[i]!=EOS && is_note==FALSE;i++){
				if(STRNEQU(&(buf[i]),"*Note ",strlen("*Note "))){
					is_note=TRUE;
					buf[i]=EOS;
					for(j=i+6;buf[j]!=':' && buf[j]!=EOS;j++)	;
					buf[j]=EOS;
					strcpy(note,&(buf[i+6]));
				}
			}
			if(is_note==TRUE){
				fprintf(mainfh,"%s",buf);
				fprintf(mainfh,"  @{\042%s\042 LINK %s-%s}",note,file,note);
				fprintf(mainfh,"%s\n",&(buf[j+3]));
			}
			else{
				fprintf(mainfh,"%s\n",buf);
			}
		}
		fgets(buf,LMAX,omfh);
	}
	
	fprintf(mainfh,"@endnode\n");

	fclose(omfh);
	fclose(mainfh);
}
