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


int main ( int argc, const char *argv[])
{
	char *p,path[255],name[32];

	if (argc>1)
	{
		build_player((char*)argv[1]);
	}
	else
		printf("Usage: jazbuild [performance file]\r\n");
		
	return(0);
}

build_player(char *name)
{
	FILE *prf,*mdtab,*envelopes,*patches,*samples,*parse,*ext,*cnf,*test;
	char path[255],midiname[255],buffer[128],com[128],line[255],label[64];
	int mode,err,l,patch,ch,ptch,from,to,off,pri;
	char *lbuffer,*sp;
	
	lbuffer=malloc(65535L);
	if (!lbuffer)
		return;
	*lbuffer=0;
	
	printf("JAZBuild V1.1\r\n-------------\r\n\r\n");

	prf=fopen(name,"r");	
	strcpy(path,name);
	*strrchr(path,'\\')=0;
	strcat(path,"\\");
	strcpy(midiname,strrchr(name,'\\')+1);
	*strchr(midiname,'.')=0;
	strcat(midiname,".MID");
			
	strcpy(buffer,path);
	strcat(buffer,"MODULES\\ATTEN1.DAS");
	test=fopen(buffer,"r");
	if (test)
	{
		fclose(test);
		strcat(path,"MODULES\\");
	}

	sprintf(buffer,"%sPATCHES.DAT",path);
	patches=fopen(buffer,"w");	
	sprintf(buffer,"%sMDTAB.DAT",path);
	mdtab=fopen(buffer,"w");	
	sprintf(buffer,"%sSAMPLES.DAT",path);
	samples=fopen(buffer,"w");	
	sprintf(buffer,"%sENVELOPES.DAT",path);
	envelopes=fopen(buffer,"w");	
	sprintf(buffer,"%sPARSE.CNF",path);
	parse=fopen(buffer,"w");	
	sprintf(buffer,"%sEXTERNAL.DAT",path);
	ext=fopen(buffer,"w");	
	sprintf(buffer,"%sSYNTH.CNF",path);
	cnf=fopen(buffer,"w");	

	if (!cnf || !prf || !patches || !samples || !mdtab || !envelopes || !parse || !ext)
	{
		printf("---ERROR: Can't open files!\r\n");
		return;
	}

	printf("Creating files...\r\n");
	
	l=err=0;
	mode=0;
	while (fgets(buffer,255,prf))
	{
		if (!l && strncmp(buffer,";#ATPERF",8))
		{
			printf("---ERROR: This is no performance file!\r\n");
			err=1;
			break;
		}
		l=1;
		
		if (!mode && buffer[0]==';' && buffer[1]==':')	/* Patch Map Table */
		{
			sscanf(buffer,";: %d %d %d - %d %d %d",&ch,&ptch,&from,&to,&off,&pri);
			sprintf(buffer,"%d: %d - %d %d %d %d\r\n",ch,from,to,ptch,off,pri); 
			fputs(buffer,parse);
		}
	
		if (strstr(buffer,"section set GLOBALVAR"))	/* GLOBALVAR */
		{
			mode=-1;
		}
		if (strstr(buffer,"section set SYNTHCNF"))	/* SYNTHCNF */
		{
			printf("... SYNTH.CNF\r\n");
			mode=5;
		}
		if (strstr(buffer,"section set MDTAB"))	/* MDTAB */
		{
			printf("... MDTAB.DAT\r\n");
			mode=1;
		}
		if (strstr(buffer,"section set ENVELOPE"))	/* ENVELOPE */
		{
			printf("... ENVELOPE.DAT\r\n");
			mode=2;
		}
		if (strstr(buffer,"section set SAMPLE"))	/* SAMPLE */
		{
			printf("... SAMPLE.DAT\r\n");
			mode=3;
		}
		if (strstr(buffer,"section set PATCH\n"))	/* PATCH */
		{
			printf("... PATCH.DAT\r\n");
			mode=4;
		}
		
		if (!strstr(buffer,"section set"))
		{
			switch(mode)
			{
				case 1:
					fputs(buffer,mdtab);
				break;

				case 2:
					fputs(buffer,envelopes);
				break;
				
				case 3:
					fputs(buffer,samples);
				break;

				case 4:
					if (buffer[0]=='\t')
					{
						sscanf(buffer," .dc.l %s",label);
						if (label[0]!='$')
						{
							if (strncmp(label,"ROM_",4))
							{
								sp=strstr(lbuffer,label);
								if (!sp || (sp && sp[strlen(label)]!=10))
								{
									sprintf(line,"\t.extern %s\n",label);
									fputs(line,ext);	
									sprintf(line,"\tXDEF %s\n",label);
									strcat(lbuffer,line);
								}
							}
						}
					}
					fputs(buffer,patches);
				break;
				
				case 5:
					fputs(buffer,cnf);
				break;
			}
		}
	
	}
	
	fputs(lbuffer,mdtab);
	fclose(prf);
	fclose(mdtab);
	fclose(envelopes);
	fclose(samples);
	fclose(patches);
	fclose(parse);
	fclose(ext);
	fclose(cnf);
	printf("Done!\r\n");

#if TOS
	Dsetpath(path);	
#else
	chdir(path);
#endif

	if (!err)
	{
/*		printf("Calling parser...\r\n");
		strcpy(buffer,"c:\\bin\\parse.ttp");
		sprintf(com,"-q -o%sSCORE.DAT %s%s",path,path,midiname);
		exec(0,buffer,com,"",&err);*/
		
/*		printf("Calling make...\r\n");
		strcpy(buffer,"c:\\bin\\make.ttp");
		exec(0,buffer,"","",&err);*/
/*		
		printf("Calling parser...\r\n");
		sprintf(com,"parse -q -o%sSCORE.DAT %s%s",path,path,midiname);
		system(com);
		printf("Calling make...\r\n");
		system("make");*/
	}
	free(lbuffer);
}



FileCopy(char *src,char *dest)
{
	char *buf;
	FILE *s,*d;
	long len;
	
	s=fopen(src,"rb");
	d=fopen(dest,"wb");
	if (s && d)
	{
		fseek(s,0,2);
		len=ftell(s);
		fseek(s,0,0);
		buf=malloc(len);
		if (buf)
		{
			fread(buf,1,len,s);
			fwrite(buf,1,len,d);
			fclose(s);
			fclose(d);
			free(buf);
		}
		else
			return(-1);	
	}
	else
		return(-33);

	return(0);
}
