#! /usr/bin/perl

$spl = substr $ENV{COMP_LINE}, 0, $ENV{COMP_POINT};
$spl =~ /([^ ]*)$/;
$partial = $1;

#print STDERR "\n@ARGV [@ARGV[1], $ENV{COMP_LINE}, $ENV{COMP_POINT}, $spl, $partial]\n";

open (GALELOGS, "<", "$ENV{HOME}/var/log/gale.log") or die "$ENV{HOME}/var/log/gale.log: $!";

while (<GALELOGS>) {
  if ($head and /^To: /) {
    @segments = split(/ /);

    while ($arg = shift(@segments)) {
      chomp ($arg);
      if ($partial =~ /@/) {
	if ($arg =~ /^\Q$partial\E(.*)/) {
	  $answer = @ARGV[1] . $1;
	  $hash{$answer} = 1;
	}
      }
      else {
	if ($arg =~ /^\Q$partial\E(@.*|\.?[^.@]*)/) {
	  $answer = @ARGV[1] . $1;
	  $hash{$answer} = 1;
	}
      }
    }
  }

  if (/^--*$/) {
    $head = 1;
  }
  else {
    $head = 0;
  }

}

foreach $key (keys %hash) {
  unless ($key =~ m/[\/@]/) {
    $key_count++;
    $dummy_key=$key;
  }
  print "$key\n";
}

if ($key_count == 1) {
  print "$dummy_key@";
}
  
