/* $VER: output.c 2.1 (05.06.94)
 */

#include "global.h"

/* Locale support */
#define CATCOMP_NUMBERS
#define CATCOMP_BLOCK
#include "CassLabel_strings.h"


/**********************************************************************
 *                        Generate the output                         *
 **********************************************************************/
void generate_file(char *path)
{
	char *pos1, *pos2;
	char bufline1[256], bufline2[256], argline[8][256];
	int buflineflag = 0, arglinecount = 0;
	struct NodeEntry *entry, emptynode = {"", ""};
	long endA, endB, translateflag;
	FILE *fh, *out;

	/* Get number of all entries */
	get(LV_SideA, MUIA_List_Entries, &endA);
	get(LV_SideB, MUIA_List_Entries, &endB);

	/* Get state of Translate-Checkmark */
	get(CM_Translate, MUIA_Selected, &translateflag);

	if ((fh = fopen(GetStr(PS_PopFormat_String), "r")) && (out = fopen(path, "w")))
	{
		while (pos1 = fgets(bufline1, 256, fh))
		{
			if (bufline1[0] == '@')
			{
				/* Read in argline */
				pos1++;
				while (*pos1 != '\n')
				{
					pos2 = strchr(pos1, '@');
					*pos2++ = '\0';

					/* Insert the common entries */
					if (!strcmp(pos1,"POSITION"))
							strcpy(argline[arglinecount++], CYA_Position[GetCycle(CY_Position)]);
					if (!strcmp(pos1,"LENGTH"))
							strcpy(argline[arglinecount++], CYA_Length[GetCycle(CY_Length)]);
					if (!strcmp(pos1,"NUMBER"))
							strcpy(argline[arglinecount++], GetStr(ST_Number));

					if (!strcmp(pos1,"TOPTITLE_A"))
						if (translateflag)
							strcpy(argline[arglinecount++], translate_to_latex(GetStr(ST_TitleA)));
						else
							strcpy(argline[arglinecount++], GetStr(ST_TitleA));
					if (!strcmp(pos1,"TOPGROUP_A"))
						if (translateflag)
							strcpy(argline[arglinecount++], translate_to_latex(GetStr(ST_GroupA)));
						else
							strcpy(argline[arglinecount++], GetStr(ST_GroupA));
					if (!strcmp(pos1,"DATE_A"))
							strcpy(argline[arglinecount++], GetStr(ST_DateA));
					if (!strcmp(pos1,"SOURCE_A"))
							strcpy(argline[arglinecount++], CYA_Source[GetCycle(CY_SourceA)]);
					if (!strcmp(pos1,"NOISER_A"))
							strcpy(argline[arglinecount++], CYA_NoiseR[GetCycle(CY_NoiseRA)]);

					if (!strcmp(pos1,"TOPTITLE_B"))
						if (translateflag)
							strcpy(argline[arglinecount++], translate_to_latex(GetStr(ST_TitleB)));
						else
							strcpy(argline[arglinecount++], GetStr(ST_TitleB));
					if (!strcmp(pos1,"TOPGROUP_B"))
						if (translateflag)
							strcpy(argline[arglinecount++], translate_to_latex(GetStr(ST_GroupB)));
						else
							strcpy(argline[arglinecount++], GetStr(ST_GroupB));
					if (!strcmp(pos1,"DATE_B"))
							strcpy(argline[arglinecount++], GetStr(ST_DateB));
					if (!strcmp(pos1,"SOURCE_B"))
							strcpy(argline[arglinecount++], CYA_Source[GetCycle(CY_SourceB)]);
					if (!strcmp(pos1,"NOISER_B"))
							strcpy(argline[arglinecount++], CYA_NoiseR[GetCycle(CY_NoiseRB)]);

					/* Insert the list entries */
					if (!strcmp(pos1,"TITLE_A"))
					{
						pos1 = pos2;
						pos2 = strchr(pos1, '@');
						*pos2++ = '\0';
						if (atoi(pos1) - 1 < endA)
							DoMethod(LV_SideA, MUIM_List_GetEntry, atoi(pos1) - 1, &entry);
						else
							entry = &emptynode;
						if (translateflag)
							strcpy(argline[arglinecount++], translate_to_latex(entry->title));
						else
							strcpy(argline[arglinecount++], entry->title);
					}
					if (!strcmp(pos1,"GROUP_A"))
					{
						pos1 = pos2;
						pos2 = strchr(pos1, '@');
						*pos2++ = '\0';
						if (atoi(pos1) - 1 < endA)
							DoMethod(LV_SideA, MUIM_List_GetEntry, atoi(pos1) - 1, &entry);
						else
							entry = &emptynode;
						if (translateflag)
							strcpy(argline[arglinecount++], translate_to_latex(entry->group));
						else
							strcpy(argline[arglinecount++], entry->group);
					}
					if (!strcmp(pos1,"EXTENSION_A"))
					{
						pos1 = pos2;
						pos2 = strchr(pos1, '@');
						*pos2++ = '\0';
						if (atoi(pos1) - 1 < endA)
							DoMethod(LV_SideA, MUIM_List_GetEntry, atoi(pos1) - 1, &entry);
						else
							entry = &emptynode;
						if (translateflag)
							strcpy(argline[arglinecount++], translate_to_latex(entry->extension));
						else
							strcpy(argline[arglinecount++], entry->extension);
					}

					if (!strcmp(pos1,"TITLE_B"))
					{
						pos1 = pos2;
						pos2 = strchr(pos1, '@');
						*pos2++ = '\0';
						if (atoi(pos1) - 1 < endB)
							DoMethod(LV_SideB, MUIM_List_GetEntry, atoi(pos1) - 1, &entry);
						else
							entry = &emptynode;
						if (translateflag)
							strcpy(argline[arglinecount++], translate_to_latex(entry->title));
						else
							strcpy(argline[arglinecount++], entry->title);
					}
					if (!strcmp(pos1,"GROUP_B"))
					{
						pos1 = pos2;
						pos2 = strchr(pos1, '@');
						*pos2++ = '\0';
						if (atoi(pos1) - 1 < endB)
							DoMethod(LV_SideB, MUIM_List_GetEntry, atoi(pos1) - 1, &entry);
						else
							entry = &emptynode;
						if (translateflag)
							strcpy(argline[arglinecount++], translate_to_latex(entry->group));
						else
							strcpy(argline[arglinecount++], entry->group);
					}
					if (!strcmp(pos1,"EXTENSION_B"))
					{
						pos1 = pos2;
						pos2 = strchr(pos1, '@');
						*pos2++ = '\0';
						if (atoi(pos1) - 1 < endB)
							DoMethod(LV_SideB, MUIM_List_GetEntry, atoi(pos1) - 1, &entry);
						else
							entry = &emptynode;
						if (translateflag)
							strcpy(argline[arglinecount++], translate_to_latex(entry->extension));
						else
							strcpy(argline[arglinecount++], entry->extension);
					}

					/* Locale Support */
					if (!strcmp(pos1,"LANG_POSITION"))
						if (translateflag)
							strcpy(argline[arglinecount++], translate_to_latex(GetLocaleString(MSG_LABEL_POSITION)));
						else
							strcpy(argline[arglinecount++], GetLocaleString(MSG_LABEL_POSITION));
					if (!strcmp(pos1,"LANG_LENGTH"))
						if (translateflag)
							strcpy(argline[arglinecount++], translate_to_latex(GetLocaleString(MSG_LABEL_LENGTH)));
						else
							strcpy(argline[arglinecount++], GetLocaleString(MSG_LABEL_LENGTH));
					if (!strcmp(pos1,"LANG_DATE"))
						if (translateflag)
							strcpy(argline[arglinecount++], translate_to_latex(GetLocaleString(MSG_LABEL_DATE)));
						else
							strcpy(argline[arglinecount++], GetLocaleString(MSG_LABEL_DATE));

					pos1 = pos2;
				}

				/* Begin the printing of bufline2 & argline */
				switch (arglinecount)
				{
					case 1:
						fprintf(out, bufline2, argline[0]);
						break;
					case 2:
						fprintf(out, bufline2, argline[0], argline[1]);
						break;
					case 3:
						fprintf(out, bufline2, argline[0], argline[1], argline[2]);
						break;
					case 4:
						fprintf(out, bufline2, argline[0], argline[1], argline[2], argline[3]);
						break;
					case 5:
						fprintf(out, bufline2, argline[0], argline[1], argline[2], argline[3], argline[4]);
						break;
					case 6:
						fprintf(out, bufline2, argline[0], argline[1], argline[2], argline[3], argline[4], argline[5]);
						break;
					case 7:
						fprintf(out, bufline2, argline[0], argline[1], argline[2], argline[3], argline[4], argline[5], argline[6]);
						break;
					case 8:
						fprintf(out, bufline2, argline[0], argline[1], argline[2], argline[3], argline[4], argline[5], argline[6], argline[7]);
						break;
				}

				arglinecount = 0;
				buflineflag = 0;
			}
			else
			{
				if (!buflineflag)
					buflineflag = 1;
				else
					fprintf(out, bufline2);

				strcpy(bufline2, bufline1);
			}
			
		}
		/* Is the buffer full? */
		if (buflineflag)
			fprintf(out, bufline2);

		fclose(fh);
		fclose(out);
	}
}


/**********************************************************************
 *                           Print output                             *
 **********************************************************************/
void print(void)
{
	char pathbuf[256], linebuf[512];
	static struct TagItem ProcTags[] =
	{
		SYS_Input, 0,
		SYS_Output, 0,
		NP_Error, 0,
		TAG_DONE
	};

	stccpy(pathbuf, dirbuf_generate, 256);
	AddPart(pathbuf, "CassLabel_out", 256);
	generate_file(pathbuf);
	sprintf(linebuf, "RX %s %s %s\n", GetStr(PS_PopDriver_String), pathbuf, dirbuf_generate);
	ProcTags[0].ti_Data = Input();
	ProcTags[1].ti_Data = Output();
	ProcTags[2].ti_Data = (ULONG) stderr;
	SystemTagList(linebuf, ProcTags);
}
