#!/usr/bin/perl

use CGI qw(:standard);

$room=1;
$unlock=7;
@doors=(6,7,8);
$username=cookie('MazeID');
if($username) {
createPage();
} else {
print redirect('http://127.0.0.1/cgi-bin/maze/entry.pl');
}

sub createPage {
  open (COOKLIST, "C:/mazeperm/$username.txt") || die "open: $!";
  while (<COOKLIST>) {  
    $perm=$_;
  }
  close (COOKLIST);
  @perms=split(/,/, $perm);
  print header(),start_html("Room #$room");
  if (@perms[$room]) {
    print h1("Welcome to Room $room");

    if (!(@perms[$unlock])) {

      print p("You have unlocked the door to room #$unlock.");
      @perms[$unlock]=1;
      $newperms=join(",",@perms);
      open (COOKLIST, ">C:/mazeperm/$username.txt") || die "open: $!";
      $newperms=join(",",@perms);
      print COOKLIST "$newperms";
      close (COOKLIST);
    }
  print p("You may go from here to:");
  for ($i=0; $i<3; $i++){
    print p("<A HREF='maze@doors[$i].pl'>Room # @doors[$i]</A>");
}
  
} else {
print h1("No entry");
print p("You don't have that key.  You may <A HREF='maze1.pl'>start over</A> if you like.");
}
print close_html();
}