#!/usr/bin/env perl
use CGI;

my $cgi = CGI->new();

my $header = '';

if($cgi->param('code')) {
    my $code = uc($cgi->param('code'));
    my $who = $cgi->param('who');
    open(my $fh, ">>", "opened.in")
    or $header .= $cgi->h2("Error: $!");
    my $now = time;
    print $fh "$code\t$now\t$who\n";
    close($fh);
    $header .= $cgi->h1("Opened: $code");
    print $cgi->redirect("http://nelhage.scripts.mit.edu/science?admin=1");
    exit;
} elsif ($cgi->param('reset')) {
    open(my $fh, ">", "opened.in") or die("$!");
    print $fh "MADS\t0\tfree\n";
    close($fh);
}

print $cgi->header(
    -charset => 'utf-8',
    $cgi->param('admin') ? () : (-refresh => 10)
   );
exec("python", "sciencetown/Notes/hex/science.py",
     "-d", "sciencetown/Notes/hex/science.in",
     "-o", "opened.in",
     $cgi->param('admin') ? ("-m") : (),
     $cgi->param('all') ? ("-a") : (),
    );
die("Unable to render grid: $!");

