/******************************************************************/
/* Program[i]: m[i].c							  								*/
/* Created By: Guy "Smiley" McArthur (smiley@seds.lpl.arizona.edu)*/
/* Purpose: Reads in Messier Catalog, prints out a HTML file for  */
/*          each object						                           */
/******************************************************************/
#include<stdio.h>
int main(void);

int i = 0, t = 0;
char file[11];
FILE *fp; /* m???.html */
FILE *in; /* data.txt  */
struct MESSIER
	{
	 int 		number;
	 int 		ngc;
	 char 	con[3];
	 int 		type;
	 char    type_link[12];
	 int 		alpha;
	 float 	a_min;
	 int 		delta;
	 int 		d_min;
	 float 	mag;
	 float 	ly;
	 char 	description[30];
	} m[111];

/********************************************************************/
int main(void)
{
char data[80];
char link1[10];
char link2[10];
char link3[10];

   if((in  = fopen("data.txt", "r")) == NULL) return(0);

   while(!feof(in))
   {
	++i;
	fgets(data, 81, in);

	sscanf(data, "%d,%d,%3c,%d,%d,%f,%d,%d,%f,%f",&m[i].number,&m[i].ngc,m[i].con,&m[i].type,&m[i].alpha,&m[i].a_min,&m[i].delta,&m[i].d_min,&m[i].mag,&m[i].ly);


	switch(m[i].type)
		{
		case(1):
		sprintf(m[i].description,"Open Cluster");
		sprintf(m[i].type_link, "cluster.html");
		break;
		case(2):
		sprintf(m[i].description,"Globular Cluster");
		sprintf(m[i].type_link, "cluster.html");
		break;
		case(3):
		sprintf(m[i].description,"Planetary Nebula");
		sprintf(m[i].type_link, "nebula.html");
		break;
		case(4):
		sprintf(m[i].description,"Diffuse Nebula");
		sprintf(m[i].type_link, "nebula.html");
		break;
		case(5):
		sprintf(m[i].description,"Spiral Galaxy");
		sprintf(m[i].type_link, "galaxy.html");
		break;
		case(6):
		sprintf(m[i].description,"Elliptical Cluster");
		sprintf(m[i].type_link, "galaxy.html");
		break;
		case(7):
		sprintf(m[i].description, "Irregular Galaxy");
		sprintf(m[i].type_link, "galaxy.html");
		break;
		case(8):
		sprintf(m[i].description,"Double Star");
		sprintf(m[i].type_link, "cluster.html");
		break;
		case(9):
		sprintf(m[i].description,"Supernova Remnant");
		sprintf(m[i].type_link, "nebula.html");
		break;
	}/* end switch */

	}/* end while, end pre-loop */

	for(i=1 ; i<111 ; i++)   /* Main for loop *********************************/
	{
		/* determine next galaxy, next nebula, next cluster*/
		for(t = i+1; t<112 ; ++t)
			{
			/* Find Next Cluster */
			if(m[t].type == 1 || m[t].type == 2 || m[t].type == 8)
				{
				sprintf(link1, "m%03i.html", t);
				break;
				}
			}
		for(t = i+1; t<112 ; ++t)
			{
			/* Find Next Nebula */			if(m[t].type == 3 || m[t].type == 4 || m[t].type == 9)
				{
				sprintf(link2, "m%03i.html", t);
				break;
				}
			}
		for(t = i+1; t<112 ; ++t)
			{
			/* Find Next Galaxy */			if(m[t].type == 5 || m[t].type == 6 || m[t].type == 7)
				{
				sprintf(link3, "m%03i.html", t);
				break;
				}
			}


	/* Determine File name */
	sprintf(file, "m%03i.html", i);
	if((fp = fopen(file, "wt")) == NULL)
		{
		printf("Can't open file %s", file);
		return(0);
		}
	printf("Writing %s\n", file);
	/* Write HTML */
	fprintf(fp, "<HEAD><TITLE>Messier Object %i</TITLE></HEAD>\n\n\n", i);
	fprintf(fp, "<BODY>\n");
	fprintf(fp, "<H1>\n\t<a href=\"../large/m%i.gif\">", i);
	fprintf(fp, "<IMG ALT=\"m%i.gif\"  SRC=\"../small/m%i.gif\" BORDER=0></a>\tM %i\n</H1>\n\n", i, i, i);

	fprintf(fp, "<i>\t<A HREF=\"../%s\">%s</A>\tM%i (NGC %i)</i>\n<p>\n\n", m[i].type_link, m[i].description, i, m[i].ngc);

	fprintf(fp, "->Type Text Description and Information HERE<-\n<p>\n\n");
	fprintf(fp, "<TABLE BORDER=3>\n");
	fprintf(fp, "\t<TR>\n");
	fprintf(fp, "\t\t<TH ALIGN=LEFT>Right ascension\t</TH>\n");
	fprintf(fp, "\t\t<TD ALIGN=RIGHT>%02i : %02.1f\t(hours : minutes)</TD>\n\t</TR>\n", m[i].alpha, m[i].a_min);
	fprintf(fp, "\t<TR>\n");
	fprintf(fp, "\t\t<TH ALIGN=LEFT>Declination\t</TH>\n");
	fprintf(fp, "\t\t<TD ALIGN=RIGHT>%+03i : %02i\t(degrees : minutes)</TD>\n\t</TR>\n", m[i].delta, m[i].d_min);
	fprintf(fp, "\t<TR>\n");
	fprintf(fp, "\t\t<TH ALIGN=LEFT>Distance\t</TH>\t\n");
	fprintf(fp, "\t\t<TD ALIGN=CENTER>%.1f\t(light-years*10^3)</TD>\n", m[i].ly);
	fprintf(fp, "\t</TR>\n");
	fprintf(fp, "\t<TR>\n");
	fprintf(fp, "\t\t<TH ALIGN=LEFT>Visual magnitude</TH>\n");
	fprintf(fp, "\t\t<TD ALIGN=CENTER>\t%.1f\t</TD>\n", m[i].mag);
	fprintf(fp, "\t</TR>\n");
	fprintf(fp, "</TABLE>\n\n<P>\n\n");

	fprintf(fp, "<a href=\"http://seds.lpl.arizona.edu/\">");
	fprintf(fp, "<img alt=\"SEDS\" src=\"../seds.gif\"></a>\n");

	fprintf(fp, "<a href=\"../Messier.html\">");
	fprintf(fp, "\t<img alt=\"Home\" src=\"../messier.gif\"></a>\n");

	fprintf(fp, "<a href=\"m%03i.html\">", i-1);
	fprintf(fp, "<img alt=\"Backward\"  src=\"../backward.gif\"></a>\n");

	fprintf(fp, "<a href=\"m%03i.html\">", i+1);
	fprintf(fp, "<img alt=\"Forward\"  src=\"../forward.gif\"></a>\n");

	fprintf(fp, "<a href=\"%s\"><img alt=\"Next Cluster\"  src=\"../globular.ico.gif\"></a>\n", link1);

	fprintf(fp, "<a href=\"%s\"><img alt=\"Next Nebula\" src=\"../nebula.ico.gif\"></a>\n", link2);

	fprintf(fp, "<a href=\"%s\"><img alt=\"Next Galaxy\" src=\"../galaxy.ico.gif\"></a>\n", link3);

	fprintf(fp, "\n\n\n\n");
	fprintf(fp, "<!created by Guy Smiley>\n");
	fprintf(fp, "<!smiley@seds.lpl.arizona.edu>\n");
	fprintf(fp, "</BODY>");
	fclose(fp);
	}/* end big for  **********************************************************/
fclose(in);
return(1);
}