/*
		dviout.hlp/dvioute.hlp
		
		Rewrite dviout.rtf/dvioute.rtf which are translated from 
		dviout.mhp/dvioute.mhp by MakeHelp 

        Sep. 17, 2002, Written by SHIMA
 */

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

#define	MAXBUF	0x1800
#define	MAXLINE	0x1000

char buf[MAXBUF], buf2[MAXBUF];

#define	helpj	"dviout.rtf"
#define	helpe	"dvioute.rtf"
#define	helpjn	"dviout.tmp"
#define	helpen	"dvioute.tmp"

char *replacej = 
	"{\\rtf1\\ansi \\deff0\n"
	"{\\fonttbl\n"
	"   {\\f0\\fmodern ＭＳ ゴシック;}\n"
	"   {\\f1\\froman Times New Roman;}\n"
	"   {\\f2\\froman Symbol;}\n"
	"   {\\f3\\fswiss Arial;}\n"
	"   {\\f4\\froman ＭＳ 明朝;}\n"
	"   {\\f5\\fmodern ＭＳ ゴシック;}\n"
	"   {\\f6\\froman Tms Rmn;}\n"
	"   {\\f7\\fswiss Helv;}\n"
	"   {\\f8\\fmodern ＭＳ ゴシック;}\n"
	"   {\\f9\\froman ＭＳ 明朝;}}\n";

char *replacee = 
	"{\\rtf1\\ansi \\deff0\n"
	"{\\fonttbl\n"
	"   {\\f0\\fswiss Arial;}\n"
	"   {\\f1\\froman Times New Roman;}\n"
	"   {\\f2\\froman Symbol;}\n"
	"   {\\f3\\fswiss Arial;}\n"
	"   {\\f4\\froman 標準明朝;}\n"
	"   {\\f5\\froman Courier;}\n"
	"   {\\f6\\froman Tms Rmn;}\n"
	"   {\\f7\\fswiss Helv;}\n"
	"   {\\f8\\froman Courier;}\n"
	"   {\\f9\\froman ＭＳ 明朝;}}\n";

void main(int argc, char **argv)
{
	FILE *fp, *fp2;
	int lp, i, j;
	char *pt, *st;

	for(lp = 0; lp < 2; lp++){
		if((fp = fopen(lp?helpj:helpe,"r")) == NULL)
			continue;
		fgets(buf, MAXLINE, fp);
		if(buf[0] != '{' || buf[1] != '\n'){
			fclose(fp);
			continue;
		}
		if((fp2 = fopen(lp?helpjn:helpen,"w")) == NULL){
			fclose(fp);
			continue;
		}else
			fputs(lp?replacej:replacee,fp2);
		for(j = 0; j < 12; j++)
			fgets(buf, MAXLINE, fp);
#if 0
		while(fgets(buf, MAXLINE, fp)){
#else
		while(fgets(buf2, MAXLINE, fp)){
			*buf = 0;
			pt = buf2;
			do{
				if((st = strstr(pt, "\\b0 ")) != NULL)
					*st = 0;
				strcat(buf, pt);
				if(st){
					pt = st + 4;
				}
			}while(st);
#endif
			*buf2 = 0; 
			pt = buf;
			do{
				if((st = strstr(pt, "\\plain")) != NULL)
					*st = 0;
				strcat(buf2, pt);
				if(st){
					strcat(buf2, "\\plain\\fs20");
					pt = st + 6;
				}
			}while(st);
			if(!lp){
				fputs(buf2, fp2);
				continue;
			}
			*buf = 0;
			pt = buf2;
			do{
				if((st = strstr(pt, "\\pard")) != NULL)
					*st = 0;
				strcat(buf, pt);
				if(st){
					strcat(buf, "\\pard\\sl240");
					pt = st + 5;
				}
			}while(st);
			fputs(buf, fp2);
		}
		fclose(fp);
		fclose(fp2);
		if(lp){
//			unlink("dviout.$$$");
//			rename("dviout.rtf","dviout.$$$");
			unlink("dviout.rtf");
			rename("dviout.tmp","dviout.rtf");
			printf("dviout.rtf is arranged!\n");
		}else{
//			unlink("dvioute.$$$");
//			rename("dvioute.rtf","dvioute.$$$");
			unlink("dvioute.rtf");
			rename("dvioute.tmp","dvioute.rtf");
			printf("dvioute.rtf is arranged!\n");
		}
	}
}
