#!/usr/bin/perl

$VERSION="v0.9.7.2R";

require 5.002;
require"open2.pl";
require"ctime.pl";

require"admin.pl";
require"generate_plaintext.pl";
require"printed_stats.pl";
require"process_change_registration.pl";
require"show_registration.pl";
require"starting_page.pl";
#require"stats.pl";
require"stats_form_query.pl";
require"stats_form_process.pl";

use Competitor;
use Constants;
use Couple;
use Dance;
use Database;
use Error;
use General;
use Hashtable;
use Mail;
use MultiDance;
use Number;
use SingleDance;
use Vector;

use strict;
use vars qw($VERSION);
use sigtrap;
use Socket;

my ($remote, $port, $iaddr, $paddr, $proto, $line);
my ($arg, @invoc, $home, $date, @post_data, $post_data, %post_data, $k, $v,
    $comp, $data, $leader, $follower, $SOCK);

local($main::server_connection_open=0);

################

sub connect_to_server {
  if ($main::server_connection_open) {
    return;
  }
  General::log("opening connection to server...");

#  $remote=shift||'localhost';
#  $port=shift||4565;
  $remote='localhost';
  $port=4565;
 
  if ($port =~/\D/) {
    $port=getservbyname($port, 'tcp');
  }
  Error::Croak("No port!\n") unless $port;
  
  $iaddr=inet_aton($remote) || Error::Croak("no host: $remote!\n");
  $paddr=sockaddr_in($port, $iaddr);
  
  $proto=getprotobyname('tcp');
  
  socket(SOCK, PF_INET, SOCK_STREAM, $proto) || Error::Croak("socket: $!\n");
  connect(SOCK, $paddr) || Error::Croak("connect: $!\n");
  $SOCK=\*SOCK;
  General::log("done.");
  $main::server_connection_open=1;
} 

################

$|=1;

open(STDERR, ">/tmp/.openimpetus_client_stderr");

if ($#ARGV==0) {
  my $arg=$ARGV[0];
  if ($arg eq "-v" || $arg eq "--version") {
    print "$VERSION\n";
  } elsif ($arg eq "-t" || $arg eq "--tar") { # hidden option
    @invoc=split('/', $0);
    pop(@invoc);
    chdir(join('/', @invoc));
    $home=$ENV{HOME};
    system("tar -cf $home/oi-$VERSION.tar *.pl *.pm *.html");
    chdir($home);
    system("gzip oi-$VERSION.tar");
  } elsif ($arg eq "-T" || $arg eq "--tar_all") { # hidden option
    @invoc=split('/', $0);
    pop(@invoc);
    chdir(join('/', @invoc));
    $home=$ENV{HOME};
    system("tar -cf $home/oi-$VERSION.all.tar .");
    chdir($home);
    system("gzip oi-$VERSION.all.tar");
  } elsif ($arg eq "-h" || $arg eq "--help") {
    print "Usage: $0 [ -v|--version | -h|--help ]\n";
  }
  exit(0);
}

$date=&ctime(time);

if ($ENV{"QUERY_STRING"} eq "") {
#  $_="openimpetus_major_dispatch=starting_page&compname=MIT";
  $_=<STDIN>;
} else {
  $_=$ENV{"QUERY_STRING"};
}
General::openLog($date."\n".$VERSION."\n$_\n");

#if (//) {
#  $_="openimpetus_major_dispatch=starting_page&compname=MIT";
#}
@post_data=split('&', $_);
foreach $_ (@post_data) {
  s/^\s*//;
  s/\s*$//;
  s/\+/ /g;
}

foreach $post_data (@post_data) {
  ($k, $_)=split('=', $post_data);
  $_=~s/%([0-9A-Fa-f][0-9A-F])/pack("H2","$1")/eg;
  s/^\s*//;
  s/\s*$//;
  s/\+/ /g;
  $post_data{$k}=$_;
}

$comp=$post_data{"compname"};

################

print "Content-Type:text/html\n\n";

Constants::fetch_constants($comp);
Database::register_comp($comp);

$data=$post_data{"openimpetus_major_dispatch"};
if ($data eq "starting_page") {
  if (!(Constants::get("REGISTRATION_OPEN"))) {
    Error::RegistrationIsClosed();
  }
  &starting_page();
} elsif ($data eq "registration") {
  $data=$post_data{"openimpetus_minor_dispatch"};
  if (!(Constants::get("REGISTRATION_OPEN"))) {
    Error::RegistrationIsClosed();
  }

#  connect_to_server();

  if ($data eq "show_registration") {
    &show_registration(\%post_data);
  } elsif ($data eq "process_change_registration") {
    &process_change_registration(\%post_data);
  } else {
    Error::PrintErrorMessage(Constants::get("ERROR_MSG4"));
  }
} elsif ($data eq "admin") {
  &admin(\%post_data);
} elsif ($data eq "stats_form") {
  $data=$post_data{"openimpetus_minor_dispatch"};

  connect_to_server();

  if ($data eq "query") {
    &stats_form_query(\%post_data);
  } elsif ($data eq "process") {
    &stats_form_process(\%post_data);
  } else {
    Error::PrintErrorMessage(Constants::get("ERROR_MSG4"));
  } 
} else {
  Error::PrintErrorMessage(Constants::get("ERROR_MSG3"));
}

$date=&ctime(time);
General::log("Exiting: $date");
close(LOG);

1;
