//world.c
//July 14, 2006  7:10am

#include<stdio.h>

#define IMAGE_PATH "images/"
#define TEXT_PATH  "text/"
#define HTML_PATH  "html/"
#define FILE_TYPE  ".html"
//form of the file, beginning and ending
#define IMAGE_HEADER "<img src=\""
#define IMAGE_BOOK_TAILER  "0000.png\" width=400><br>\n"
#define IMAGE_CHAP_TAILER  "00.png\" width=200><br>\n\n"
#define HEAD "<html>\n<head>\n<title>Cherokee New Testament Online</title>\n<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n</head>\n<body>\n<center>\n"
#define TAIL "_.png\" width=200>\n</center>\n</body>\n</html>"

//table construction
#define TABLE_HEADER "<table border=1>\n\t<tr><th><a href=\""
#define TABLE_IMAGE_MIDDLE ".png\">"
#define IMAGE_ENDLINK ".png\" width=400></a><br>\n"
#define TABLE_CHER_HEADER "\t<tr><th>"
#define TABLE_UTF_HEADER "\t<tr><th>"
#define TABLE_UTF_TAILER "\n"
#define TABLE_IMAGE_TAILER ".png\" width=400><br>\n"
#define TABLE_CHER_TAILER "\n"
#define TABLE_ENGL_HEADER "\t<tr><th>"
#define TABLE_TAILER "<br>\n</table><br>\n\n"

//book construction
#define BOOKS 27

//converts int to two ASCII characters (with preceding zeroes)
void ito2c(i,a,b)
	int i;
	char *a,*b;
{
	*b=48+i%10;
	*a=48+(i-i%10)/10;
	return;
}

//strips newline from stdin input
//void stripnl(char *str)
//{	while(strlen(str) && ( (str[strlen(str) - 1] == 13) ||
 //               ( str[strlen(str) - 1] == 10 )))
   //     {	str[strlen(str) - 1] = 0;}
//}


int main()
{
int sizes[28] = {28,16,24,21,28,16,16,13,6,6,4,4,5,3,6,4,3,1,13,5,5,3,5,1,1,1,22,0};
int chap_verse[28][28] = 
{25,23,17,25,48,34,29,34,38,42,30,50,58,36,39,28,27,35,30,34,46,46,39,51,46,75,66,20,
45,28,35,41,43,56,37,38,50,52,33,44,37,72,47,20,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0,
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,0 ,0 ,0,
14,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0,
25,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0,
20,29,22,11,14,17,17,13,21,11,19,17,18,20,8 ,21,18,24,21,15,27,21,0 ,0 ,0 ,0 ,0 ,0,
0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0};

	int book = 0;
	int chapter = 0;
	int verse = 0;
	char a,b;
	char oname[10];	//xxyy.html[NULL]
	char eng_iname[11];//wwxxyy.eng[NULL]
	char cwy_iname[11];//wwxxyy.cwy[NULL]
	char hts_iname[11];//wwxxyy.hts[NULL]
	FILE *outfile;
	FILE *english;
	FILE *htsfile;
	FILE *cherokee;
	char buffer[4095];
	oname[4]=0;

	for(book=0;book<BOOKS;book++)
	{
		printf("Book %d\n",book+1);
		for(chapter=0;chapter<sizes[book];chapter++)
		{
			//build a filename
			ito2c(book+1,&a,&b);
			oname[0]=a;
			oname[1]=b;
			ito2c(chapter+1,&a,&b);
			oname[2]=a;
			oname[3]=b;
			oname[4]='.';
			oname[5]='h';
			oname[6]='t';
			oname[7]='m';
			oname[8]='l';
			oname[9]=0;
			printf("File template is %s\n",oname);
			
			//create the file
			if((outfile = fopen(oname, "w")) == NULL)
			{
				printf("Cannot create HTML output file.\n");
				return(1);
			}

			//start writing data to the file
			fputs(HEAD,outfile);
			fputs(IMAGE_HEADER, outfile);
                        ito2c(book+1,&a,&b);
                        fputc(a,outfile);
                        fputc(b,outfile);
			fputs(IMAGE_BOOK_TAILER,outfile);
			fputs(IMAGE_HEADER, outfile);
			fputc(a,outfile);
			fputc(b,outfile);
                        ito2c(chapter+1,&a,&b);
                        fputc(a,outfile);
                        fputc(b,outfile);
                        fputs(IMAGE_CHAP_TAILER,outfile);
			//printf("  Chapter %d\n",chapter+1);
//Start drawing the verses
			for(verse=0;verse<chap_verse[book][chapter];verse++)
			{
//Start drawing the image			
				fputs(TABLE_HEADER,outfile);
	                        ito2c(book+1,&a,&b);
        	                fputc(a,outfile);
                	        fputc(b,outfile);
	                        ito2c(chapter+1,&a,&b);
        	                fputc(a,outfile);
                	        fputc(b,outfile);
                        	ito2c(verse+1,&a,&b);
	                        fputc(a,outfile);
        	                fputc(b,outfile);
					
				fputs(TABLE_IMAGE_MIDDLE,outfile);
				fputs(IMAGE_HEADER,outfile);

	                        ito2c(book+1,&a,&b);
        	                fputc(a,outfile);
                	        fputc(b,outfile);
	                        ito2c(chapter+1,&a,&b);
        	                fputc(a,outfile);
                	        fputc(b,outfile);
                        	ito2c(verse+1,&a,&b);
	                        fputc(a,outfile);
        	                fputc(b,outfile);

//insert here

				fputs(IMAGE_ENDLINK,outfile);



//Start drawing the english text
				fputs(TABLE_ENGL_HEADER,outfile);
				//build filename

	                        ito2c(book+1,&a,&b);
        	                eng_iname[0]=a;
                	        eng_iname[1]=b;
                        	ito2c(chapter+1,&a,&b);
	                        eng_iname[2]=a;
        	                eng_iname[3]=b;
                	        ito2c(verse+1,&a,&b);
				eng_iname[4]=a;
				eng_iname[5]=b;
				eng_iname[6]='.';
	                        eng_iname[7]='e';
        	                eng_iname[8]='n';
                	        eng_iname[9]='g';
                        	eng_iname[10]=0;
	                        //open the file
        	                if((english = fopen(eng_iname, "r")) == NULL)
                	        {
                        	        //printf("Error Opening File.\n");
                                	//exit(1);
                        	}
				else
				{
					fgets(buffer,sizeof(buffer),english);
					fputs(buffer,outfile);
				}



//start drawing the UTF code
				fputs(TABLE_UTF_HEADER,outfile);
                                //build filename

                                ito2c(book+1,&a,&b);
                                cwy_iname[0]=a;
                                cwy_iname[1]=b;
                                ito2c(chapter+1,&a,&b);
                                cwy_iname[2]=a;
                                cwy_iname[3]=b;
                                ito2c(verse+1,&a,&b);
                                cwy_iname[4]=a;
                                cwy_iname[5]=b;
                                cwy_iname[6]='.';
                                cwy_iname[7]='u';
                                cwy_iname[8]='t';
                                cwy_iname[9]='f';
                                cwy_iname[10]=0;
                                //open the file

                        	printf("File template is %s\n",cwy_iname);
                                if((cherokee = fopen(cwy_iname, "r")) == NULL)
                                {
                                        printf("Error Opening File.\n");
                                        return(1);
					//segmentation fault right here!
                                }
                                else
				{
                                	fgets(buffer,sizeof(buffer),cherokee);
                                	fputs(buffer,outfile);
                                }	
				fputs(TABLE_UTF_TAILER,outfile);

//Start drawing the TXT code
				fputs(TABLE_UTF_HEADER,outfile);
                                //build filename
                                ito2c(book+1,&a,&b);
                                hts_iname[0]=a;
                                hts_iname[1]=b;
                                ito2c(chapter+1,&a,&b);
                                hts_iname[2]=a;
                                hts_iname[3]=b;
                                ito2c(verse+1,&a,&b);
                                hts_iname[4]=a;
                                hts_iname[5]=b;
				hts_iname[6]='.';
                                hts_iname[7]='t';
                                hts_iname[8]='x';
                                hts_iname[9]='t';
                                hts_iname[10]=0;
                                //open the file

				printf("File template is %s\n",hts_iname);
                                if((htsfile = fopen(hts_iname, "r")) == NULL)
                                {
                                        printf("Error Opening File.\n");
                                        return(1);
                                        //segmentation fault right here!
                                }
                                else
                                {
	                                fgets(buffer,sizeof(buffer),htsfile);
                                        fputs(buffer,outfile);
					fclose(htsfile);
                                }

                                fputs(TABLE_UTF_TAILER,outfile);


//Start drawing the HTS code
				fputs(TABLE_CHER_HEADER,outfile);
                                //build filename
                                ito2c(book+1,&a,&b);
                                hts_iname[0]=a;
                                hts_iname[1]=b;
                                ito2c(chapter+1,&a,&b);
                                hts_iname[2]=a;
                                hts_iname[3]=b;
                                ito2c(verse+1,&a,&b);
                                hts_iname[4]=a;
                                hts_iname[5]=b;
				hts_iname[6]='.';
                                hts_iname[7]='h';
                                hts_iname[8]='t';
                                hts_iname[9]='s';
                                hts_iname[10]=0;
                                //open the file

				printf("File template is %s\n",hts_iname);
                                if((htsfile = fopen(hts_iname, "r")) == NULL)
                                {
                                        printf("Error Opening File.\n");
                                        return(1);
                                        //segmentation fault right here!
                                }
                                else
                                {
	                                fgets(buffer,sizeof(buffer),htsfile);
                                        fputs(buffer,outfile);
                                }

                                fputs(TABLE_CHER_TAILER,outfile);





				fclose(cherokee);
				fclose(english);
				fclose(htsfile);
				fputs(TABLE_TAILER,outfile);
			//	printf("    Verse %d\n",verse+1);
			}
			fputs(IMAGE_HEADER,outfile);
			ito2c(book+1,&a,&b);
                        fputc(a,outfile);
                        fputc(b,outfile);

			fputs(TAIL,outfile);
			fclose(outfile);
		}
	}
/*	if((outfile = fopen(oname, "w")) == NULL)
        {
	        printf("Error Opening File.\n");
                exit(1);
        }


	fputs(HEAD,outfile);


        fclose(outfile);
*/
	return;
}
