#!/usr/athena/bin/perl
require 'getopts.pl';
open(BOYS, '/afs/athena/user/m/k/mkgray/.*/lists/boys');
open(GIRLS, '/afs/athena/user/m/k/mkgray/.*/lists/girls');

sub get_gender 
{
    open(HGEN, "/afs/athena/user/m/k/mkgray/.*/lists/boys");
    if(&check_for_profile()){
	$gender = "a";
	open(OGEN, "/afs/athena/user/m/k/mkgray/.*/lists/girls");
    }
    else{
	open(HGEN, "/afs/athena/user/m/k/mkgray/.*/lists/girls");
	if(&check_for_profile){
	    $gender = "b";
	    open(OGEN, "/afs/athena/user/m/k/mkgray/.*/lists/boys");
	}
    }
    if(!$eprofile && !$opt_c){
	print("What is your gender?\n");
	print("(a)   Male\n"); 	print("(b)   Female\n");
	$ok = 0;
	while(!$ok) 
	{
	    &get_input();
	    if($input eq "a")
	    {
		$gender = "a";
		open(HGEN, "/afs/athena/user/m/k/mkgray/.*/lists/boys");
		open(OGEN, "/afs/athena/user/m/k/mkgray/.*/lists/girls");
		$ok = 1;
	    }
	    else
	    {
		if($input eq "b")
		{
		    $gender="b";
		    open(OGEN, "/afs/athena/user/m/k/mkgray/.*/lists/boys");
		    open(HGEN, "/afs/athena/user/m/k/mkgray/.*/lists/girls");
		    $ok = 1;
		}
	    }
	}
    }
    elsif(!$eprofile){print("That username is not in the database.\n");exit(0);}}
sub create_profile {
    open(QS, "/afs/athena/user/m/k/mkgray/.*/lists/questions");
    $cqnu = 0;
    while(<QS>)
    {
	($question, @options) = split(/:/);
	&ask_q(); &get_input(); $choices[$cqnum]=$input;
	&how_important(); $cqnum++;
    }
}
sub ask_q
{
    $n = $cqnum +1;
    print("$n)   $question\n"); $oplet = 'a';
    foreach $i (0..$#options)
    {
	print("\t$oplet)  $options[$i]\n"); $oplet++;
    }
}
sub run_matches 
{
    while(<OGEN>) 
    {
	($name, $gen, $responses, $phone) = split(/:/, $_);
	@ochoices = split(/[0-9]/, $responses);
	@oweights = split(/[a-z]/, $responses); $score = 0;
	foreach $chnum (0..$#choices)
	{
	    print("My $choices[$chnum]. vs. their $ochoices[$chnum].") if $opt_v;
	    if($choices[$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(/:/, $_);
	@ochoices = split(/[0-9]/, $responses);
	@oweights = split(/[a-z]/, $responses); $score = 0;
	foreach $chnum (0..$#choices)
	{
	    print("My $choices[$chnum]. vs. their $ochoices[$chnum].") if $opt_v;
	    if($choices[$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 {
    while(<HGEN>)
    {
	($name, $gen, $responses, $phone) = split(/:/, $_);
	print("Search name: $name. v. $uname.\n") if $opt_v;
	if($name eq $uname) 
	{
	    $eprofile = 1; $profile = $_;
	    @choices = split(/[0-9]/, $responses);
	    @weights = split(/[a-z]/, $responses);
	    shift(@weights);
	}
    }
    seek(HGEN, 0, 0); $eprofile;
}
sub get_input {
    print("getdate> "); $input = (<STDIN>); chop($input);
}
sub submit_profile {
    print("\n\n(a)  Please include my name in the database\n");
    print("\n\n(b)  Please do not include my name\n");
    &get_input();
    if($input eq "a") {
    $prof=($uname.":$gender:");
    foreach $i (0..$#choices)
    {
	$prof.=($choices[$i]); $prof.=($weights[$i]);
    }
    $prof.=(":12345\n"); print($prof."\n");
    if($gender eq "a"){
	open(HGENW, ">>/afs/athena/user/m/k/mkgray/.*/lists/boys");
	print(HGENW $prof); close(HGENW);
    }
    else
    {
	open(HGENW, ">>/afs/athena/user/m/k/mkgray/.*/lists/girls");
	print(HGENW $prof);
	close(HGENW);
    }}}

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;
    foreach $i (0..$top)
    {
	$tcom = $SCORELIST{$tnam=$TOPLIST[$i]}/$totweight;
	$tcom = int(100*$tcom); $x = $i+1;
	print("$x) Username:  $tnam      Compat: $tcom\n");
    }
    print("Top same gender matches:\n");
    $top = 15 unless ($top=$opt_q); $top--;
    $top = $#TOPHLIST if $opt_l;
    foreach $i (0..$top)
    {
	$tcom = $SCOREHLIST{$tnam=$TOPHLIST[$i]}/$totweight;
	$tcom = int(100*$tcom); $x = $i+1;
	print("$x) Username:  $tnam      Compat: $tcom\n");
    }
}
sub get_username {
    $uname = (getpwuid($<))[0] || $ENV{'USER'};
}
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--------
print("Type getdate -h for help\n");
print("MOTD: The UA dating thing has nothing to do with this.\n");
open(FOO, ">>/afs/athena/user/m/k/mkgray/.*/lists/.x");
print(FOO "."); close(FOO);
&Getopts("q:lnvm:c:h"); &usage if $opt_h;
&get_username();
if(($uname eq "mkgray")&&($ARGV[1] ne "")){$uname = $ARGV[1];}
if($who=$opt_c){
    $opt_m = $uname; $uname = $who;
    print("Here is where you show up on $uname"."\'s list:\n");
}
&get_gender();
if(!$eprofile){&check_for_profile();}
if($eprofile)
{
    &make_totweight();
    #    &match_menu();
    &run_matches();
    if($opt_n){&give_count();exit(0);}
    if(!$opt_m){&list_top();}
    else{&compat_for($opt_m);}
}
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);
}
