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

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

eND

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

$time = time();

$cookieid = "atid";
$cookie = $ENV{"HTTP_COOKIE"};
if ($cookie =~ m/$cookieid\=([^;]+)/) {
	$id = $1;
}
else {
	print "no id<BR>\n";
	exit;
}
$subfolder = substr($id,0,3);

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

($fl = param("fl")) || ($fl = "train.txt");
$tease = param("tease");
$theme = param("theme");
$org = param("org");
($tl = param("tl")) || ($tl = $info{"traininglevel"});
$tl = 121 if $tl == 120 && rnd(2) < 1;

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


#### Getting the PHRASES
$phr="no";  ####for now
$phr = "yes" if $tl == 10;
if ($phr eq "yes" || $phr eq "two" || $phr > 0) {
	@letters = ("b","t","e","c");
	LOOKATEACHLETTER: foreach $letter (@letters) {
		@ic = ();
		if ($tl == 10 && $letter eq "t") {
			if ($info{"svphrases"} eq "yes") {
				@ic = ("ids/$subfolder/$id/svphrases");
			}
			else {
				push @ic, "at/cgi-bin/phrases/atsv";
				push @ic, "at/cgi-bin/phrases/svf" if $info{"iggender"} eq "f";
				push @ic, "at/cgi-bin/phrases/svfm" if $info{"iggender"} eq "f" && $info{"gender"} eq "m";
				push @ic, "at/cgi-bin/phrases/atdomme" if $info{"typetrain"} eq "domme";
			}
		}
		@phrases = ();
		if (@ic) {
			foreach (@ic) {
				$fl = "$_.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);
			@tphrases = @phrases if $letter eq "t";
			push @{$phrases{$letter}}, @phrases;
			$phrasecounter{$letter} = 0;
			$phrasetotal{$letter} = $n;
		}
	}
	printpage("t/start/nophrases.htm") if $phrasetotal{"c"} + $phrasetotal{"t"} + $phrasetotal{"e"} == 0;
}

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

###getting PICTURES  (and sets some other parameters
$npics = $info{"npics"};
if (! $npics) {
	print "On your control panel, you need to indicate how many pictures you have in your folder. It must be greater than zero. For example, if you had pictures called pic1, pic2, pic3, and pic4, the number would be 4.<BR>\n";
	exit;
}

for ($i=1;$i <= $npics; $i++) {
	push @tpics, "pic$i";
}

@tpics = shuffle(@tpics);
$tp = @tpics;

### and now the epics
@pfl = ("images/women/dressed","images/women/scanty","images/women/nudes","images/women/ct");

foreach (@pfl) {
	$jfile = "/home12c/sub007/sc13648-GPVH/at/$_";
	opendir(FILES,$jfile) || print "2Couldn'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;
	}
}

@epics = shuffle(@epics);
$nepics = @epics;

####adding to the page

##### get the page to put data in

open(DATA,"cgi-bin/shells/athomecare.htm") || open(DATA,"cgi-bin/shells/cgi-bin/shells/athomecare.htm") || print"problem reading athome<BR>\n";

@lines = <DATA>;
close(DATA);
$ntrials = 32;
$mtrials = $ntrials - 1;
$cmtrials = 12;
$bmtrials = $cmtrials - 1;
$looking = 0;
$tj = 0;
@crit = (0, .35, .5, .65, .75, .75);
foreach (@lines) {
	if ($looking == 0) {
		if (m/^var pic /) {
			print qq~var pic = new Array(~;
			$j = 1;
			$npics = $info{"npics"};
			for ($i=0;$i<10;$i++) {
				print qq~"$tpics[$j].jpg",~;
				$j++;
				$j = 1 if $j > $npics;
			}
			print qq~"");\n~;
		}
		elsif (m/^var igname /) {
			print qq~var igname = "$igname";~;
			$looking = 1;
		}
		else {
			print;
		}
	}
	elsif ($looking == 1 && m/^XX/) {
		print qq~ pg.document.writeln('<p><img src=$tpics[$j].jpg align=right>');\n~;
		if ($tl == 1) {
			print qq~ pg.document.writeln('<p><img src=$tpics[$j].jpg align=right>You may touch yourself sexually only when your screen includes a picture of me.');\n~;
		}
		elsif ($tl == 2) {
			print qq~ pg.document.writeln('You may touch yourself sexually only when you are paying attention to the picture of me.');\n~;
			print qq~ pg.document.writeln('It does not matter where you are <i>looking</i>. What counts is what you are <i>attending to</i>. If your attention strays from me, stop touching yourself; if your attention is drawn to me, touch yourself.');\n~;
			print qq~ pg.document.writeln('You may try to pay attention to whichever picture you want, but your touching should go with what you are <i>actually</i> attending to, not what you are <i>trying</i> to attend to. ');\n~;
		}
		$looking = 1;
	}
	else {
		print;
	}
}


exit;

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/at");
	open(DATA,"$fl") || open(DATA,"$fl") || gracefulend("There was a problem reading $fl");
	@lines = <DATA>;
	close(DATA);
	$name = $info{"name"};
	foreach (@lines) {
		s/NAME/$name/o;
		print;
	}
	exit;
}

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}];
	if ($p =~ s/^\-//) {
		push @trphrases, "<p>$p";
	}
	else {
		push @trphrases, "<p>Imagine " . $p;
	}
	$phrasecounter{$let}++;
	$phrasecounter{$let} = 0 if $phrasecounter{$let} == $phrasetotal{$let};
}




