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

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

eND

$cookieid = "cpid";
$cookie = $ENV{"HTTP_COOKIE"};

$cookie =~ m/$cookieid\=([^;]+)/;
$id = $1;

$time = time;

$id = $time unless $id;

$subfolder = substr($id,0,1);

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

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

$event = param("event");
$file = param("file");


if ($event eq "change") {
	$target = param("msg");
	@newphrases = split /(\n\r\n\m)/, $target;
	open(DATA,">CPs/$subfolder/$id/$file") || print "Couldn't open: $!<BR>\n";
	foreach $newphrase (@newphrases) {
		if ($newphrase =~ m/\S/) {
			if ($newphrase =~ s/\bI /you /g) {
				$newphrase =~ s/ am / are /g
			}
			$newphrase =~ s/\bmine /yours /g;
			$newphrase =~ s/\bmy /your /g;
			$newphrase =~ s/\bmyself\b/yourself/g;
			$newphrase =~ s/\me\b/you/g;
			print DATA "$newphrase";
			push @phrases, "$newphrase";
		}
	}
	close(DATA);
	print "<font color=red>Those changes were made. Here is your new list.</font><br><BR>\n";
	$event = "edit";
}

else {
	open(DATA,"CPs/$subfolder/$id/$file");
	@phrases = <DATA>;
	close(DATA);
}

if ($event eq "edit") {
	push @togo, qq~<p>To add phrases, add it or insert it on its own line. You can also change phrases. To delete a phrase, simply erase it. When you are done, click on "Change to These Phrases"~;
	push @togo, qq~<p>These are your phrases for your general training. For example, to get yourself to be helpful, you could have phrases like "being helpful" or "helping other people" or "helping someone in need".~ if $file eq "gentrain.txt";
	push @togo, qq~<p>Here, you describe what you look like. Like "short" or "long nails" or "red hair" or "muscular"~ if $file eq "looks.txt";
	push @togo, qq~<p>These are things that your personality is not good at. Even if your core self or other personalities are good at. In other words, you are listing disabilities here.~ if $file eq "limitations.txt";
	push @togo, qq~<p>These are things that you don't want to have happen to you. Like losing money. They are used in the hypnosis to make you afraid of things.~ if $file eq "limitations.txt";
	push @togo, qq~<p>These are things that your character should fear.~ if $file eq "fears.txt";
	push @togo, qq~<p>Your entries should begin with verbs ending in ing, like running or having or going or being~ if $file eq "dislikes.txt" || $file eq "likes.txt" || $file eq "limitations.txt" || $file eq "gentrain.txt" || $file eq "fears.txt";

	push @togo, qq~<form method=post action="/cgi-bin/rare/editphrasesall.pl"><center><textarea name=msg rows=15 cols=60>~;
	foreach $phrase (@phrases) {
		push @togo, qq~$phrase~;
	}

	push @togo, qq~</textarea><br><input type=submit value="Change to These Phrases"></center><input type=hidden name=event value=change><input type=hidden name=file value=$file></form>~;
	push @togo, qq~<p><a href=/cgi-bin/main.pl>Return</a>~;
	printonblankerpage(@togo);
}

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;
		}
		else {
			print;
		}
	}
	exit;
}


