#!/usr/bin/perl
use CGI qw(:standard);

print <<eND;
Content-type: text/html

eND

$dirlocation = "/home12c/sub007/sc13648-GPVH/";
chdir($dirlocation) || print "Couldn't change directory $!<BR>\n";

$time = time;

$cookieid = "cpid";

$cookie = $ENV{"HTTP_COOKIE"};
$cookie =~ m/$cookieid\=([^;]+)/;
$id = $1;
exit unless $id;

$to = param("id");
$toname = param("name");
$heading = param("heading");

open(DATA,"cp/conversations/gcommlist.txt") || open(DATA,"cp/conversations/gcommlist.txt") || print "Problem with reading gcommlist: $!<BR>\n";
CHECKLOOP: while ($checkid = <DATA>) {
	chomp($checkid);
	if ($checkid eq $id) {
		$fromname = <DATA>;
		chomp($fromname);
		last CHECKLOOP;
	}
	else {
		$temp = <DATA>;
		$temp = <DATA>;
	}
}
close(DATA);


$heading = "$fromname/$toname" unless $heading;
$subg = substr($time,0,3);
open(DATA,">cp/conversations/$subg/f$time.htm") || open(DATA,">cp/conversations/$subg/f$time.htm") || print "couldn't start, subg is $subg: $!<BR>\n";
print DATA qq~<head>\n<META HTTP-EQUIV="cache-control" CONTENT="no-cache">\n<meta name="expires" content="13 July 2004">\n</head>\n<body><table width=800><tr><td>\n~;
print DATA qq~$heading<p>Started by: $fromname<p>To: $toname\n~;
print DATA qq~<!---START--->\n~;
print DATA qq~<!---END---><hr><p>You say (or do)\n~;
print DATA qq~<form method=post action="/conversations/cgi-bin/gcommadd.pl"><BR>\n~;
print DATA qq~<textarea name=msg rows=10 cols=40></textarea>\n~;

print DATA qq~<BR><input type=checkbox size=30 name=selfpicture> Picture of self (if you have pictures)\n~;

print DATA qq~<center><input type=submit value="I said or did this"></center>\n~;
print DATA qq~</form><script>document.forms[0].elements[0].focus()</script>\n~;

print DATA qq~</td></tr></table></body>\n~;
close(DATA);

if ($to eq "all" || $to eq "some") {
	open(DATA,"cp/conversations/gcommlist.txt") || open(DATA,"cp/conversations/gcommlist.txt") || print "Problem with reading gcommlist: $!<BR>\n";
	while ($checkid = <DATA>) {
		chomp($checkid);
		$p = param($checkid);
		push @toids, $checkid if ($to eq "all" || param($checkid) eq "on");
		$temp = <DATA>;
		$temp = <DATA>;
	}
	push @toids, $id if ($to eq "some" && ! grep $_ eq $id, @toids);
	foreach (@toids) {
		open(DATA2,">>cp/conversations/people/$_.txt") || open(DATA2,">>cp/conversations/people/$_.txt") || print "Couldn't add to id file: $!<BR>\n";
		print DATA2 "f$time\n$heading\n$fromname/$to\nnone\n$time\n";
		close(DATA2);
	}
	close(DATA);
}

else {
	open(DATA,">>cp/conversations/people/$id.txt") || open(DATA,">>cp/conversations/people/$id.txt") || print "Couldn't add to id file: $!<BR>\n";
	print DATA "f$time\n$heading\n$fromname/$toname\nnone\n$time\n";
	close(DATA);
	if ($to ne $id) {

		open(DATA,">>cp/conversations/people/$to.txt") || open(DATA,">>cp/conversations/people/$to.txt") || print "Couldn't add to id file: $!<BR>\n";
		print DATA "f$time\n$heading\n$fromname/$toname\nnone\n$time\n";
		close(DATA);
	}
}

open(DATA,">cp/conversations/convs/f$time.txt") || open(DATA,">cp/conversations/convs/f$time.txt") || print "Couldn't write conv file: $!<BR>\n";
#####the following if to all is added 7/10
if ($to eq "all" || $to eq "some") {
	foreach (@toids) {
		print DATA "$_\n";
	}
}
else {
	print DATA "$id\n";
	print DATA "$to\n" unless $to eq $id;
}
close(DATA);
push @togo, qq~<META HTTP-EQUIV="Pragma" CONTENT="no-cache">\n~;
push @togo, "starting a conversation between $toname and $fromname";
push @togo, "<BR><BR><a href=/conversations/$subg/f$time.htm>start message</a>";
push @togo, "<BR><BR><a href=/conversations/cgi-bin/gcommeditmenu.pl?msgid=f$time>change heading</a>";
push @togo, "<BR><BR><a href=http://makeapersonality.com/cgi-bin/main.pl>return</a>";
printonblankerpage(@togo);

sub printonblankerpage {
	@toprint = @_;
	chdir("/home12c/sub007/sc13648-GPVH/cp");
	open(DATA,"cgi-bin/shells/startshell.htm") || open(DATA,"cgi-bin/shells/startshell.htm");
	@lines = <DATA>;
	close(DATA);
	$contentmarker = "<!--content-->";

	foreach (@lines) {
		if (m/$contentmarker/o) {
			print @toprint;
		}
		else {
			print;
		}
	}
	exit;
}

