#!/usr/athena/bin/perl

open(BOYS, '/var/local/www/getdate/boys');
open(GIRLS, '/var/local/www/getdate/girls');

$boys = '/var/local/www/getdate/boys';
$girls = '/var/local/www/getdate/girls';

sub get_gender 
{
    # Stable, kludgy and slow, but works find
    # and doesn't use any DB dependant stuff
    $gender = '';
    open(HGEN, "$boys");
    if(&check_for_profile()){
	$gender = "a";
	open(OGEN, "$girls");
    }
    else{
	open(HGEN, "$girls");
	if(&check_for_profile){
	    $gender = "b";
	    open(OGEN, "$boys");
	}
    }
    
    if(!$eprofile){
	$request =~ /gender=([^&]+)&/;
	$input = ($1 eq 'male') ? 'a':'b';
	$request =~ s/gender=([^&]+)&//;
	if($input eq "a")
	{
	    $gender = "a";
	    open(HGEN, "$boys");
	    open(OGEN, "$girls");
	    $ok = 1;
	}
	else
	{
	    if($input eq "b")
	    {
		$gender="b";
		open(OGEN, "$boys");
		open(HGEN, "$girls");
		$ok = 1;
	    }
	}
    }
}

sub create_profile {
    # Stable, straightforward, broken...


    open(QS, "/var/local/www/getdate/questions");
    $cqnum = 1;
    while(<QS>)
    {
	($question, @options) = split(/[:\n]/);
	$request =~ /$cqnum\=([^&]+)/;
	$ans = $1;
	print("Matched against $cqnum\=([^&]+), got --$ans--\n");

	$letp = 'a';
	for $opt (@options){
	    if($ans eq $opt){
		$input = $letp;
		print("Aha, $ans equals $letp) $opt\n");
	    }
	    $letp++;
	}
	$choices[$cqnum]=$input;

	$mcq = $cqnum."m";
	$request =~ /$mcq\=([^&]+)/;
	$mans = $1;

	print("Matched against $mcq\=([^&]+), got --$mans--\n");

	$letp = 'a';
	for $opt (@options){
	    if($mans eq $opt){
		$input = $letp;
		print("Aha, $mans equals $letp) $opt\n");
	    }
	    $letp++;
	}
	$mchoices[$cqnum]=$input;
	

	$icq = $cqnum."i";
	$request =~ /$icq\=([^&]+)/;
	$ians = $1;

	$weights[$cqnum]=$ians;

	$cqnum++;
    }
}


sub run_matches 
{
    # Ack, DB code.  Bluh.


    while(<OGEN>) 
    {
	($name, $gen, $responses, $phone) = split(/:/, $_);
	@bchoices = split(/[0-9]/, $responses);
	@ochoices = grep(substr($_, 0, 1), @bchoices);
	@omchoices = grep(substr($_, 1, 1), @bchoices);

	@oweights = split(/[a-z]+/, $responses);
	$score = 0;
	foreach $chnum (0..$#mchoices)
	{
	    print("My $choices[$chnum]. vs. their $ochoices[$chnum].") if $opt_v;
	    if($mchoices[$chnum] eq $ochoices[$chnum])
	    {
		$score += int($weights[$chnum]);
		print("  $score") if $opt_v;
	    }
	    print("\n") if $opt_v;
	}
	$SCORELIST{$name}=$score;
	print("$name gets a score of $score.\n") if $opt_v;
    }
    @TOPLIST = sort byvalue keys %SCORELIST;
    
    while(<HGEN>) 
    {
	($name, $gen, $responses, $phone) = split(/:/, $_);
	@bchoices = split(/[0-9]/, $responses);
	@ochoices = grep(substr($_, 0, 1), @bchoices);
	@omchoices = grep(substr($_, 1, 1), @bchoices);

	@oweights = split(/[a-z]+/, $responses);
	$score = 0;
	foreach $chnum (0..$#mchoices)
	{
	    print("My $choices[$chnum]. vs. their $ochoices[$chnum].") if $opt_v;
	    if($mchoices[$chnum] eq $ochoices[$chnum])
	    {
		$score += int($weights[$chnum]);
		print("  $score") if $opt_v;
	    }
	    print("\n") if $opt_v;
	}
	$SCOREHLIST{$name}=$score;
	print("$name gets a score of $score.\n") if $opt_v;
    }
    @TOPHLIST = sort byhvalue keys %SCOREHLIST;
}



sub check_for_profile {
    #  Oooh, yuck I hope this works

    while(<HGEN>)
    {
	($name, $gen, $responses, $phone) = split(/:/, $_);
	print("Search name: $name. v. $uname.\n") if $opt_v;
	if($name eq $uname) 
	{
	    $eprofile = 1;
	    $profile = $_;
	    @bchoices = split(/[0-9]/, $responses);
	    @choices = grep(substr($_, 0, 1), @bchoices);
	    @mchoices = grep(substr($_, 1, 1), @bchoices);
	    @weights = split(/[a-z]+/, $responses);
	    shift(@weights);
	    print("got a profile\n");
	}
    }
    seek(HGEN, 0, 0);
    $eprofile;
}


sub submit_profile
{
    #  Ooh, this uses the new DB stuff.  Notice, 'mchoices'

    $prof=($uname.":$gender:");
    foreach $i (0..$#choices)
    {
	$prof.=($choices[$i]);
	$prof.=($mchoices[$i]);	# This is the new DB thing
	$prof.=($weights[$i]);
    }
    if($gender eq "a"){
	open(HGENW, ">>$boys");
	print(HGENW "$prof\n");
	close(HGENW);
    }
    else
    {
	open(HGENW, ">>$girls");
	print(HGENW "$prof\n");
	close(HGENW);
    }			

    print("Profile submitted for $uname\n");
    
}


sub byvalue {
    $SCORELIST{$b} <=> $SCORELIST{$a};
}

sub byhvalue {
    $SCOREHLIST{$b} <=> $SCOREHLIST{$a};
}
sub compat_for {
    local($name) = @_;
    if($tcom= $SCORELIST{$tnam=$name}/$totweight){
	$tcom = int(100*$tcom);
	foreach $i (0.. $#TOPLIST){
	    if($TOPLIST[$i] eq $name){
		$rank = $i+1; 
		last;
	    }
	}
	print("$rank) Username: $tnam\tCompat: $tcom\n");
    }
    elsif($tcom= $SCOREHLIST{$tnam=$name}/$totweight){
	$tcom = int(100*$tcom);
	foreach $i (0.. $#TOPHLIST){
	    if($TOPHLIST[$i] eq $name){
		$rank = $i+1;
		last;
	    }
	}
	print("$rank) Username: $tnam\tCompat: $tcom\n");
    }
    else{print("That username is not in the database.\n");}
    
}
sub list_top {
    print("Top opposite gender matches:\n");
    $top = 15 unless($top=$opt_q);
    $top--;
    $top = $#TOPLIST if $opt_l;
    print("<ul>\n");
    foreach $i (0..$top)
    {
	$tcom = $SCORELIST{$tnam=$TOPLIST[$i]}/$totweight;
	$tcom = int(100*$tcom);
	$x = $i+1;
	print("<li>$x) Username:  $tnam      Compat: $tcom\n");
    }
    print("</ul>\n");
    print("Top same gender matches:\n");
    $top = 15 unless ($top=$opt_q);
    $top--;
    $top = $#TOPHLIST if $opt_l;
    print("<ul>\n");
    foreach $i (0..$top)
    {
	$tcom = $SCOREHLIST{$tnam=$TOPHLIST[$i]}/$totweight;
	$tcom = int(100*$tcom);
	$x = $i+1;
	print("<li>$x) Username:  $tnam      Compat: $tcom\n");
    }
    print("</ul>\n");
}

sub get_username {
    # fixed for WWW
    $request =~ /name=([^&]+)/;
    $uname = $1;
    $request =~ s/name=([^&]+)//;
}

sub make_totweight {
    foreach $j (0..$#weights)
    {
	$totweight += int($weights[$j]);
    }
    print("Total weight: $totweight\n") if $opt_v;
}

sub how_important {
    print("How important is it that your match responds the same? [0-9]\n");
    $input = 22;
    while(($input>9)||($input<0)){
	&get_input();
    }
    $weights[$cqnum] = int($input);
}
sub give_count{
    if($gender eq "a"){
	$yours = "boys";
	$nyours = "girls";
    }
    else{
	$yours = "girls";
	$nyours = "boys";
    } 
    $total = $#TOPHLIST+$#TOPLIST;
    print("The database currently contains $#TOPHLIST $yours\n");
    print("and $#TOPLIST $nyours.  (Total of $total)\n");
}

#  --------Main--------
sub do_match {
    local($args, $cmd) = @_;
    $args =~ m,\?(.+),;
    $request = $1;
    $request =~ s/\+/ /g;


    &get_username(); # Done
    &get_gender(); # Done

    if(!$gender){	# Ugh, I think this works now
	print("<h1>You need to submit answers to questions first.  Duh.</h1>\n");
    }
    &make_totweight();		# Didn't need to be touched
    &run_matches();		# Fixed, I think
    &list_top();		# Should work
}

sub do_datesubmit {
    local($args, $cmd) = @_;
    $args =~ m,\?(.+),;
    $request = $1;
    $request =~ s/\+/ /g;

    print("<pre>\n");
    &get_username();		# Done
    &get_gender();		# Done

    if($eprofile){
	print("<h1>You have already submitted answers</h1>\n");
	alarm(0);
	return;
    }

    &create_profile();		# Done, with new DB stuff
    &submit_profile();		# Done

    alarm(0);
}



sub crap {
    if(!$eprofile){&check_for_profile();}
    if($eprofile)
{
    &make_totweight();
    #    &match_menu();
    &run_matches();
    &list_top();
}
else				# 
{ 
    &create_profile();
    &submit_profile();
    &make_totweight();
    #    &match_menu();
    &run_matches();
    if(!$opt_m){&list_top();}
    else{&compat_for($opt_m);}
}
}

sub usage {
    print("getdate [-n] [-q num] [-m bar] [-c baz]\n");
    print("-n\tCheck how many people are in the database\n");
    print("-q num\tList top num matches\n");
    print("-m name\tSee where name shows up on your list\n");
    print("-c name\tSee where you show up on name's list\n");
    exit(0);
}
