#!/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/at";
chdir($dirlocation) || print "Couldn't change directory $!<BR>\n";


#### Getting the PHRASES
$phr="yes";

if ($phr eq "yes" || $phr eq "two" || $phr > 0) {
	@letters = ("t","e","c");
	foreach $letter (@letters) {
		@ic = ();
		if ($letter eq "e") {
			@ic = ("ndtrnothanks");
		}
		elsif ($letter eq "t") {
			@ic = ("ndtrthanks");
		}
		elsif ($letter eq "c") {
			@ic = ("ndtrnothanks2");
		}
		@phrases = ();
		if (@ic) {
			foreach (@ic) {
				$fl = "cgi-bin/phrases/$_.txt";
				open(DATA,"$fl") || open(DATA,"$fl") || print "trouble: $!<BR>\n";;
				push @phrases, <DATA>;
				close(DATA);
			}
		}
		$phrases{$letter} = [];

		$n = @phrases;
		@phrases = shuffle(@phrases);
		push @{$phrases{$letter}}, @phrases;
		$phrasecounter{$letter} = 0;
		$phrasetotal{$letter} = $n;
	}
}

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/athomeneedfd.txt") || open(DATA,"cgi-bin/shells/cgi-bin/shells/athomeneedfd.txt") || print "problem reading athomeneedfd<BR>\n";

@lines = <DATA>;
close(DATA);
$ntrials = 24;
$cmtrials = 24;
$looking = 0;
$tj = 0;
foreach (@lines) {
	if ($looking == 0) {
		if (m/^var pic /) {
			print qq~var pic = new Array(~;
			$j = 0;
			$npics = $info{"npics"};
			for ($i=0;$i<=$ntrials-1;$i++) {
				$tj = 0;
				print qq~"$tpics[$j].jpg",~;
				$j++;
				$j = 0 if $j == $npics;
			}
			print qq~"$tpics[$ntrials-1]");\n~;
		}
		elsif (m/^var pic2/) {
			print qq~var pic2 = new Array(~;
			print qq~"$epics[0]"~;
			$jj = 1;
			for ($i=0;$i<$cmtrials;$i++) {
				print qq~,"$epics[$jj]"~;
				$jj++;
				$jj = 0 if $jj == $nepics;
			}
			print qq~);\n~;
		}
		elsif (m/^var phrase /) {
			$jp = 0;
			print qq~var phrase = new Array(~;
			for ($i=0;$i<$ntrials;$i++) {
				$t = getphrase("t");
				chomp($t);
				$chrm = chr(13);
				$t =~ s/$chrm//gm;
				print qq~"$t",~;
			}
			print qq~"");\n~;
		}
		elsif (m/^var phrase2/) {
			$jp = 0;
			print qq~var phrase2 = new Array(~;
			for ($i=0;$i<$ntrials;$i++) {
				$t = getphrase("e");
				chomp($t);
				$chrm = chr(13);
				$t =~ s/$chrm//gm;
				print qq~"$t",~;
			}
			print qq~"");\n~;
		}
		elsif (m/^var phrase3/) {
			$jp = 0;
			print qq~var phrase3 = new Array(~;
			for ($i=0;$i<$ntrials;$i++) {
				$t = getphrase("c");
				chomp($t);
				$chrm = chr(13);
				$t =~ s/$chrm//gm;
				print qq~"$t",~;
			}
			print qq~"");\n~;
		}
		elsif (m/^var igname /) {
			print qq~var igname = "$igname";~;
			$looking = 1;
		}
		else {
			print;
		}
	}
	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 getphrase {
	my $let = $_[0];
	$p = ${$phrases{$let}}[$phrasecounter{$let}];
	$phrasecounter{$let}++;
	$phrasecounter{$let} = 0 if $phrasecounter{$let} == $phrasetotal{$let};
	return $p;
}




