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

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

$docroot = $ENV{"DOCUMENT_ROOT"};
$docroot =~ s/\/cp//;

print "docroot is $docroot";

chdir($docroot);
#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"};

open(DATA,">CPs/$subfolder/$id/lasttime.txt");
print DATA "$time";
close(DATA);

$new = "yes" if substr($time,0,4) - substr($id,0,4) < 3;

#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>";
push @togo, "<br><span style=font-size:16pt>Dominant</span>" if $subfolder="d";


if (opendir(DATA,"$docroot/cp/images/people/$subfolder/$id") ) {
print "got here<BR>\n";
	@filenms = readdir(DATA);
	closedir(DATA);
	@filenms = grep m/\w\./, @filenms;
	$r = int(rand(@filenms));
	push @togo, qq~<img src=/images/people/$subfolder/$id/$filenms[$r] align=right class=picture title="looking in your mirror">~ if @filenms;
}
else {
	$haspics = "no";
}

push @togo, "<BR><span style=font-size:14>&nbsp;&nbsp;&nbsp;You are having your period</span><BR>\n" if $time < $info{"endprd"};

###todo
push @togo, "<p><span style=font-size:16pt>Things to Do:</span>";

push @togo, "<BR><a href=/cgi-bin/rare2/training.pl style=padding-left:10px;font-size:14pt>Training</a>";
push @togo, qq~<br><a href=/cgi-bin/chrv.pl style=padding-left:10px;font-size:14pt>Hypnosis (general)</a>~;
push @togo, "<BR><a href=/cgi-bin/rare2/mail.pl style=padding-left:10px;font-size:14pt>Mail</a>";
push @togo, "<BR><a href=/t/start/infocenter.htm style=padding-left:10px;font-size:14pt>Information Center</a>";
push @togo, "<BR><a href=/t/start/sfbinfo.htm style=padding-left:10px;font-size:14pt>SfB information</a>" if $info{"sfbrole"};
push @togo, "<BR><a href=/t/femsl/info.htm style=padding-left:10px;font-size:14pt>Information from Christina</a>" if substr($id,0,1) eq "f";



###information

push @togo, "<BR><BR><span style=font-size:16pt>Information:</span>";
push @togo, "<BR><a href=/cgi-bin/rare/aboutyou3a.pl style=padding-left:10px;font-size:14pt>Basic Information about you</a>";
push @togo, "<BR><a href=/cgi-bin/rare/aboutyou2.pl style=padding-left:10px;font-size:14pt>Picture choices and uploading</a>";
push @togo, "<BR><a href=/cgi-bin/rare/mainabout.pl style=padding-left:10px;font-size:14pt>Personalize Your Training</a>";



###NICE###push @togo, "<br><a href=/cgi-bin/newbin/girlroom5.pl?event=options;id=$id;name=$name target=_top>More options</a>";
###NICE###push @togo, "<br><a href=/cgi-bin/newbin/girlroom5.pl?event=getmenu;id=$id;name=$name target=_top>Get something</a>";
push @togo, "<BR><br><a href=/t/start/logout.htm target=_parent>logout</a> (but remember your name and password";



push @togo, " </td></tr></table>";

printonblankerpage(@togo);
exit;



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




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

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

sub printpage {
	my ($fl,$insert) = @_;
	chdir("$docroot/cp");
	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("$docroot/cp");
	open(DATA,"cgi-bin/shells/shellgirlroom.htm") || open(DATA,"cgi-bin/shells/shellgirlroom.htm")  || gracefulend("There was a problem reading body: $!") if $iframe ne "yes";

	@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 gettime2 {
	my $t = $_[0];
	$r = int($t / (24 * 60 * 60) );
	if ($r > 10) {
		$rx = "$r days";
	}
	elsif ($r > 1) {
		$rx = "$r days";
		$h = int($t/3600) - 24 * $r;
		$rx .= " and $h hour";
		$rx .= "s" if $h != 1;
	}
	elsif ($r > 0) {
		$rx = "one day";
		$h = int($t/3600) - 24;
		$rx .= " and $h hour";
		$rx .= "s" if $h != 1;
	}
	else {
		$r = int($t / (60 * 60) );
		if ($r > 8) {
			$rx = "$r hours";
		}
		elsif ($r > 1) {
			$rx = "$r hours";
			$h = int($t/60) - 60 * $r;
			$rx .= " and $h minute";
			$rx .= "s" if $h != 1;
		}
		elsif ($r > 0) {
			$rx = "one hour";
			$h = int($t/60) - 60;
			$rx .= " and $h minute";
			$rx .= "s" if $h != 1;
		}
		else {
			$r = int($t /60 );
			$rx = "$r minute";
			$rx .= "s" if $r != 1;
		}
	}
	return $rx;
}







