#include <stdio.h>
#include <string.h>
#include <proto/exec.h>

#include "AmigaEncoding.h"
/* Amiga standard character encoding array *amigaEncoding[256]. */

/* My own functions: */

char	*freadline(FILE *sourceFile);
char	*FindBaseName(char *typefaceName);
char	*GetMetricFilename(char typefaceNames[][68]);
ULONG	WriteMetricsVector(char *afmFilename, int typeWeight);

int main(int argc, char *argv[])
{
	FILE	*afmFiles[4],*metricFile,*metricVectorFile;
	ULONG	metricOffsets[5],vectorLengths[4];
	char	typefaceNames[4][68],vectorFilename[22];
	char	familyName[30],*incomingString,*singleChars,oneChar;
	int	test,index,index2;

/* This first part is specific to the CLI/Shell version of ConvertAFM.
   It checks the AFMs' contents to make sure they belong to the same
   typeface family. The program aborts conversion if:

   1) The first AFM file isn't found,
   2) One of the files isn't a FontMetrics 2.0 file, or
   3) The files aren't from the same typeface family.

   NOTE: The program continues conversion even if one .AFM file
   isn't found (Except for the first). This allows the user to
   include only a plain & italic version, or a plain & bold version,
   or any such combination, so long as they belong to the same
   typeface family.

   The third condition is overridden if the user supplies an alternate
   .metric filename. Custom .metric organizations are then possible. */

	if (argc<5)
	{
		printf("\nUsage: %s Reg.AFM [Bld.AFM] [Itl.AFM] [BldItl.AFM] [Output.metric]\n",argv[0]);
		printf("\nTo omit an .AFM, insert a dummy filename (eg: none.afm).\nYou MUST give one .AFM for Plain style.\n\n");
		exit(0);
	}

	familyName[0]=0;
	if (argc==6)
		strcpy(familyName,argv[5]);

/* Start checking the files themselves. Are they FontMetrics 2.0? */
	
	for (index=0;index<4;index++)
		afmFiles[index]=fopen(argv[index+1],"r");
	if (afmFiles[0]==0)
	{
		printf(".AFM file for Plain style not found.\nType:  %s  for help.\n",argv[0]);
		for (index=0;index<4;index++)
			fclose(afmFiles[index]);
		exit(0);
	}

/* Okay, check the typeface names. */

	for (index=0;index<4;index++)
	{
		if (afmFiles[index]!=0)
		{
			incomingString=freadline(afmFiles[index]);
			if (strcmp(incomingString,"StartFontMetrics 2.0"))
			{
				printf("%s is not an AFM 2.0 file!  Type  %s  for help.\n",argv[index+1],argv[0]);
				for (index2=0;index2<4;index2++)
					fclose(afmFiles[index2]);
				exit(0);
			}

			test=-1;
			while (test!=0)
			{
				incomingString=freadline(afmFiles[index]);
				sscanf(incomingString,"%s\n",typefaceNames[index]);
				test=strcmp(typefaceNames[index],"FontName");
			}
			sscanf(incomingString,"%s %s",typefaceNames[index],typefaceNames[index]);
			fclose(afmFiles[index]);
		}
		else
		{
			strcpy(typefaceNames[index],typefaceNames[0]);
			fclose(afmFiles[index]);
		}
	}

	if (strlen(familyName)==0)
		strcpy(familyName,GetMetricFilename(typefaceNames));

	if (strlen(familyName)==0)
	{
		printf("Error: These AFMs are from different type families!\n");
		exit(0);
	}
	else
	{
		printf("Typefaces chosen for %s:\n========================================\n",familyName);
		printf("Regular:     %s\n",typefaceNames[0]);
		for (index=1;index<4;index++)
		{
			test=strcmp(typefaceNames[index],typefaceNames[0]);
			if (test==0)
				strcpy(typefaceNames[index],"(No .AFM Given)");
		}
		printf("Bold:        %s\n",typefaceNames[1]);
		printf("Italic:      %s\n",typefaceNames[2]);
		printf("Bold-Italic: %s\n",typefaceNames[3]);
		printf("========================================\n");
	}
		

/* And now the facts are established. We have four or less AFMs
   to make metrics vectors from, and all idiot-proofing is done. */

	for (index=0;index<4;index++)
	{
		vectorLengths[index]=WriteMetricsVector(argv[index+1],index+1);
		if (vectorLengths[index]!=0)
			printf("Processed %s\n",typefaceNames[index]);
	}
	printf("========================================\n%s Complete.\n",familyName);

/* Now, four or less files are sitting in T: named by their
   typeface names. I generate the .metric by first writing
   five longwords (metric offsets and the file size) followed
   by these four or less files concatenated together. 
   Poof! Instant .metric file! */

	metricFile=fopen(familyName,"w");
	if (metricFile==0)
	{
		printf("Unable to create .metric file. \n");
		fclose(metricFile);
		exit(0);
	}

	metricOffsets[0]=20;
	for (index=1;index<5;index++)
		metricOffsets[index]=metricOffsets[index-1]+vectorLengths[index-1];

	for (index=1;index<4;index++)
		if (vectorLengths[index]==0)
			metricOffsets[index]=0;

	for (index=0;index<5;index++)
	{
		singleChars=&metricOffsets[index];
		fprintf(metricFile,"%c%c%c%c",singleChars[0],singleChars[1],singleChars[2],singleChars[3]);
	}

	for (index=0;index<4;index++)
	{
		strcpy(vectorFilename,"T:");
		strcat(vectorFilename,typefaceNames[index]);
		metricVectorFile=fopen(vectorFilename,"r");
		for (index2=0;index2<vectorLengths[index];index2++)
		{
			oneChar=fgetc(metricVectorFile);
			fputc(oneChar,metricFile);
		}
		fclose(metricVectorFile);
		remove(vectorFilename);
	} 

/* Whew! That's a lot of disk access! By now the .metric
   is complete and the T: files are gone. Time to close up
   and quit. */

	fclose(metricFile); 
	exit(0);
};

#include "GetMetricFilename.c"
#include "FindBaseName.c"
#include "freadline.c"
#include "WriteMetricsVector.c"

