#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <exec/ports.h>
#include <graphics/gfx.h>
#include <exec/semaphores.h>
#include <graphics/rastport.h>
#include <cnet/empty.h>
#include <cnet/cnet.h>

FILE	*fp;

char	level[4] = "99",
		area[5][17],
		command[15][5][17],
		helptext[15][5][76],
		line[15][5][100],
		key[3],
		args[10],
		file[30] = "pfiles:helper/";
short	i = 0,
		j = 0,
		x = 0,
		y = 0,
		maxges,
		maxx,
		max[6];

main(int argc,char **argv)
{
	PreMain(argc,argv);        /*** Let it here, dute! ***/

	SetDoing("Helper");

	/* AUSGABE DES RAHMENS */

	ReadFile("pfiles:helper/helper.ansi",0);

	/* EINLESEN DES HELPFILES */

	strcat(file,argv[2]);

	if (fp=fopen(file,"r"))
	{
		while (fgets(area[i],sizeof(area[0]),fp))
		{
			while (fgets(level,sizeof(level),fp)&&(atoi(level)<32))
			{
				if (z->user1.Access>=(atoi(level)))
				{
					fgets(command[j][i],sizeof(command[0][0]),fp);
					fgets(helptext[j][i],sizeof(helptext[0][0]),fp);

					x=73;
					do
					{
						helptext[j][i][x]=32;
						x--;
					}
					while (helptext[j][i][x]<11);
					helptext[j][i][74]=0;

					x=13;
					do
					{
						command[j][i][x]=32;
						x--;
					}
					while (command[j][i][x]<11);
					command[j][i][14]=10;
					command[j][i][15]=0;

					j++;
				}
				else
				{
					fgets(line[0][0],sizeof(command[0][0]),fp);
					fgets(line[0][0],sizeof(helptext[0][0]),fp);
				}
			}
			max[i]=j-1;
			i++;
			j=0;
		}
		maxx=(i-1);
		fclose(fp);
	}
	else
		goto hell;



	for(i=0;i<(maxx+1);i++)
	{
		PutText("cb");
		sprintf(line[0][0],"[4;%dH%s",(i*15+2),area[i]);
		PutText(line[0][0]);
		PutText("c3");
	}

	for(i=0;i<maxx+1;i++)
	{
		for(j=0;j<(max[i]+1);j++)
		{
			sprintf(line[j][i],"[%d;%dH%s",(6+j),(i*15+2),command[j][i]);
			PutText(line[j][i]);
		}
	}

	x=0;
	y=0;

	PutText("[44m");
	PutText(line[0][0]);
	PutText("[22;2H[31;40m");
	PutText(helptext[0][0]);

	key[0]=0;
	while (key[0]!=32 && key[0]!=13)
	{
		key[0]=CNetGetKey();
key0:
		/* RAUF UND RUNTER */

		if (key[0]==27)
		{
			key[1]=CNetGetKey();
			if (key[1]==91)
			{
				key[2]=CNetGetKey();
				if (key[2]==66 && y<max[x])
				{
					PutText("[33;40m");
					PutText(line[y][x]);
					y+=1;
					PutText("[33;44m");
					PutText(line[y][x]);
					PutText("[22;2H[31;40m");
					PutText(helptext[y][x]);
				}
				if (key[2]==68 && x>0)
				{
					PutText("[33;40m");
					PutText(line[y][x]);
					x-=1;
					if (y>max[x])
						y=max[x];
					PutText("[33;44m");
					PutText(line[y][x]);
					PutText("[22;2H[31;40m");
					PutText(helptext[y][x]);
				}
				if (key[2]==65 && y>0)
				{
					PutText("[33;40m");
					PutText(line[y][x]);
					y-=1;
					PutText("[33;44m");
					PutText(line[y][x]);
					PutText("[22;2H[31;40m");
					PutText(helptext[y][x]);
				}
				if (key[2]==67 && x<maxx)
				{
					PutText("[33;40m");
					PutText(line[y][x]);
					x+=1;
					if (y>max[x])
						y=max[x];
					PutText("[33;44m");
					PutText(line[y][x]);
					PutText("[22;2H[31;40m");
					PutText(helptext[y][x]);
				}
			}
			else
			{
				key[0]=key[1];
				goto key0;
			}
		}
		if (key[0]==13)
		{
			i=16;
			while (command[y][x][i]<33 || command[y][x][i]>96)
				i--;
			command[y][x][(i+1)]=0;
			strcpy(z->CStack,command[y][x]);
		}
	}


hell:
	PutText("[24;1H[ pF1");
	PostMain();          /*** Must be here ***/
}