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

basicheader();

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

$time = time();

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

###does this work?
$id = param("doing") if param("doing");

#system "/home12c/sub007/sc13648-GPVH/cp/cgi-bin/rare/recordactivity.pl", $id, "training";

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

$phr = param("phr");

$fl = param("fl");
$tease = param("tease");
$theme = param("theme");
$org = param("org");
$oldtl = $info{"traininglevel"};
$type = 1;
$need = "yes";

$name = $info{"name"};

printpage("index.htm") unless $fl;

$newfile = $fl;

$fl =~ s/\.txt//;
$shortprefix = $fl;
$prefix = "t/$fl";

####  calculate which instruction to give. How hard can that be?
$need = param("exc");
($touch = param("type")) || ($touch = 1);



$instruction = 1;


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

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

##### get the page to put data in
if ($fl =~ m/jack/i) {
	open(DATA,"cgi-bin/shells/scriptshellnocache.htm") || open(DATA,"cgi-bin/shells/scriptshellnocache.htm") || gracefulend(" reading scriptshell");
}
else {
	open(DATA,"cgi-bin/shells/scriptshell.htm") || open(DATA,"cgi-bin/shells/scriptshell.htm") || gracefulend(" reading scriptshell");
}
@lines = <DATA>;
close(DATA);
$endfront = "<!---endfront--->";
$startend = "<!---startend--->";
$stage = 1;
foreach (@lines) {
	if (m/$endfront/o) {
		$stage = 2;
	}
	elsif (m/$startend/o) {
		$stage = 3;
	}
	elsif ($stage == 1) {
		push @front, $_;
	}
	elsif ($stage == 3) {
		push @end, $_;
	}
}

############## Getting the script

open(DATA,"cgi-bin/scripts/$newfile") || open(DATA,"cgi-bin/scripts/$newfile") || gracefulend(", for some reason, reading $newfile");
@lines = <DATA>;
close(DATA);

############ Getting the PHRASES

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


@letters = ("t","e");
$sc = param("sc");
$phr = param("phr");
$personal = param("personal");
LOOKATEACHLETTER: foreach $letter (@letters) {
	@ic = ();
	if ($letter eq "t") {
		if ($phr) {
			@ic = ("pairs/$phr");
		}
		elsif (param("ty") eq "allfemale") {
			if (open(DATA,"CPs/$subfolder/$id/femchoices.txt") ) {
				while ($_ = <DATA>) {
					push @ic, "pairs/$1" if m/^([^=]+)\=on/;
				}
				close(DATA);
			}
			else {
				@ic = ("pairs/guy1","pairs/guy2","pairs/needself","pairs/female","pairs/patriarch","pairs/getbf","pairs/sharefeelings","pairs/affirmation","pairs/affirmationmale","pairs/ofwomen","pairs/feelings","pairs/noconflict");
			}
		}
		elsif (param("ty") eq "sup") {
			@ic = ("sgsupervise");
		}
		elsif ($sc eq "inf") {
			@ic = ("sgserviceinf");
		}
		elsif ($sc eq "self" && $info{"servant"} eq "yes") {
			@ic = ("sgself");
		}
		else {
			@ic = ("sgservicenew","sgservice");
		}
	}
	else {
		if ($sc eq "up") {
			@ic = ("sgserviceupother");
		}
		elsif (param("ty") eq "allfemale") {
			if (open(DATA,"CPs/$subfolder/$id/femchoices.txt") ) {
				while ($_ = <DATA>) {
					push @ic, "pairs/$1"."other" if m/^([^=]+)\=on/;
				}
				close(DATA);
			}
			else {
				@ic = ("pairs/guy1other","pairs/guy2other","pairs/needselfother","pairs/femaleother","pairs/patriarchother","pairs/getbfother","pairs/sharefeelingsother","pairs/affirmationother","pairs/ofwomenother","pairs/feelingsother","pairs/noconflictother");
			}
		}
		elsif (param("ty") eq "ih") {
			@ic = ("serviceihother");
		}
		elsif ($sc eq "inf") {
			@ic = ("sgserviceinfe");
		}
		elsif ($sc eq "fixer" || $name eq "Miranda" || $name eq "Lily") {
			@ic = ("sgserviceother2");
		}
		elsif (param("ty") eq "ih2sg") {
			@ic = ("serviceih2sgother");
		}
		elsif ($phr) {
			@ic = ("pairs/$phr" . "other");
		}
	}
	@phrases = ();

	if (($cat = param("cat")) && $letter eq "t") {
		$fl = "CPs/$subfolder/$id/$cat.txt";
		open(DATA,"$fl") || open(DATA,"$fl");
		@p = <DATA>;
		close(DATA);

		foreach $p (@p) {
			push @phrases, "Imagine $p";
			push @phrases, "Feel your need for $p";
			push @phrases, "Feel your desire for $p";
		}
	}
	elsif (@ic) {
		foreach (@ic) {
			$fl = "cp/cgi-bin/phrases/$_.txt";
			open(DATA,"$fl") || open(DATA,"$fl");
			push @phrases, <DATA>;
			close(DATA);
		}
	}
	$phrases{$letter} = [];
	if ($letter eq "b") {
		push @{$phrases{$letter}}, @phrases;
	}
	else {
		push @phrases, @{$phrases{"b"}};
		$n = @phrases;
		@phrases = shuffle(@phrases);
		push @{$phrases{$letter}}, @phrases;
		$phrasecounter{$letter} = 0;
		$phrasetotal{$letter} = $n;
	}
}
printpage("t/start/nophrases.htm") if $phrasetotal{"c"} + $phrasetotal{"t"} + $phrasetotal{"e"} == 0;


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


###getting PICTURES  (and sets some other parameters
($startingpage = param("start") ) || ($startingpage = "start");
$target = param("target") || param("personal");

@pfl = ("images/people/$subfolder/$id");

foreach (@pfl) {
	$pagesource = "/" . $_;
	if (m/images\/people\/(.*)/) {
		$sfx = $1;
		$prfx = "/home12c/sub007/sc13648-GPVH/";
		$targ = $prfx . "cp/images/people/$sfx";
		opendir(FILES2,$targ);
		@temp = readdir(FILES2);
		closedir(FILES2);
		$ttt = @temp;
		unless ($ttt > 1) {
			opendir(FILES2,$prfx . "jacksharem.com/images/othergirls/$sfx");
			$pagesource = "http://jacksharem.com/images/othergirls/$sfx";
			@temp = readdir(FILES2);
			closedir(FILES2);
		}
	}
	else {
		$jfile = "/home12c/sub007/sc13648-GPVH/$_";
		opendir(FILES2,$jfile) || print "Couldn't open directory $jfile: $!<BR>\n";
		@temp = readdir(FILES2);
		closedir(FILES2);
	}
	@temp = grep(m/(jpg|jpeg|gif|tif|bmp)/i, @temp);
	$nn = 22;
	$nn = @temp if @temp < 22;
	@temp = shuffle(@temp) if @temp > 22;
	for ($i=0;$i<$nn;$i++) {
		push @tpics, $pagesource . "/" . $temp[$i];
	}
}

$counter{"t"} = 0;
$ncounter{"t"} = @tpics;
@tpics = shuffle(@tpics);
$tp = @tpics;

### and now the epics
@pfl = ("images/women");

foreach (@pfl) {
	$jfile = "/home12c/sub007/sc13648-GPVH/cp/$_";
	opendir(FILES,$jfile) || print "Couldn't open directory $jfile: $!<BR>\n";
	@temp = readdir(FILES);
	closedir(FILES);
	@temp = grep(m/(jpg|jpeg|gif|tif|bmp)/i, @temp);
	foreach $picname (@temp) {
		push @epics, "/" . $_ . "/" . $picname;
	}
}

$counter{"e"} = 0;
$ncounter{"e"} = @epics;
@epics = shuffle(@epics);
$ep = @epics;

########### finding the random picture files at the start
$i = 0;
CHECKFORRANDOM: foreach (@lines) {
	if (m/^\*\*\*(\S*)/) {
		$firstpage = $1;
		last CHECKFORRANDOM;
	}
	elsif (s/image\-random(\w)\: //) {
		$j = $1;
		$findimagefile{"$j"} = $i;
		chomp;
		@imgd = split /\,/, $_;
		if (@imgd > 1) {
			$r = int(rand(@imgd));
			$_ = $imgd[$r];
		}
		$imagedir[$i] = "images/$_";
		$i++;
	}
}


######## random pictures
for ($i=0; $i < @imagedir; $i++) {
#### get picture names and store in @temp 2
	if ($imagedir[$i] =~ m/^images\/([te]pic)s/) {
		$type = $1 . "choices";
		@pfl = split /\,/, $info{"$type"};
		@temp2 = ();
		foreach (@pfl) {
			$jfile = "/home12c/sub007/sc13648-GPVH/cp/images/cate/$_";
			opendir(FILES,$jfile) || print "Couldn't open directory $jfile: $!<BR>\n";
			@temp = readdir(FILES);
			closedir(FILES);
			@temp3 = grep m/(jpg|jpeg|gif|tif|bmp)/i, @temp;
			foreach $temp (@temp3) {
				push @temp2, "cate/$_/$temp";
			}
		}
		$imagedir[$i] = "images";
	}
	else {
		$jfile = "/home12c/sub007/sc13648-GPVH/cp/" . $imagedir[$i];
		opendir(FILES,$jfile) || print "Couldn't open directory $jfile: $!<BR>\n";
		@temp = readdir(FILES);
		closedir(FILES);
		@temp2 = grep m/(jpg|jpeg|gif|tif|bmp)/i, @temp;
	}

#### shuffle and store in rimagesfilesnames
	$n = @temp2;
	@temp2 = shuffle(@temp2);
	$counter[$i] = 0;
	$ncounter[$i] = $n;
	$rimagesfilenames[$i] = [];
	foreach $fl3 (@temp2) {
		push @{$rimagesfilenames[$i]}, $imagedir[$i]. "/" . $fl3;
	}
}

$starting = "yes";
$imagestart = "<img src=/";

### MAKE THE TRAINING PAGES

$ntrainingpages = 32;
$ntrainingpages = 16 if $instruction == 0;
$j = 0;


$ntrainingpages = 33;
@crit = (0, .15, .3, .5, .7, .8, .95);

if ($org eq "yes") {
	$ntrainingpages = 48;
}

$ttype = "justone";
$tmtex = 11 - 1.4 * $need;
for ($i = 1; $i <= $ntrainingpages; $i++) {
	@trlines = @front;
	$i1 = $i + 1;
	$next = "a$i1";
	$next = "ranout" if $i == $ntrainingpages;
	$tmt = $tmtex + int(rand(3));

	$r = rand(1);
	if ($r < $crit[$j] || ($j >= @crit && $r < $crit[@crit-1]) ) {
		$j = 0;
		$display = "t";
		$display = "both" unless $ttype eq "justone";
	}
	else {
		$display = "e";
		$j++;
		$tmt -= 7;
	}
	$display = "t" if $all eq "t";
	$display = "e" if $tl == 15 && $i < $mt;

	if ($display eq "both") {
		if ($phr eq "two") {
			addphrase("e");
			addphrase("t");
			$tmt = 13 + int(rand(5));
		}
		splice @trlines, 1, 0, qq~<meta http-equiv="refresh" content=$tmt;URL=/t/$dirid/$next.htm>\n~;
		addpic("t");
		addpic("e");
		addphrase("c") if $phr eq "yes";
	}
	elsif ($display eq "e") {
		splice @trlines, 1, 0, qq~<meta http-equiv="refresh" content=$tmt;URL=/t/$dirid/$next.htm>\n~;
		addpic("e");
		addphrase("e") if $phrasetotal{"e"} > 0;
	}
	elsif ($display eq "t") {
		$tmt = 4 if $tl == 13;
		splice @trlines, 1, 0, qq~<meta http-equiv="refresh" content=$tmt;URL=/t/$dirid/$next.htm>\n~;
		addpic("t");
		addphrase("t");
	}
	push @trlines, "<p><p><a href=b1.htm><font style=font-size:10>I am close to orgasming</font></a>";

	push @trlines, "<p><a href=exit.htm><font style=font-size:10>exit training</font></a>";
	push @trlines, @end;
	$fl = "t/" . $dirid . "/a" . $i . ".htm";
	open(DATA,">$fl") || open(DATA,">$fl");
	$newpage = join "", @trlines;
	print DATA $newpage || gracefulend("Error writing to $fl.");
	close(DATA);
}
### MAKE THE CM PAGES

$ntrainingpages = 16;

for ($i = 1; $i <= $ntrainingpages; $i++) {
	@trlines = @front;
	$i1 = $i + 1;
	$next = "b$i1";
	$next = "b1" if $i == $ntrainingpages;
	splice @trlines, 1, 0, qq~<meta http-equiv="refresh" content=8;URL=/t/$dirid/$next.htm>\n~;
	addpic("t");
	addphrase("t");
	push @trlines, "<p><p><a href=/cgi-bin/veryrare/getthanks.pl><font style=font-size:10>I just orgasmed</font></a><p><a href=exit.htm><font style=font-size:10>exit training</font></a>";
	push @trlines, @end;
	$fl = "t/" . $dirid . "/b" . $i . ".htm";
	open(DATA,">$fl") || open(DATA,">$fl");
	$newpage = join "\n", @trlines;
	print DATA $newpage || gracefulend("Error writing to $fl.");
	close(DATA);
}

## Edit the regular lines
$namemarker = "<!--name-->";
$name = $info{"name"};
foreach (@lines) {
	s/$namemarker/$name/o;
	while (m/\!\!\!prefix([\w\-]+)/ ) {
		push @links,$1;
		s/\!\!\!prefix/t\/$dirid\//;
	}
	s/TARGETFET/$targetfet/go;
	if (m/^image/) {
		if (m/^image.*random(\w)/) {
			$jj = $1;
			addimage($jj);
		}
		else {
			s/ .*//;
			push @images, $_;
			chomp;
			$_ = "<img src=/images/" . $_ . " align=right class=picture>\n";
		}
	}
	elsif (s/\#\#\#([\w\-]*)//) {
		chomp;
		$link = $1;
		push @links,$link;
		$_ = "<p><a href=/t/" . $dirid . "/" .  $link . ".htm>" . $_ . "</a>\n";

	}
	elsif (s/\@\@\@//) {
		chomp;
		push @links,$_;
		$_ = "<input type=hidden name=nextfile value=/t/" . $dirid . "/" .  $_ . ".htm>";
	}
	elsif (s/^INSTRUCTION(.)\://) {
		$temp = $1;
		if ($temp ne $instruction) {
			$_ = "";
		}
		else {
			$_ = "<p>" . $_;
		}
	}
	elsif (m/^[\w"]/) {
		$_ = "<p>" . $_;
	}
	elsif (m/^\*\*\*([\w\-]*)/) {
		push @addresses, $1;
		if ($starting eq "yes") {
			$starting = "no";
		}
		else {
			push @newlines,"</td></tr></table></body></html>\n";
		}
	}
	if (m/^\*\*\*/) {
		if (s/\+\+\+(\d+) ([\w\-]+)//) {
			$link = $2;
			$t = $1;
			push @links,$link;
			$entry = '<meta http-equiv="refresh" content="' . $t . "000;URL=/t/" . $dirid . "/" .  $link . '.htm">' . "\n";
			push @newlines, $_;
			push @newlines, "<html><head>\n";
			push @newlines, $entry;
		}
		else {
			push @newlines, $_;
			push @newlines, "<html><head>\n";
		}
		push @newlines,@front;
	}
	else {
		push @newlines, $_;
	}
}

push @newlines,"</td></tr></table></body></html>";


foreach (@newlines) {
	if (m/^\<meta http\-equiv\=\"refresh/) {
		s/000//;
	}
}

$counter = 0;

foreach (@newlines) {
	if (m/^\*\*\*([\w\-]+)/) {
		$nextfile = $1;
		if ($counter > 0) {
			$fl = "t/" . $dirid . "/" . $newfile . ".htm";
			open(DATA,">$fl") || open(DATA,">$fl");
			print DATA $newpage;
			close(DATA);

		}
		$newfile = $nextfile;
		$counter++;
		exit if $counter > 500;
		$newpage = "";
	}
	elsif ($counter > 0) {
		$newpage .= $_;
	}
}


####writing the last page -- the others were done as new pages were found
$fl = "t/" . $dirid . "/" . $newfile . ".htm";
open(DATA,">$fl") || open(DATA,">$fl");
print DATA $newpage || gracefulend("Error writing to $fl.");
close(DATA);

printpage("t/$dirid/a1.htm") if ($org eq "yes" || $tl == 144);
printpage("t/$dirid/startsv21.htm") if $tl == 21;
printpage("t/$dirid/startsv121.htm") if $tl == 121;
printpage("t/$dirid/startsv221.htm") if $tl == 221;
printpage("t/$dirid/startinsertion.htm") if $theme eq "insertion";
printpage("t/$dirid/startcaressing.htm") if $theme eq "caressing";

printpage("t/$dirid/$startingpage.htm");

exit;


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 {
	$fl = $_[0];
	chdir("/home12c/sub007/sc13648-GPVH/cp");
	open(DATA,"$fl") || open(DATA,"$fl") || gracefulend("There was a problem1 reading $fl: $!");
	@lines = <DATA>;
	close(DATA);
	$name = $info{"name"};
	$name = $target if $target;
	$name = $personal if $personal;
	$target = param("focus");
	foreach (@lines) {
		s/NAME/$name/o;
		s/TARGET/$target/o;
		print;
	}
	exit;
}

###apparently does images that aren't tpic or epic

sub addimage {
	my $jj = $_[0];
	$i = $findimagefile{"$jj"};
	print "Seems to be an image without a file: $jj<BR>\n" unless defined($findimagefile{"$jj"});
	$pict = ${$rimagesfilenames[$i]}[$counter[$i]];
	$counter[$i]++;
	$counter[$i] = 0 if $counter[$i] == $ncounter[$i];
	push @images, $pict;
	$_ = "<img src=/" . $pict . " align=right class=picture>\n";
	return;
}

sub shuffle {
	$n = @_;
	my $temp;
	for ($j=0; $j < $n - 1; $j++) {
		$k = int( rand(1) * ($n-$j) ) + $j;
		$temp = $_[$k];
		$_[$k] = $_[$j];
		$_[$j] = $temp;
	}
	return @_;
}

sub addphrase {
	my $let = $_[0];
	$p = ${$phrases{$let}}[$phrasecounter{$let}];
	my $pic = $$r[$usedcounter];
	$p =~ s/YOURNAME/$name/o;
	$p =~ s/NAME/$name/;
	push @trlines, "<p>$p";
	$phrasecounter{$let}++;
	$phrasecounter{$let} = 0 if $phrasecounter{$let} == $phrasetotal{$let};
}

sub addpic {
	my $let = $_[0];
	$r = \@tpics if $let eq "t";
	$r = \@epics if $let eq "e";
	$pict = $$r[$counter{$let}];
	$usedcounter = $counter{$let};
	$counter{$let}++;
	$counter{$let} = 0 if $counter{$let} == $ncounter{$let};
	push @images, $pict;
	$pict =~ s/^[^\/]\///;
	push @trlines, "<img src=" . $pict . " align=right class=picture>\n";
	return;
}




