#include <stdio.h>
#include <string.h>
#include <math.h>

#include "find.h"

/*	CMS Generation: 3                       26-OCT-89
*/


#define PI	3.141592
#define RADIANS (PI/180.0)

char bar[5]="\t";

FILE *fin,*fout;
int indents=0;

standard_header(name)
char *name;
{
	fprintf(fout,"%s\n",name);

}
standard_ending()
{
	fprintf(fout,"----------\n");

}

error_ending()
{
	fprintf(fout,"---- errors occured \n");

}


read_file(dx,dy,fp)
int dx,dy;
FILE 	*fp;
{
	                
	int i;                                                

	int x,w,y,h,dummy;
	int l_style= -1;                                                  
	int t_size; 
	char style[200],dummy_line[200],command[200],sdum[7];

	char link_file[200],note_file[200];
	char *start;

	link_file[0]='\0';
	note_file[0]='\0';
                                                                
	dummy=fgets(dummy_line,200,fp);	/* bounds or sub-object */

	while( dummy != NULL)
	{

		if (strchr(dummy_line,'[') != 0)   /* start of sub-object */
		{
			dummy=fgets(dummy_line,80,fp);	/* top bounds */
			read_file(dx+x,dy+y,fp);                
			dummy=fgets(dummy_line,100,fp);	/* blank line */
			dummy=fgets(dummy_line,100,fp);	/* blank line */
			dummy=fgets(dummy_line,100,fp);	/* new object */
		
	      	}
		else 
		if (strchr(dummy_line,']') != 0)   /* end of sub object */
		{
			sscanf(dummy_line,"]%s",note_file); /* get note */
			if (strchr(note_file,'.') != 0) /* valid filename */
			{
				for (i=0;i<indents;i++)    /* print indents */
					fprintf(fout,bar);
				fprintf(fout,"NOTE: %s \n",note_file);
		     	}
			return(1);
	      	}
		else	/* must be normal object */
		{                                          
		sscanf(dummy_line,"%d%d%d%d%d%s",&x,&y,&w,&h,&style,note_file);

			fgets(command,200,fp);	
  		  	fgets(style,100,fp);	/* line style */

			/** if note file for this object print it out **/
			dummy=fgets(dummy_line,200,fp); /* link file */
			
			/*** if we have a link file ***/

			if (strchr(dummy_line,'.') != 0)	
			{
			sscanf(dummy_line,"%s",link_file);
			for (i=0;i<indents;i++) 
				fprintf(fout,bar);         

		      		fprintf(fout,"LINK: %s",link_file);
				if (strchr(note_file,'.') !=0)
				{
					fprintf(fout," + NOTE: %s",note_file);
					note_file[0]='\0';
				}
				fprintf(fout,"\n");
	     			indents++;

				if (do_file(link_file)==NULL)
				 fprintf(fout,"***\n");

				--indents;

				link_file[0]='\0';
			}          
			else
			if (strchr(note_file,'.') !=0)
			{
				for (i=0;i<indents;i++) 
					fprintf(fout,bar);
				fprintf(fout,"NOTE: %s \n",note_file);
				note_file[0]='\0';
			}
	


			dummy=fgets(dummy_line,200,fp); /* blank line */
			dummy=fgets(dummy_line,200,fp); /* next bounds*/
		} /* end of not [ or ] */

	}   /* end of while */	
}

do_file(name)  
char *name;
{
	FILE *fin;
	char line[200];
	int i;

/* open mdraw file */                      
                          
	if ( (fin=fopen(name,"r") ) == NULL)
	{
	 printf("error opening file\n");
	 return(NULL);
	}
	
/* for each line , draw command */
        
	fseek(fin,0L,0);

	fgets(line,200,fin);	/* get zoom file and note file*/

	read_file(0,0,fin);

/* close mdraw file */
	fclose(fin);

	return(1);
}

main(argc,argv)
int argc;
char *argv[];
{
	FILE *fopen();
	char c,line[256];
	short unsigned class,element,listlen;
	short unsigned header;
	int tlen,i;

/*** let's see, what do we need to do? ***/                  
/* open mdraw file */
/* open output file */

/* write standard header */

/* for each drawing command in mdraw file */
	/* write link file and note file */

/* write standard ending */

/* close output file */
/* close mdraw file */


/*** that's it... so.... ****/
/***** lets do it !!!! ******/


/* open output */

	if (argc <= 2)     
	{printf("format: mtree <input> <output>\n");
	 printf("no output specified\n");
	 return(1);
	}
	/* open output metafile */
	if ( (fout=fopen(argv[2],"w")) == NULL)
	{
	 printf("error opening output \n");
	 return(1);
	}
/* write standard header */
	standard_header(argv[1]);


	if (argc <= 1)
	{printf("format: mtree <input> <output>\n");
	 printf("No input specified\n");
	 return(1);
	}
                                                    
	if (do_file(argv[1]) == NULL)
	{
	error_ending();
	return(1);
	}
	standard_ending();

/* write standard ending */

/* close metafile */
	fclose(fout);


}
