#!/usr/local/bin/perl
#
# mutt_ph_query.pl
# Really quick hack to format ph output into a format suitable for my
# patch to mutt for external address query.  Looks up name and email
# from a ph/qi database.
#
# Brandon Long (blong@fiction.net) 4/6/98
#
# Public Domain, nothing special here, no guaruntees, warranties,
# usefulness for a purpose, blah blah

$PH = "ph";

if (!defined ($ARGV[0]))
{
  print STDERR "Usage: $0 <ph arguments>\n";
  exit (1);
}

open (QUERY, "$PH '@ARGV' return name email|");
@reply = <QUERY>;
if ($reply[0] !~ /----------/)
{
  print STDERR "$0: $reply[0]";
  exit (1);
}

$state = 1;
foreach $line (@reply)
{
  if ($state == 1)
  {
    $state = 2 if ($line =~ /----------/);
  } elsif ($state == 2)
  {
    if ($line =~ /name:/)
    {
      $line =~ /name: (.*)/;
      $name = $1;
      $state = 3;
    }
  } elsif ($state == 3)
  {
    if ($line =~ /email:/)
    {
      $line =~ /email: (.*)/;
      $email = $1;
      $RESULT{$name} = $email;
      $state = 1;
    }
  }
}

foreach $i (sort keys %RESULT)
{
  print "$RESULT{$i}\t$i\n";
}

exit (0);
