//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 "\nᎠᏯᏙᎸᎢ "
#define TAIL "_.png\" width=200>\n</center>\n</body>\n</html>"
#define AFTER_CHAPTER "\n\n"
//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 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 "\n"

//book construction
#define BOOKS 27
#define KITUWA 1
#define CHEROKEE_HTML 2

//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,
80,52,38,44,39,49,50,56,62,42,54,59,35,35,32,31,37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,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 MODE = KITUWA;
	int book = 0;
	int chapter = 0;
	int verse = 0;
	char a,b;
	char oname[10];	//book.utf[NULL]
			//book.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);

		//build a filename
		ito2c(book+1,&a,&b);
		oname[0]=a;
		oname[1]=b;
		oname[2]='.';
		oname[3]='u';
		oname[4]='t';
		oname[5]='f';
		oname[6]=0;
		printf("File template is %s\n",oname);
		
		//create the file
		if((outfile = fopen(oname, "w")) == NULL)
		{
			printf("Cannot create UTF output file.\n");
			return(1);
		}

//This is where the Book heading is inserted.  bb0000.utf
                ito2c(book+1,&a,&b);
                cwy_iname[0]=a;
                cwy_iname[1]=b;
                cwy_iname[2]='0';
                cwy_iname[3]='0';
                cwy_iname[4]='0';
                cwy_iname[5]='0';
                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);
			fputc('\n',outfile);

                }	
		fclose(cherokee);

//end of printing book title.  Input file closed

		for(chapter=0;chapter<sizes[book];chapter++)
		{

			//start writing data to the file
			fputs(HEAD,outfile);  //"Chapter "

			//Be sure to include grammatical case handling for when there is a single chapter in a book.

			ito2c(chapter+1,&a,&b);
			if(a!='0')
                        {
				fputc(a,outfile);
			}
			fputc(b,outfile);
			fputs(AFTER_CHAPTER,outfile);  // \n\n
			//printf("  Chapter %d\n",chapter+1);

			//Start drawing the verses
			for(verse=0;verse<chap_verse[book][chapter];verse++)
			{
                                //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
				{
                      			//Insert verse number
					if(a!='0')
					{
						fputc(a,outfile);
					}
					else
					{
						fputc(32,outfile);
					}
					fputc(b,outfile);
					fputc(32,outfile);
					
					//Insert Verse
			          	fgets(buffer,sizeof(buffer),cherokee);
                                	fputs(buffer,outfile);
                                }	
				fputs(UTF_TAILER,outfile); // \n
				fclose(cherokee);
				
			//	printf("    Verse %d\n",verse+1);
			}
		}
		fclose(outfile);
	}
	return;
}
