#include <iostream.h>
#include <string.h>
#include <fstream.h>
#include <unistd.h>
#include "cgi.h"

int main()
{
	int i=2;	//skip first chars -- used for cutting from recent.txt
	char in[10000];
	char name[128];
	char parent[128];
	char text[10000];
	char path1[256];
	char path2[256];
	char path3[256];
	char num[10];
	fstream tfile;
	Get(in);
	NextParam(in,name);
	NextParam(in,parent);
	NextParam(in,text);
	
	chdir("/home/httpd/dfcd/memory/");
	char *txt=".txt";
	strcpy(path1,name);
	strcat(path1,txt);
	ofstream n;
	n.open(path1, ios::app);
	n<<endl;
	n<<text;
	n.close();

	char *dat=".dat";
	strcpy(path2,parent);
	strcat(path2,dat);
	
	char testln[256];			//So there are no duplicates in lists
	ifstream file;
	file.open(path2,ios::in);
	while(file.getline(testln,256))
	{
		if(strstr(testln,name)) 
		{
			file.close();
			goto skiplist;
		}
	}
	file.close();
	
	n.open(path2, ios::app);
	n<<endl;
	n<<"<li><a href='"<<name<<".htm'>"<<name<<"</a>";
	n.close();
	
	n.open("recent.txt",ios::app);
	n<<"<li><a href='"<<name<<".htm'>"<<name<<"</a>"<<endl;
	n.close();
	/*char *head;
	head=(char *)malloc(10000);
	file.open("recent.txt",ios::in);
	file.read(head,10000);
	head[file.gcount()]=0;
	file.close();

	while(head[i]!=10 && head[i]!=13 && head[i]!=0){i++;}
	if(head[i]==0) goto dotally;
	if(head[i+1]==10 || head[i+1] ==13) ++i;
	head = head+i+1;	//make new file without first line
	n.open("recent.txt",ios::out);
	n<<head;
	n<<endl;
	n<<"<li><a href='"<<name<<".htm'>"<<name<<"</a>";
	n.close();*/
dotally:	
	tfile.open("tally.txt",ios::in);
	tfile>>i;
	i++;
	tfile.close();
	tfile.open("tally.txt",ios::out);
	sprintf(num,"%i",i);
	tfile<<num<<endl;
	//tfile<<EOF;
	tfile.close();


skiplist:
	char *htm=".htm";
	strcpy(path3,name);
	strcat(path3,htm);
	n.open(path3, ios::out);
	n<<"<html><body bgcolor='black' text='green' link='green' vlink='green' active='green'><font face='Lucida Console' color='green'><!-- #include=\""<<name<<".txt\" --></font></body></html>";
	n.close();



	cout<<"Content-type: text/html\r\n\r\n";
	cout<<"<html><body bgcolor='black' text='green' link='green' vlink='green' active='green'><font face='Lucida Console' color='green' size='2'>File Saved</font></body></html>";
}
