/*      BBS/DOS ”Ä—p”Å LOG-Cutter      by Gori
                 usage : logc bbs logfile
*/
#include	"logc.h"
#define		VER		"1.30-2806"
#define		BUF		2000
static FILE *deff,*logf;

int ldtocr(char *i,int k)
{
	int a;
	char r;
	a=k;
	for(k--;k>0;k--){
		r=getc(deff);
		if (r=='\n') break;
		*i=r; i++;
		if (feof(deff)) return(-1);
	}
	*i=0x00;
	if (!k) while (getc(deff)!='\n');
	return (a-k);
}

void opendef(char *f)
{
	char *e,*b,*bs;
	bs=(char *)malloc(80);

	strcpy(bs,f);
	chext(bs,"def");
	if ((deff=fopen(bs,"r"))!=NULL) return;

	e=getenv("PATH");
	while (1){
		b=bs;
		while(*e!=';'){
			*b=*e; b++; e++;
			if (!*e) help(10);
		}
		if (*(e-1)=='\\') b--;
		*b=0x00;
		strcat(bs,"\\logc.def");
		if ((deff=fopen(bs,"r"))!=NULL) return;
		e++;
	}
}

int setlddat(const char *bbs)
{
	char *bbt,*bbts;
	int a=0;

	bbts=bbt=(char *)malloc(20);
	*bbt=0x00;
	while (strcmp(bbt,bbs)){
		bbt=bbts; *bbt=0x00;
		while (*bbt!='b'){
			if (ldtocr(bbt,19)==-1) help(221);
			if (isupper(*bbt)) (*bbt)-='A'-'a';
			if (*bbt=='z') return (-1);
		}
		if (*(bbt+1)=='+') a=1;
		for (;*bbt;bbt++) if (*bbt==' ') *bbt=0x00;
		bbt=bbts;
		bbt++;
		while (*bbt!='\t') bbt++;
		while (*bbt=='\t') bbt++;
	}
	return (a);
}

char change_loop(char *p,char *logl)
{
	int i;
	char b=0x00;

	while (*p){
		i=chusyu(p+1,logl);
		if (!i && b<*p) b=*p;
		p+=strlen(p)+1;
	}
	return b;
}

void change(char *p,int a)
{
	short int count;
	int i;
	char logl[260],*ps;

	count=filelength(fileno(deff))/65536+1;
	for (i=count;i>0;i--) putch('.');
	for (i=count;i>0;i--) putch(0x1d);
	count=1;

	while ((i=ldtocr(logl,200))!=-1){
		for (;i>0;count++,i--) if (!count) putch ('@');
		switch(change_loop(p,logl)){
			case START:
				i=1;
				while(i){
					fputs(logl,logf); putc('\n',logf);
					i=ldtocr(logl,200);
					for (;i>0;count++,i--) if (!count) putch ('@');
					ps=p; i=1;
					while(*ps && i){
						if (*ps==END) i=chusyu(ps+1,logl);
						ps+=strlen(ps)+1;
					}
				}
				break;
			case MMINUS:
				i=ldtocr(logl,200);
				for (;i>0;count++,i--) if (!count) putch ('@');
				break;
			case MINUS:
				break;
			case PPLUS:
				fputs(logl,logf); putc('\n',logf);
				i=ldtocr(logl,200);
				for (;i>0;count++,i--) if (!count) putch ('@');
				fputs(logl,logf); putc('\n',logf);
				break;
			case PLUS:
				fputs(logl,logf); putc('\n',logf);
				break;
			default:
				if (a) {
					fputs(logl,logf); putc('\n',logf);
				}
				break;
		}
	}
	putch('@'); putch('\x0d');
}

int main (int argc,char **argv)
{
	char *p;
	char cbuf[80];
	int a,i;

	cputs("\x0d\nBBS/DOS ”Ä—p”Å LOG-CUTTER logc.exe ver."VER"    by Gori");
	cputs("\x0d\n\x0d\n");
	if (argv[0]==NULL) help(102);
	if (argc==1) help(0);
	if (argc==2) help(1);

	opendef(argv[0]);
	a=setlddat(argv[1]);
	if (a==-1) help(11);
	p=(char *)malloc(BUF);
	if (p==NULL) help (100);
	buftoset(p,BUF);
	fclose(deff);

	for (i=2;*argv[i];i++){
		if ((deff=fopen(argv[i],"r"))==NULL) help(12);
		strcpy(cbuf,argv[i]);
		chext(cbuf,"_lg");
		if ((logf=fopen(cbuf,"w"))==NULL) help(13);

		cputs("Changeing \"");
		cputs(argv[1]);
		cputs("\" / ");
		cputs(argv[i]);
		cputs("\" : ");
		change(p,a);

		fclose(deff);
		fclose(logf);
		putspc(6);
		cputs("Normal finished \"");
		cputs(argv[1]);
		cputs("\" / ");
		cputs(argv[i]);
		putspc(40);
		cputs("\x0d\n");
	}
	exit(0);
}

