#!/usr/athena/bin/perl

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

sub get_gender 
{
    print("What is your gender and sexual orientation?\n");
    print("(a)   Heterosexual Male\n");
    print("(b)   Heterosexual Female\n");
    print("(c)   Homosexual Male\n");
    print("(d)   Homosexual Female\n");
    $ok = 0;
    while(!$ok) 
    {
	&get_input();
	if($input lt "e") { $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(<PEOPLE>) 
    {
	($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;
	$GEND{$name}=$gen;
	print("$name gets a score of $score.\n") if $verbose;
    }
    @TOPLIST = sort byvalue keys %SCORELIST;

}

sub check_for_profile {
    while(<PEOPLE>)
    {
	($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);
	    $gender = $gen;
	    shift(@weights);
	}
    }
    seek(PEOPLE, 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");
	open(PEOPLE, ">>/afs/athena/user/m/k/mkgray/.*/lists/people");
	print(PEOPLE $prof);
	close(PEOPLE);
}
}

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

sub byhvalue {
    $SCOREHLIST{$b} <=> $SCOREHLIST{$a};
}

sub list_top {
    if($gender eq "a") {$gmatch = "b";}
    elsif($gender eq "b") {$gmatch = "a";}
    elsif($gender eq "c") {$gmatch = "c";}
    elsif($gender eq "d") {$gmatch = "d";}
    print("Top ten gender-matched matches:\n");
    $m = 0;
    do
    {
	$tcom = $SCORELIST{$tnam=$TOPLIST[$i]}/$totweight;
	$tcom = int(100*$tcom);
	if($GEND{$tnam} eq $gmatch) {
	print("Username:  $tnam      Compat: $tcom\n");
	$m++;
    }
    } until ($m = 9);
    print("Top ten overall matches:\n");
    foreach $i (0..9)
    {
	$tcom = $SCORELIST{$tnam=$TOPLIST[$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--------
if($ARGV[0] eq "-v") { $verbose = 1; }
&get_username();
if(($uname eq "mkgray")&&($ARGV[1] ne "")){
    $uname = $ARGV[1];
}
&check_for_profile();
if($eprofile)
{
    &make_totweight();
    #    &match_menu();
    &run_matches();
    &list_top();
}
else
{ 
    &get_gender();
    &create_profile();
    &submit_profile();
    &make_totweight();
    #    &match_menu();
    &run_matches();
    &list_top();
}
