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

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

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

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

$time = time;

mkdir "CPs/$subfolder/$id";

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

$event = param("event");

if ($event eq "makechanges") {
	@targets = param();
	open(DATA,">>CPs/$subfolder/$id/girl.txt");
	MCLOOP: foreach $target (@targets) {
		next MCLOOP if $target eq "event";
		$value = param($target);
		print DATA "$target=$value\n";
		$info{$target} = $value;
print "got here, target is $target and value is $value<BR>\n";
	}
	close(DATA);
	if (param("wanttobe") eq "yes") {
		$godot=1;
	}
}

elsif ($event eq "makesinglechange") {
	@targets = param();
	open(DATA,"CPs/$subfolder/$id/girl.txt") || open(DATA,"CPs/$subfolder/$id/girl.txt");
	@lines = <DATA>;
	close(DATA);
	$target = param("target");
	$value = param($target);
	open(DATA,">CPs/$subfolder/$id/girl.txt");
	foreach (@lines) {
		print DATA unless m/^$target\=/o;
	}
	print DATA "$target=$value\n";
	$info{$target} = $value;
	close(DATA);
}
if ($subfolder eq "d") {
	push @togo, qq~<p><a href=/t/start/upload.htm>add pictures of you and your sub</a>\n~;
	push @togo, qq~<p><a href=/cgi-bin/rare/picup.pl?event=list>remove pictures of you and your sub</a>\n~;
}
else {
	push @togo, qq~<p><a href=/t/start/hyppics.htm>Change pictures for the hypnotist</a>\n~;
	push @togo, qq~<p><a href=/t/start/upload.htm>add pictures of you</a>\n~;
	push @togo, qq~<p><a href=/cgi-bin/rare/picup.pl?event=list>remove pictures of you</a>\n~;
}

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")  || print "There was a problem reading shellpage: $!";
	@lines = <DATA>;
	close(DATA);
	$contentmarker = "<!--content-->";

	foreach (@lines) {
		if (m/$contentmarker/o) {
			print @toprint;
		}
		#elsif (s/^ROOMCOLOR//) {
			#($roomcolor = $info{"roomcolor"}) || ($roomcolor = "#ffffe0");
			#print "background-color:$roomcolor;";
		#}
		else {
			print;
		}
	}
	exit;
}

