#if 0
gcc dok2html.c -o dok2html
exit
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>

#define INDEXSPALTE	4

struct data
{
	struct data *next;
	char kap[10];
	char name[100];
};

char dok[200],html[200],z[120],l[120];
char olddir[200],kname[200];
char vor[200],jetzt[200],nach[200];
int mode=0,kap=0;
struct data *liste=NULL,*ende=NULL;

void add(char *kap,char *name)
{
	struct data *s;
	if (s=malloc(sizeof(struct data)))
	{
		if (ende)
		{
			ende->next=s;
		}
		else
		{
			liste=s;
		}
		ende=s;
		s->next=NULL;
		strcpy(s->kap,kap);
		strcpy(s->name,name);
	}
}
struct data *finda(char *kap,struct data *s)
{
	s=(!s)?liste:s->next;
	
	while (s)
	{
		if (!strncmp(s->kap,kap,strlen(kap))) return s;
		s=s->next;
	}
	return NULL;
}
struct data *findk(int k,struct data *s)
{
	s=(!s)?liste:s->next;
	
	while (s)
	{
		if ((s->kap[0]-'0')==k) return s;
		s=s->next;
	}
	return NULL;
}

void hhead(FILE *f)
{
	fprintf(f,"<html>\n<head>\n");
	fprintf(f,"<title>Xclasses: %s</title>\n",kname);
	fprintf(f,"</head>\n<body>\n");
	fprintf(f,"<!--#include virtual=\"/~schmitzj/include/body_xclasses_doku.html\"-->\n");
	fprintf(f,"<IMG ALIGN=CENTER SRC=\"../../../pics/logos/xclasses_doku.gif\" ALT=\"Xclasses doku\">\n");
	fprintf(f,"<!--#include virtual=\"/~schmitzj/include/pfad.html\"-->\n",kname);
}
void hend(FILE *f)
{
	static int s=0;
	time_t t;
	struct tm *_tm;
	char dt[30];

	t=time(NULL);
	_tm=localtime(&t);
	strftime(dt,sizeof(dt),"%d-%m-%Y",_tm);
	

	fprintf(f,"<hr><center>\n");
	if (strcmp(jetzt,"index.html"))
	{
		fprintf(f,"<a href=\"%s\"><img src=\"../../../pics/buttons/zurueck.gif\" border=0 alt=\"%s\"></a>\n",vor,vor);
		fprintf(f,"<a href=\"index.html\"><img src=\"../../../pics/buttons/uebersicht.gif\" border=0 alt=\"uebersicht\"></a>\n");
		fprintf(f,"<a href=\"%s\"><img src=\"../../../pics/buttons/naechster.gif\" border=0 alt=\"%s\"></a>\n",nach,nach);
	}
	else
	{
		fprintf(f,"<a href=\"../index.html\"><img src=\"../../../pics/buttons/uebersicht.gif\" border=0 alt=\"uebersicht\"></a>\n");
	}
	fprintf(f,"</center><hr>\n");
	fprintf(f,"<table width=87%%><tr><td>");
	fprintf(f,"<i>made with <b>dok2html</b> "__DATE__" by J&uuml;rgen Schmitz </i>\n");
	fprintf(f,"<td align=right>%s, %02x",dt,++s);
	fprintf(f,"</table></body></html>\n");
}
void split(char *von,char *nr,char *name)
{
	char *to;
	int m=0;
	to=nr;
	while (*von)
	{
		if (!m && *von==' ')
		{
			*to=0;
			to=name;
			m=1;
		}
		else
		{
			*to=*von;
			to++;
		}
		von++;
	}
	*to=0;
}
void umlautcpy(char *to,char *from)
{
	while (*from)
	{
		if (*from=='ä' || *from=='Ä' || *from=='ö' || *from=='Ö'
		 || *from=='ü' || *from=='Ü' || *from=='ß' || *from=='<'
		 || *from=='>')
		{
			*to='&';
			to++;
			switch (*from)
			{
				case 'ß':
					*to='s'; to++;
					*to='z'; to++;
					*to='l'; to++;
					*to='i'; to++;
					*to='g'; to++;
					break;
				case '<':
					*to='l'; to++;
					*to='t'; to++;
					break;
				case '>':
					*to='g'; to++;
					*to='t'; to++;
					break;
				case 'ä':
					*to='a'; to++;
					break;
				case 'Ä':
					*to='A'; to++;
					break;
				case 'ö':
					*to='o'; to++;
					break;
				case 'Ö':
					*to='O'; to++;
					break;
				case 'ü':
					*to='u'; to++;
					break;
				case 'Ü':
					*to='U'; to++;
					break;
			}
			if (*from!='ß' && *from!='<' && *from!='>')
			{
				*to='u'; to++;
				*to='m'; to++;
				*to='l'; to++;
			}
			*to=';';
		}
		else
		{
			*to=*from;
		}
		to++;
		from++;
	}
	*to=0;
}
void tabstart(FILE *out)
{
	fprintf(out,"</tt><table rowspacing=0 rowpadding=0 cellspacing=0 cellpadding=0>\n");
}
void tabtr(FILE *out,struct data *s1,struct data *s2)
{
	char mrk1[30],mrk2[15];
	struct data d;
	d.next=NULL;
	d.kap[0]=0; d.kap[2]=0;
	d.name[0]=0;

	mrk1[0]=0; mrk2[0]=0;	
	if (!s1) s1=&d;
	if (!s1->kap[2])
	{
		strcpy(mrk1,"<font size=5>");
		strcpy(mrk2,"</font>");
	}
	fprintf(out,"<tr><td align=left valign=top width=5%%><b>%s<a href=\"%s.html\">%s</a>%s</b><br><td valign=top width=45%%><a href=\"%s.html\">%s</a>\n",mrk1,s1->kap,s1->kap,mrk2,s1->kap,s1->name);
	if (s2)
	{
		mrk1[0]=0; mrk2[0]=0;
		if (!s2->kap[2])
		{
			strcpy(mrk1,"<font size=5>");
			strcpy(mrk2,"</font>");
		}
		fprintf(out,"    <td><br>\n");
		fprintf(out,"    <td align=left valign=top width=5%%><b>%s<a href=\"%s.html\">%s</a>%s</b><td valign=top width=45%%><a href=\"%s.html\">%s</a>\n",mrk1,s2->kap,s2->kap,mrk2,s2->kap,s2->name);
	}
}
void tabaus(FILE *out,char *k1)
{
	struct data *s1;

	s1=finda(k1,NULL);
	while (s1=finda(k1,s1))
	{
		tabtr(out,s1,NULL);
	}
}
void tabindex(FILE *out,int kmax)
{
	struct data *s1=NULL,*s2=NULL;
	int k1,k2;
	struct data d;
	d.next=NULL;
	d.kap[0]=0;
	d.name[0]=0;

	k1=1;
	k2=INDEXSPALTE+1;

	while (k1<=INDEXSPALTE || k2<=kmax)
	{
		s1=findk(k1,s1);
		s2=findk(k2,s2);
		if (!s1)
		{
			k1++;
			if (k1<=INDEXSPALTE)
			{
				s1=NULL;
			}
			else
			{
				s1=&d;
			}
		}
		if (!s2)
		{
			k2++;
			if (k2<=kmax)
			{
				s2=NULL;
			}
			else
			{
				s2=&d;
			}
		}
		tabtr(out,s1,s2);
	}
}
void tabende(FILE *out)
{
	fprintf(out,"</table>\n");
}

void work(void)
{
	FILE *in,*out=NULL;
	char kn[20],kt[100],kjetzt[20];
	int i,j,kmax=0;

	if (in=fopen(dok,"r"))
	{
		getcwd(olddir,sizeof(olddir));
		if (chdir(html)) exit(10);
		strcpy(kjetzt,"0.");
		strcpy(jetzt,"index.html");
		strcpy(vor,jetzt); strcpy(nach,jetzt);
		strcpy(kname,"Inhaltsverzeichnis");
		if (out=fopen("index.html","w"))
		{
			chmod("index.html",S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
			hhead(out);
			while (!feof(in))
			{
				if (fgets(l,sizeof(l),in))
				{
					i=strlen(l);
					if (l[i-1]==13)
					{
						l[i-1]=0; i--;
					}
					if (l[i-1]==10)
					{
						l[i-1]=0; i--;
					}
					if (l[i-1]==13)
					{
						l[i-1]=0; i--;
					}
					umlautcpy(z,l);

					if (isdigit(z[0]))
					{
						switch (mode)
						{
							case 0: /* start -> index kopf */
								mode=1;
								break;
							case 1: /* index kopf -> index rumpf */
								mode=2;
							case 2: /* index rumpf -?> seiten */
								if ((z[0]-'0')>=kmax)
								{
									if (kmax!=z[0]-'0')
									{
										kmax=z[0]-'0';
									}
									break;
								}
								mode=3;
								fprintf(out,"<h2>Inhaltsverzeichnis</h2>\n");
								tabstart(out);
								tabindex(out,kmax);
								tabende(out);
							case 3: /* seiten */
								split(z,kn,kt);
								strcpy(nach,kn);
								strcat(nach,".html");
								if (out)
								{
									fprintf(out,"</pre>\n");
									if (!strncmp(kjetzt,kn,strlen(kjetzt)))
									{
										tabstart(out);
										tabaus(out,kjetzt);
										tabende(out);
									} 
									hend(out);
									fclose(out);
								}
								strcpy(vor,jetzt);
								strcpy(kjetzt,kn);
								strcpy(jetzt,kn);
								strcat(jetzt,".html");
								if (!(out=fopen(jetzt,"w")))
								{
									printf("Can't write %s\n",jetzt);
									exit(10);
								}
								chmod(jetzt,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
								strcpy(kname,kt);
								hhead(out);
								break;
						}
					}
					switch (mode)
					{
						case 2:
							if (isdigit(z[0]))
							{
								split(z,kn,kt);
								add(kn,kt);
							}
							break;
						case 3:
							if (isdigit(z[0]))
							{
								fprintf(out,"<h2>%s</h2>\n<pre>\n",z);
							}
							else
							{
								fprintf(out,"%s\n",z);
							}
							break;
					}
				}
			}
			if (out)
			{
				strcpy(vor,jetzt);
				strcpy(nach,"index.html");
				fprintf(out,"</pre>\n");
				hend(out);
				fclose(out);
			}
		}
		chdir(olddir);
		fclose(in);
	}
}
void main(int argc,char *argv[])
{
	if (argc>=3)
	{
		strcpy(dok,argv[1]);
		strcpy(html,argv[2]);
		if (html[strlen(html)-1]!='/') strcat(html,"/");
		work();
	}
	else
	{
		printf("Usage: %s <dokfile> <html-dir>\n",argv[0]);
	}
}
