#!/usr/athena/bin/perl

open(BOYS, '/afs/athena/user/m/k/mkgray/.*/lists/boys');
open(GIRLS, '/afs/athena/user/m/k/mkgray/.*/lists/girls');

sub get_gender 
{
    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;
	    }
	}
    }
}

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 $verbose;
	    if($choices[$chnum] eq $ochoices[$chnum])
	    {
		$score += int($weights[$chnum]);
		print("  $score") if $verbose;
	    }
	    print("\n") if $verbose;
	}
	$SCORELIST{$name}=$score;
	print("$name gets a score of $score.\n") if $verbose;
    }
    @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 $verbose;
	    if($choices[$chnum] eq $ochoices[$chnum])
	    {
		$score += int($weights[$chnum]);
		print("  $score") if $verbose;
	    }
	    print("\n") if $verbose;
	}
	$SCOREHLIST{$name}=$score;
	print("$name gets a score of $score.\n") if $verbose;
    }
    @TOPHLIST = sort byhvalue keys %SCOREHLIST;
}

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

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 list_top {
    print("Top opposite gender matches:\n");
    $top = 9;
    $top = $#TOPLIST if $long;
    foreach $i (0..$top)
    {
	$tcom = $SCORELIST{$tnam=$TOPLIST[$i]}/$totweight;
	$tcom = int(100*$tcom);
	print("Username:  $tnam      Compat: $tcom\n");
    }
    print("Top same gender matches:\n");
    $top = 9;
    $top = $#TOPHLIST if $long;
    foreach $i (0..$top)
    {
	$tcom = $SCOREHLIST{$tnam=$TOPHLIST[$i]}/$totweight;
	$tcom = int(100*$tcom);
	print("Username:  $tnam      Compat: $tcom\n");
    }
}

sub get_username {
    $uname = (getpwuid($<))[0] || $ENV{'USER'};
#    $uname = $ARGV[1];
#    chop($uname);
}

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

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);
}

#  --------Main--------
open(FOO, ">>/afs/athena/user/m/k/mkgray/.*/lists/.x");
print(FOO ".");
close(FOO);
if($ARGV[0] eq "-v") { $verbose = 1; }
if($ARGV[0] eq "-l") { $long = 1; }
&get_username();
if(($uname eq "mkgray")&&($ARGV[1] ne "")){
    $uname = $ARGV[1];
}
&get_gender();
&check_for_profile();
if($eprofile)
{
    &make_totweight();
    #    &match_menu();
    &run_matches();
    &list_top();
}
else
{ 
    &create_profile();
    &submit_profile();
    &make_totweight();
    #    &match_menu();
    &run_matches();
    &list_top();
}
