
require "printed_stats.pl";

sub admin {
  my $post_data_ref=$_[0];
  my $data=$$post_data_ref{"openimpetus_minor_dispatch"};
  my $retval;

  General::log("minor: $data");
  if ($data eq "redo_stats") {
#    Database::loadDB(Constants::get(DATABASE));
#    $retval=&do_stats();
    print "<HTML>\n<HEAD><TITLE> ".Constants::get(HTML_MSG25).
      " </TITLE></HEAD>";
    print Constants::get(BODY_TAG);
    if (Database::do_stats()) {
      print "<h1 align=center>".Constants::get(HTML_MSG23)."</h1><p>\n";

      print "<h3 align=center>\n";
      my $foo="[ <a href=\"".
        Constants::get(STATS_N)."\">".
	  Constants::get(HTML_MSG27)."</a> | ".
	    "<a href=\"".
	      Constants::get(STATS_S)."\">".
		Constants::get(HTML_MSG20)."</a> | ".
		  "<a href=\"".
		    Constants::get(STATS_CATL)."\">".
		      Constants::get(HTML_MSG21)."</a> | ".
			"<a href=\"".
			  Constants::get(STATS_CATS)."\">".
			    Constants::get(HTML_MSG22)."</a> | \n".
			      "<a href=\"http://".
				Constants::get(FORM_URL).
				  "?openimpetus_major_dispatch=stats_form&openimpetus_minor_dispatch=query&compname=".Constants::get(COMPDIR)."\">".
				    Constants::get(HTML_MSG58)."</a> ]<p>\n";
      my $a=Constants::get(STATS_DIR);
      my $b=Constants::get(STATS_DIR_HREF);
      $foo=~s@$a@$b@g;
      print $foo;
      print "</h3>\n";
    } else {
      print "<h1 align=center>".Constants::get(HTML_MSG24)." ($retval)</h1><p>\n";
    }
    General::PrintHTMLTail();

  } elsif ($data eq "get_all_addresses") {
    my ($ret_ref, $i);

    Database::loadDB(Constants::get(DATABASE));
    $ret_ref=Database::getEmailAddresses();
    General::log("sizeof ret: $#$ret_ref");

    open (OUT, ">/tmp/.openimpetus_email_addresses");
    for ($i=0;$i<=$#$ret_ref;$i++) {
      print "$$ret_ref[$i]<br>\n";
      print OUT "bcc: $$ret_ref[$i]\n";
    }
    close(OUT);
  } elsif ($data eq "mail_all_comps") {
    Error::Croak("Unimplemented Admin Function: mail_all_comps");

  } elsif ($data eq "generate_numbers") {
    Error::Croak("Unimplemented Admin Function: generate_numbers, whatever THAT does.....");

  } elsif ($data eq "assign_numbers") {
    General::log("Admin: assign_numbers");
    $retval=Number::automatically_assign_numbers();
    print "<HTML><HEAD><TITLE> ".Constants::get(HTML_MSG25).
      " </TITLE></HEAD>";
    print Constants::get(BODY_TAG);
    print "<h1 align=center>".Constants::get(HTML_MSG38)."</h1>\n";
    if ($retval) {
      print Constants::get(HTML_MSG41)."<p>";
    } else {
      print Constants::get(ERROR_MSG28)." ($retval).<p>";
    }
    General::PrintHTMLTail();

  } elsif ($data eq "generate_plaintext") {
    General::log("Admin: generate_plaintext");
    Database::loadDB(Constants::get(DATABASE));
    print "<HTML><HEAD><TITLE> ".Constants::get(HTML_MSG25).
      " </TITLE></HEAD>";
    print Constants::get(BODY_TAG);
    print "<h1 align=center>".Constants::get(HTML_MSG38)."</h1>\n";

    $retval=&generate_plaintext();
    if ($retval==0) {
      print Constants::get(HTML_MSG42)."<p>";
    } else {
      print Constants::get(ERROR_MSG28)." ($retval).<p>";
    }
    General::PrintHTMLTail();

  } elsif ($data eq "generate_postscript") {
    General::log("Admin: generate_postscript");
    Database::loadDB(Constants::get(DATABASE));
    print "<HTML><HEAD><TITLE> ".Constants::get(HTML_MSG25).
      " </TITLE></HEAD>";
    print Constants::get(BODY_TAG);
#    Postscript::generate();
    print "<h1 align=center>".Constants::get(HTML_MSG38)."</h1>\n";
    $retval=&print_sheets();
    if ($retval==0) {
      print Constants::get(HTML_MSG43)."<p>";
    } else {
      print Constants::get(ERROR_MSG28)." ($retval).<p>";
    }
    General::PrintHTMLTail();

  } elsif (($data eq "open_registration") || ($data eq "close_registration")){
    $constants_filename=Constants::getConstantsFilename();
    open(CONSTANTS, "$constants_filename") ||
      Error::CantOpenFile($constants_filename);
    while(<CONSTANTS>) {
      if (/REGISTRATION_OPEN/) {
	push(@constants, "REGISTRATION_OPEN=1\n") if ($data eq "open_registration");
	push(@constants, "REGISTRATION_OPEN=0\n") if ($data eq "close_registration");
      } else {
	push(@constants, $_);
      }
    }
    close(CONSTANTS);
    open(CONSTANTS, ">$constants_filename") ||
      Error::CantOpenFile($constants_filename);
    for ($i=0;$i<=$#constants;$i++) {
      print CONSTANTS $constants[$i];
    }
    close(CONSTANTS);
    print "<HTML><HEAD><TITLE> ".Constants::get(HTML_MSG38).
      " </TITLE></HEAD>";
    print Constants::get(BODY_TAG);
    print "<h1 align=center>".Constants::get(HTML_MSG38)."</h1>\n";
    print Constants::get(HTML_MSG44)."<p>\n" if ($data eq "open_registration");
    print Constants::get(HTML_MSG45)."<p>\n" if ($data eq "close_registration");
    General::PrintHTMLTail();
  } elsif ($data eq "scrub_db") {
    General::log("scrubbing db");
    Database::loadDB(Constants::get(DATABASE));
    Database::scrub_db();
    General::log("done");
    print "<HTML><HEAD><TITLE> ".Constants::get(HTML_MSG38).
      " </TITLE></HEAD>\n";
    print Constants::get(BODY_TAG)."\n";
    print "<h1 align=center>".Constants::get(HTML_MSG38)."</h1>\n";
    print "Database scrubbed.\n";
    General::PrintHTMLTail();
  } elsif ($data eq "load_comp") {
    $loadcomp=$$post_data_ref{"loadcomp"};
    General::log("loading comp: ".$loadcomp);
    print "<HTML><HEAD><TITLE> ".Constants::get(HTML_MSG38).
      " </TITLE></HEAD>\n";
    print Constants::get(BODY_TAG)."\n";
    print "<h1 align=center>".Constants::get(HTML_MSG38)."</h1>\n";
    Database::loadComp($loadcomp);
    General::PrintHTMLTail();
  } else {
    Error::Croak("Admin: unrecognized minor dispatch: $data");
  }
}

1;
