#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <dir.h>

#define FALSE 0
#define TRUE !0

/* extern vvvvv */
unsigned char statpage;
unsigned char trace;
unsigned char ansi;
unsigned char local;
unsigned char remote;
unsigned char time_limit;


typedef struct mstart {
		  int section;
		  char subopt[50];
		  char headline[70];
		  fpos_t artbegin;
		  fpos_t artlen;
		  int filler;
} MSTART;

typedef struct mstaridx {
		  char mm[10][40];
		  MSTART art[50];
} MSTARIDX;

int mstar(void);
int readstar(char *msfile);
int starticle(FILE *mp, MSTART *idx);

int main(int argc, char *argv[], char *envp[]) {
	return(makehtml(argv[1]));
}

int makehtml(char *msfile)
{
/* type 101 */
	unsigned char c;
	FILE *mp;
	FILE *mpi;
	FILE *hp;
	char msindex[80];
	char htmfile[80];
	MSTARIDX mstardex;
	unsigned char section = 0;
	unsigned char option = 0;
		strlwr(msfile);
		strcpy(msindex,msfile);
		memcpy(strstr(msindex,".ms"),".idx",5);
		if((mp = fopen(msfile,"rb")) == NULL)
				 return(1);

		/* create/read index and initialize article pointers */
		memset(&mstardex,'\0',sizeof(MSTARIDX));
		if((mpi = fopen(msindex,"rb")) == NULL) {
			if((mpi = fopen(msindex,"wb")) == NULL)
				 return(1);
			printf("\r\nCreating new index file...");
			section = 0;
			option = 0;
			while(!feof(mp)) {
				unsigned char ch;
				ch = fgetc(mp);
				if (ch > 6)
					continue;
				switch(ch) {
					fpos_t artend;
					case 1:  fgets(mstardex.mm[section], 40, mp);
								section++;
								break;
					case 2:  fgets(mstardex.art[option].subopt, 50, mp);
								mstardex.art[option].section = section;
								break;
					case 3:  fgets(mstardex.art[option].headline, 70, mp);
								break;
					case 5:  fgetpos(mp, &mstardex.art[option].artbegin);
								break;
					case 6:  mstardex.art[option].artlen =
									fgetpos(mp, &artend) - mstardex.art[option].artbegin;
								option++;
								break;
					default: break;
				}
			} /* while */
			fwrite(&mstardex,sizeof(MSTARIDX),1,mpi);
		}
		else {
			 unsigned char count;
			 fread(&mstardex,sizeof(MSTARIDX),1,mpi);
			 for(count=0;count<10;count++) {
				 if(strlen(mstardex.mm[count]))
					 section++;
			 }
			 for(count=0;count<50;count++) {
				 if(strlen(mstardex.art[count].subopt))
					 option++;
			 }
		}
		fclose(mpi);

		strcpy(htmfile,msfile);
		memcpy(strstr(htmfile,".ms"),".html",5);
		if((hp = fopen(htmfile,"wb")) == NULL)
				 return(1);

		do {
			char a, b;
			/* let user select one, and call function */
			printf("\r\nMorning Star main menu\r\n\r\n");
			fprintf(hp, "<!--%s-->\r\n",htmfile);
			fprintf(hp, "<!--Morning Star converted by HTMStar-->\r\n");
			fprintf(hp, "<HTML>\r\n<HEAD>\r\n  <TITLE>Morning Star (%s)</TITLE>\r\n", htmfile);
			/* insert other head stuff here */
			fprintf(hp, "</HEAD>\r\n<BODY>\r\n");
			fprintf(hp, "<H1>Morning Star magazine</H1>\r\n(Created from %s)<BR>\r\n", msfile);
			/* H2 heading here */
			/* SECTIONS */
			for (a = 0; a < section; a++) {
				FILE *sp;
				char sectfile[80];
				char sectID[6];
				char sdrive[4], sdir[80], sname[9], sext[4];
				strcpy(sectfile, msfile);
				fnsplit(sectfile,sdrive,sdir,sname,sext);
				sectID[0] = 'a'+ a;
				if(strlen(sname) < 8)
					strncat(sname,sectID,1);
				else
					sname[7] = sectID[0];
				fnmerge(sectfile,"","",sname,".html");

				 if (strlen(mstardex.mm[a])) {
					char entry[80];
					sscanf(mstardex.mm[a],"%[^\r\n]",entry);

					 printf("\n%c %s", 'a'+ a, mstardex.mm[a]);

					 fprintf(hp, "<H3><A HREF=\"%s\">%s</A></H3>\r\n",
						sectfile, entry);	/* mstardex.mm[a]); */

					 /* OPEN SECTION FILE AND CREATE HTM */
					 sp = fopen(sectfile,"wb");
					 fprintf(sp, "<!--%s-->",sectfile);
					 fprintf(sp, "<!--Morning Star converted by HTMStar\r\n-->");
					 fprintf(sp, "<HTML>\r\n<HEAD>\r\n<TITLE>Morning Star %s (%s)</TITLE>\r\n",
							entry, sectfile); /* mstardex.mm[a] */
					 fprintf(sp, "</HEAD>\r\n<BODY>\r\n");
					 fprintf(sp, "<H1>Morning Star %s</H1>\r\n(Created from %s)<BR>\r\n",
							entry, msfile); /* mstardex.mm[a] */
					 /* CONTENTS */
					 fprintf(sp, "<A NAME=\"top\">\r\n");
					 for (b = 0; b < option; b++) {
						if (mstardex.art[b].section == a+1) {
							if (strlen(mstardex.art[b].headline)) {
								char entry[80];
								sscanf(mstardex.art[b].subopt,"%[^\r\n]",entry);
								printf("\n\t(%d) %s", b, mstardex.art[b].subopt);
								fprintf(sp, "<H3><A HREF=\"#%d\">%s</A></H3>\r\n",
									b, entry); /* mstardex.art[b].headline); */
								/* ADD author, etc. */
							}
						}
					 }
					 fprintf(sp, "<H3><A HREF=\"%s\">Return to Main Menu</A></H3>\r\n",
						htmfile);
					 for (b = 0; b < option; b++) {
					 /* ARTICLES */
						if (mstardex.art[b].section == a+1) {
							if (strlen(mstardex.art[b].headline)) {
								printf("\n\t(%d) %s", b, mstardex.art[b].headline);
								fprintf(sp, "\r\n<A NAME=\"%d\">\r\n",  b);
								htmsection(mp, &mstardex.art[b], sp);
								fprintf(sp, "\r\n<BR>\r\n<A HREF=\"#top\">Top of file</A><BR>\r\n");
							}
						}
					 }
					 /* CLOSE HTM SECTION FILE */
					 fprintf(sp, "Morning Star HTML \"%s\" converted from \"%s\" with HTMStar<BR>\r\n",
						sectfile, msfile);
					 fprintf(sp,
						"HTMStar was written by Dana Bell <A HREF=\"mailto:dbell\@gower.net\">(dbell@gower.net)</A>.\r\n");
					 fprintf(sp, "</BODY>\r\n</HTML>");
					 fclose(sp);
				 }
			}
			fprintf(hp, "Morning Star HTML \"%s\" converted from \"%s\" with HTMStar<BR>\r\n",
				htmfile, msfile);
			fprintf(hp,
				"HTMStar was written by Dana Bell <A HREF=\"mailto:dbell\
						@gower.net\">(dbell@gower.net)</A>.\r\n");
			fprintf(hp, "</BODY>\r\n</HTML>");
			fclose(hp);
		} while (FALSE); /* ONLY ONCE */
		fclose(mp);
	return(1);
}


int htmsection(FILE *mp, MSTART *idx, FILE *sp)
{

	int j = 0;
	unsigned char eoa = FALSE;
	#define PAGES 100
	fpos_t page[PAGES];
	char entry[80];
	sscanf(idx->headline,"%[^\r\n]",entry);
	/* printf("\tincluding article \"%s\".", entry );
	return(0); */

	for (j=0;j<PAGES;j++)
		 page[j] = 0L;
	j = 0;

	fseek(mp, idx->artbegin, SEEK_SET);
	printf("Reading %s\r\n", idx->headline);
	fprintf(sp, "<H3>%s</H3>\r\n", entry); /* idx->headline); */
	/* ALSO display author, etc. */
	while(!eoa) {
		int ch;
		unsigned char pcheck = FALSE;
		unsigned int maxlines;
		if (!page[j])
			fgetpos(mp, &page[j]);
		do {
			ch = fgetc(mp);
			if(ch==6) {
				eoa = TRUE;
				break;
			}
		/* putchar(ch); */
			if (ch == '\r')
				continue;
			if (ch == '\n') {
				if (pcheck) {
					fprintf(sp,"<P>\r\n");
					pcheck = FALSE;
				}
				else {
					fprintf(sp,"<BR>\r\n");
					pcheck = TRUE;
				}
				continue;
			}
			pcheck = FALSE;
			if (ch == '<') {
				fprintf(sp,"&lt");
				continue;
			}
			if (ch == '>') {
				fprintf(sp,"&gt");
				continue;
			}
			if (ch == '&') {
				fprintf(sp,"&amp");
				continue;
			}
			fputc(ch, sp);
		} while (TRUE);
		/* get user input */
	}
	return(1);
}


