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

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

eND


$time = time();

$cookieid = "atid";
$cookie = $ENV{"HTTP_COOKIE"};
$cookie =~ m/$cookieid\=([^;]+)/;
$id = $1;
exit unless $id;
$subfolder = substr($id,0,3);



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

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

open(DATA,"ids/$subfolder/$id/hyppos.txt") || open(DATA,"ids/$subfolder/$id/hyppos.txt");
@phrasespos = <DATA>;
close(DATA);
open(DATA,"ids/$subfolder/$id/hypposq.txt") || open(DATA,"ids/$subfolder/$id/hypposq.txt");
@phrasesposq = <DATA>;
close(DATA);
open(DATA,"ids/$subfolder/$id/hypneg.txt") || open(DATA,"ids/$subfolder/$id/hypneg.txt");
@phrasesneg = <DATA>;
close(DATA);
open(DATA,"ids/$subfolder/$id/hypnegq.txt") || open(DATA,"ids/$subfolder/$id/hypnegq.txt");
@phrasesnegq = <DATA>;
close(DATA);

$dirid = $id . $time;
mkdir("/home12c/sub007/sc13648-GPVH/at/t/$dirid");

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

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, $_;
	}
}

################get pictures
$pic = $info{"picloc"};

if ($pic eq "you") {
	print "This is not available at this time<BR>\n";
}
elsif ($pic eq "select") {
	$igname = $info{"igname"};
	@pfl = ("images/$igname");
}
else {
	@pfl = ("pics/$subfolder/$id");
}

foreach (@pfl) {
	$jfile = "/home12c/sub007/sc13648-GPVH/at/$_";
	opendir(FILES,$jfile) || print "1Couldn'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 @tpics, $_ . "/" . $picname;
	}
}
@tpics = shuffle(@tpics);
$tp = @tpics;
$ip = 0;


$ipage = 0;

###makepage1
$nextpage = $ipage + 1;
@lines = ();

push @lines, qq~Do you want to do this, $name?<p><a href=/t/$dirid/a$nextpage.htm>Yes</a><p><a href=/cgi-bin/atmain.pl>No</a>~;
makepage();

###makepage2
push @lines, qq~Relax and make yourself comfortable.<p><a href=/t/$dirid/a$nextpage.htm>I have done that</a>~;
makepage();

push @lines, qq~Go slowly and patiently. Fall deeper asleep as you view each instruction.<p><a href=/t/$dirid/a$nextpage.htm>I understand</a>~;
makepage();

push @lines, qq~Do you feel yourself wanting to do what I tell you to do?<p><a href=/t/$dirid/a$nextpage.htm>Yes</a><p><a href=/cgi-bin/atmain.pl>No</a>~;
makepage();

push @lines, qq~Do you want to think what I tell you to think?<p><a href=/t/$dirid/a$nextpage.htm>Yes</a><p><a href=/cgi-bin/atmain.pl>No</a>~;
makepage();

push @lines, qq~You want to pay attention to me.<p><a href=/t/$dirid/a$nextpage.htm>Yes</a>~;
makepage();

push @lines, qq~You want to trust me. As you feel your trust for me, fall deeper asleep.<p><a href=/t/$dirid/a$nextpage.htm>Yes</a>~;
makepage();


chdir("/home12c/sub007/sc13648-GPVH") || print "Couldn't change directory $!<BR>\n";
open(DATA,"ids/$subfolder/$id/hypposz.txt") || open(DATA,"ids/$subfolder/$id/hypposz.txt");
@posphrases = <DATA>;
close(DATA);
$np = @posphrases;
print "found $np positive phrases<BR>\n";


chdir("/home12c/sub007/sc13648-GPVH/at") || print "Couldn't change directory $!<BR>\n";
foreach $posphrase (@posphrases) {
print "got here<BR>\n";
	push @lines, qq~$posphrase. <p>As you feel this, fall deeper asleep.<p><a href=/t/$dirid/a$nextpage.htm>Yes</a>~;
	makepage();
}


print qq~<a href=/t/$dirid/a0.htm>start</a>~;
exit;

sub makepage {
	open(DATA,">t/$dirid/a$ipage.htm") || open(DATA,">t/$dirid/a$nextpage.htm");
	foreach (@front) {
		print DATA;
	}
	addpic();
	foreach (@lines) {
		print DATA;
	}
	foreach (@end) {
		print DATA;
	}
	close(DATA);
	@lines = ();
	$ipage++;
	$nextpage = $ipage + 1;
	return;
}

sub addpic {
	push @lines, "<img src=/$tpics[$ip] align=right>";
	$ip++;
	$ip = 0 if $ip == $tp;
	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 @_;
}