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

print "Content-type: text/html\n\n" unless $ARGV[0] eq "bh";

chdir("/home12c/sub007/sc13648-GPVH/");

$time = time();

$cookie = $ENV{"HTTP_COOKIE"};
$id = $1 if $cookie =~ m/cpid\=([^;,]+)/;
$subfolder = substr($id,0,1);

printpage("t/start/login.htm") unless $id;

if (open(DATA,"CPs/$subfolder/$id/girl.txt") || open(DATA,"CPs/$subfolder/$id/girl.txt")) {
	while ($_ = <DATA>) {
		$info{$1} = $2 if m/^([^=]+)\=(.*)/;
	}
	close(DATA);
}

$name = $info{"name"};


#start printing things
push @togo, qq~<table width=100%><tr><td>~;

$picsource = $info{"picsource"};
$picsource = "$subfolder/$id" if $picsource eq "own";

$name = $info{"name"};
push @togo, "<p>Name: <span style=font-size:16pt>$name</span>";
($gender = $info{"gender"}) || ($gender = "unknown");
push @togo, "<br>Gender: <span style=font-size:16pt>$gender</span>";
$age = $info{"age"};
push @togo, "<br>Age: <span style=font-size:16pt>$age</span>";



#normal mail

open(DATA,"cp/conversations/people/$id.txt");
push @togo, qq~<p><table style=;font-size:12pt border=3><tr><td colspan=5><h4>HouseMail</h4></td></tr>~;
push @togo, qq~<tr><td><a target=_top href=/conversations/cgi-bin/gcommmenu.pl>Start new housemail</a></td><td colspan=2></td><td></td><td></td></tr>~;
push @togo, qq~<tr><td>Heading</td><td>To/From</td><td>Last</td><td>Time</td><td>     </td></tr>\n~;
$i=0;
while ($msgid = <DATA>) {
	$heading7[$i] = <DATA>;
	$tofrom7[$i] = <DATA>;
	$lastwho7[$i] = <DATA>;
	$lasttime = <DATA>;
	$subg7[$i] = substr($msgid,1,3);
	chomp($msgid);
	$msgid7[$i] = $msgid;
	chomp($tofrom[$i]);
	chomp($lastwho[$i]);
	chomp($lasttime);
	chomp($heading[$i]);
	$tm7[$i] = getdate($lasttime);
	$lasttime[$i] = $lasttime;
	$o7[$i] = $i;
	$i++;
}
$nmessages = $i;
@o7 = sort mysort @o7;
$start = $nmessages - 10;
$start = 0 if $start < 0;
for ($i=$start;$i < $nmessages; $i++) {
	push @togo, qq~<tr><td><a target=_top href=/conversations/$subg7[$o7[$i]]/$msgid7[$o7[$i]].htm>$heading7[$o7[$i]]</a></td><td>$tofrom7[$o7[$i]]</td><td>$lastwho7[$o7[$i]]</td><td>$tm7[$o7[$i]]</td><td><a target=_top href=/conversations/cgi-bin/gcommeditmenu.pl?msgid=$msgid7[$o7[$i]]~;
	if ($id eq $roomid) {
		push @togo, qq~;roompartial=yes><span style=font-size:8pt>Manage<BR><br></span></a>~;
		push @togo, qq~</td></tr>~;
	}
	else {
		push @togo, qq~><span style=font-size:8pt>Manage<BR><br></span></a>~;
		push @togo, qq~<a target=_top href=/conversations/cgi-bin/gcommdiscard.pl?msgid=$msgid7[$o7[$i]];event=hide style=font-size:8pt>Hide</a></td></tr>~;
	}

}
push @togo, qq~</table>~;

push @togo, qq~<p><a href=/conversations/cgi-bin/gcommenroll.pl?id=$id>Enroll in New HouseMail</a>~ unless $nmessages > 0;

printonblankerpage(@togo);



sub mysort {
	$g = $lasttime[$a] <=> $lasttime[$b];
	return $g;
}


##############################


sub basicheader {
	print <<eND;
Content-type: text/html

eND
	return;
}


###############################

sub gracefulend {
	$mssg = $_[0];
	print <<eND;
<BR>There was a problem: $mssg.
</body>
</html>
eND
	exit;
}

sub printpage {
	my ($fl,$insert) = @_;
	chdir("/home12c/sub007/sc13648-GPVH/jacksharem.com");
	open(DATA,"$fl") || open(DATA,"$fl") || gracefulend("There was a problem reading $fl");
	@lines = <DATA>;
	close(DATA);
	foreach (@lines) {
		s/INSERT/$insert/o;
		print;
	}
	exit;
}


sub printonblankerpage {
	@toprint = @_;
	chdir("/home12c/sub007/sc13648-GPVH/cp");
	open(DATA,"cgi-bin/shells/startshell.htm") || open(DATA,"cgi-bin/shells/startshell.htm")  || print "There was a problem reading shellpage: $!";

	@lines = <DATA>;
	close(DATA);
	$contentmarker = "<!--content-->";
	foreach (@lines) {
		if (m/$contentmarker/o) {
			print @toprint;
		}
		elsif (m/^body/) {
			$bgr = $roominfo{"bgr"};
			$bgg = $roominfo{"bgg"};
			$bgb = $roominfo{"bgb"};
			s/nd\-color\:\#ffff80/nd\-color\:rgb\($bgr,$bgg,$bgb\)/ if defined($roominfo{"bgr"});
			print;
		}
		else {
			print;
		}
	}
	exit;
}

sub getdate {
	my $t = $_[0];
	#$t = $t * 1000 if $t < 1000000000000;
	($sec,$min,$hour,$mday,$mon,$year) = localtime($t + 3 * 3600);
	$year += 1900;
	@months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
	$min = "0$min" if $min < 10;
	if ($hour > 12) {
		$hour = $hour - 12;
		$m = "pm";
	}
	elsif ($hour eq "12") {
		$m = "pm";
	}
	elsif ($hour eq "0") {
		$m = "am";
		$hour = "12";
	}
	else {
		$m = "am";
	}
	return "$hour:$min<span style=font-size:10>$m</span> $months[$mon] $mday";
}
