/**********************************************************************
	"Copyright 1990-2026 Brian MacWhinney. Use is subject to Gnu Public License
	as stated in the attached "gpl.txt" file."
*/


#define CHAT_MODE 1

#include "cu.h"
#include "c_curses.h"

#if !defined(UNX)
#define _main temp_main
#define call temp_call
#define getflag temp_getflag
#define init temp_init
#define usage temp_usage
#endif

#define IS_WIN_MODE FALSE
#include "mul.h"

extern char OverWriteFile;
extern struct tier *headtier;
extern struct tier *defheadtier;

static char trimSp;
static char currentTrimChar= '\0';
static AttTYPE currenTrimAtt=  '\0';

void usage() {
	puts("temp *.cha file with *.txt file in the same folder");
	printf("Usage: temp filename(s)\n");
	mainusage(TRUE);
}

void init(char first) {
	if (first) {
		stout = FALSE;
		OverWriteFile = TRUE;
		FilterTier = 0;
		LocalTierSelect = TRUE;
		onlydata = 1;
		if (defheadtier->nexttier != NULL)
			free(defheadtier->nexttier);
		free(defheadtier);
		defheadtier = NULL;
	} else {
	}
}

void getflag(char *f, char *f1, int *i) {
	f++;
	switch(*f++) {
		default:
			maingetflag(f-2,f1,i);
			break;
	}
}

static void getTrimSp(FILE *fp, char *ch, AttTYPE *att) {
	char lcc;
	int  index;
	int  lc;
	AttTYPE attSp[2];

	lcc = 0;
	if (currentTrimChar != '*') {
		while (1) {
			trimSp = (char)getc_cr(fp, &attSp[0]);
			if (feof(fp))
				break;
			else if (trimSp == '*') {
				if (lcc == '\n')
					break;
			}
			lcc = trimSp;
		}
	}
	while (1) {
		trimSp = (char)getc_cr(fp, &attSp[0]);
		if (feof(fp))
			break;
		else if (trimSp == ':')
			break;
	}
	trimSp = (char)getc_cr(fp, &attSp[0]);
	while (isSpace(trimSp))
		trimSp = (char)getc_cr(fp, &attSp[0]);
	currentTrimChar = trimSp;
	currentatt = 0;
	index = 0;
	lc = index;
	ch[index] = currentTrimChar;
	att[index] = currentatt;
	index++;
	att[index] = att[index-1];
	while (1) {
		if (ch[lc] == '\n')
			tlineno++;
		ch[index] = (char)getc_cr(fp, &att[index]);
		if (feof(fp))
			break;
		else if (isSpeaker(ch[index])) {
			if (ch[lc] == '\n')
				break;
		}
		lc = index;

		if (index >= UTTLINELEN) {
			int i;
			ch[index] = EOS;
			fprintf(stderr,"ERROR. Speaker turn is longer than ");
			fprintf(stderr,"%ld characters.\n",UTTLINELEN);
			fprintf(stderr,"On line: %ld.\n", lineno);
			for (i=0; ch[i]; i++) putc(ch[i],stderr);
			putc('\n',stderr);
			cutt_exit(-1);
			index--;
		} else {
			index++;
			att[index] = att[index-1];
		}
	}
	currentTrimChar = ch[index];
	currentatt = att[index];
	ch[index] = EOS;
}


void call(void) {
	char *ext;
	char trimFName[FNSize];
	FILE *trimFp;

	strcpy(trimFName, wd_dir);
	addFilename2Path(trimFName, oldfname);
	ext = strrchr(trimFName, '.');
	if (ext != NULL)
		*ext = EOS;
	strcat(trimFName, ".kwal.cex");
	if ((trimFp=fopen(trimFName, "r")) == NULL) {
#ifdef UNX
		fprintf(stderr,"\NCAN'T OPEN .KWAL.CEX FILE \"%s\"\n\n", trimFName);
#else
		fprintf(stderr,"\n%c%CCAN'T OPEN .KWAL.CEX FILE \"%s\"%c%c\n\n", ATTMARKER, error_start, trimFName, ATTMARKER, error_end);
#endif
		return;
	}
	currentTrimChar = '\0';
	currenTrimAtt = 0;
	currentatt = 0;
	currentchar = (char)getc_cr(fpin, &currentatt);
	while (getwholeutter()) {
		if (utterance->speaker[0] == '*') {
			getTrimSp(trimFp, utterance->line, utterance->attLine);
			printout(utterance->speaker,utterance->line,utterance->attSp,utterance->attLine,FALSE);
		} else {
			printout(utterance->speaker,utterance->line,utterance->attSp,utterance->attLine,FALSE);
		}
	}
	fclose(trimFp);
}

CLAN_MAIN_RETURN main(int argc, char *argv[]) {
	isWinMode = IS_WIN_MODE;
	chatmode = CHAT_MODE;
	CLAN_PROG_NUM = TEMP;
	OnlydataLimit = 0;
	UttlineEqUtterance = TRUE;
	bmain(argc,argv,NULL);
}
