#!/usr/bin/perl

print qq(Content-type: text/xml\n\n);

use Net::ParseWhois;

my $domain = 'lawyers.com';

if( $temp = $ENV{'QUERY_STRING'} ) {
  @pairs=split(/&/, $temp );
  foreach $item( @pairs ) {
     ($key,$content)=split ( /=/, $item, 2 );
     $content=~tr/+/ /;
     $content=~ s/%(..)/pack("c",hex($1))/ge;
     $fields{$key}=$content;
  }
  $domain = $fields{ 'domain' };
}
my $whois = Net::ParseWhois::Domain->new($domain);
unless ($whois->ok) {
  warn "error: " . $whois->{'error'} . "\n" if $whois->{'error'};
  die "No match for $domain\n";
}

#use Data::Dumper;
#print Dumper($whois);
#exit;

printStartNode( "Domain_Name_Whois_Record", "" );
printNode( "Domain_Name", $whois->domain );
printNode( "Date_Time_Created", $whois->record_created );
printNode( "Date_Time_Updated", $whois->record_updated );
printNode( "Date_Time_Expires", $whois->record_expires );
printStartNode( "Registrant", "" );
printNode( "Name", $whois->name );

my @addresses = split( "\n", $whois->address );
my $bAddress1 = 0;
my $bCity = 0;
my $bState = 0;
my $bZip = 0;
foreach my $address ( @addresses ) {
  #print "$address//";
  if( $bAddress1 == 1 && $address =~ /(\d+\s.*?)$/ ) {
    printNode( "Address2", $1 );
    $bAddress2 = 1;
  }
  if( $bAddress1 == 0 && $address =~ /(\d+\s.*?)$/ ) {
    printNode( "Address1", $1 );
    $bAddress1 = 1;
  }
  if( $address =~ /(\w+), (\w+) (\w+)/ ) {
    printNode( "City", $1 );
    printNode( "State_Province", $2 );
    printNode( "Postal_Code", $3 );
    $bCity = 1;
    $bState =1;
    $bZip = 1;
  } elsif( $address =~ /(\w+), (\w+)/ ) {
    printNode( "City", $1 );
    printNode( "State_Province", $2 );
    $bCity = 1;
    $bState =1;
  }
  if( $bCity == 1 && $bZip == 1 ) {
    if( $address =~ /(\w+)/ ) {
      printNode( "Country", $1 );
    }
  }
}

printNode( "Raw_Text", $whois->name . "\n" . $whois->address . "\n" . $whois->country . "\n" );
printEndNode( "Registrant" );
if( my $c = $whois->contacts ) {
  for my $t (sort keys %$c) {
    printStartNode( $t, "" );
    printStartNode( "Raw_Text", "" );
#    print map { $_\n" } @{$$c{$t}} );
#    printNode( "Raw_Text", print map { "$_\n" } @{$$c{$t}} );
#    foreach my $node (@{$$c{$t}})
#      printNode( $node, @{$$c{$t}} );
#    }
    printEndNode( "Raw_Text" );
    printEndNode( $t );
  }
}
printEndNode( "Domain_Name_Whois_Record" );

exit;

#################################################

sub printAddress {

  my $address = shift;


}

sub printStartNode {
  my $node = shift;

  if( $node =~ /ADMIN/i ) {
    $node = "Administrative_Contact";
  }
  if( $node =~ /BILLING/i ) {
    $node = "Billing_Contact";
  }
  if( $node =~ /TECH/i ) {
    $node = "Technical_Contact";
  }
  my $attr = shift;
  $node = $node . " " . $attr if $attr;
  print qq(<$node>\n);
}

sub printEndNode {
  my $node = shift;
  if( $node =~ /ADMIN/i ) {
    $node = "Administrative_Contact";
  }
  if( $node =~ /BILLING/i ) {
    $node = "Billing_Contact";
  }
  if( $node =~ /TECH/i ) {
    $node = "Technical_Contact";
  }
  print qq(</$node>\n);
}

sub printNode {
  my $node = shift;
  my $value = shift;

  print qq(<$node>$value</$node>\n);
}

#Domain_Name
#Date_Time_Created
#Date_Time_Updated
#Date_Time_Expires
#Registrant
#  Name
#  Address_Line_1
#  Address_Line_2
#  City
#  State
#  Zip_Code
#  Country
#  Raw_Text
#Administrative_Contact
#Technical_Contact
#Billing_Contact

#<Domain_Name_Whois_Record>
#
#  <Domain_Name>jurisinformatica.com</Domain_Name>
#  <Date_Time_Created>17-Oct-08</Date_Time_Created>
#  <Date_Time_Updated>29-May-09</Date_Time_Updated>
#  <Date_Time_Expires>17-Oct-10</Date_Time_Expires>
#
#  <Registrant>
#    <Name>James Mitchell</Name>
#    <Address_Line_1>25 Forest Park Dr</Address_Line_1>
#    <Address_Line_2>Apt.777</Address_Line_2>
#    <City>Waltham</City>
#    <State>Massachusetts</State>
#    <Zip_Code>02452</Zip_Code>
#    <Country>United States</Country>
#    <Raw_Text>
#      James Mitchell
#      25 Forest Park Dr Waltham, Massachusetts 02452
#      United States
#    </Raw_Text>
#  </Registrant>
#
#  <Administrative_Contact>
#    <Name>James Mitchell</Name>
#    <Address_Line_1>25 Forest Park Dr</Address_Line_1>
#    <Address_Line_2>Apt.777</Address_Line_2>
#    <City>Waltham</City>
#    <State>Massachusetts</State>
#    <Zip_Code>02452</Zip_Code>
#    <Country>United States</Country>
#    <Email_Address>jmitchell@kensingtonllc.com</Email_Address>
#    <Telephone_Number>(781) 647-0136</Telephone_Number>
#    <Fax_Telephone_Number></Fax_Telephone_Number>
#    <Raw_Text>
#      Mitchell, James jmitchell@kensingtonllc.com
#      25 Forest Park Dr
#      Waltham, Massachusetts 02452
#      United States
#      (781) 647-0136 Fax --
#    </Raw_Text>
#  </Administrative_Contact>
#
#  <Technical_Contact>
#    <Name>James Mitchell</Name>
#    <Address_Line_1>25 Forest Park Dr</Address_Line_1>
#    <Address_Line_2>Apt.777</Address_Line_2>
#    <City>Waltham</City>
#    <State>Massachusetts</State>
#    <Zip_Code>02452</Zip_Code>
#    <Country>United States</Country>
#    <Email_Address>jmitchell@kensingtonllc.com</Email_Address>
#    <Telephone_Number>(781) 647-0136</Telephone_Number>
#    <Fax_Telephone_Number>(781) 647-0136</Fax_Telephone_Number>
#    <Raw_Text>
#      Mitchell, James jmitchell@kensingtonllc.com
#      25 Forest Park Dr
#      Waltham, Massachusetts 02452
#      United States
#      (781) 647-0136 Fax --
#    </Raw_Text>
#  </Technical_Contact>
#
#  <Billing_Contact>
#    <Name>James Mitchell</Name>
#    <Address_Line_1>25 Forest Park Dr</Address_Line_1>
#    <Address_Line_2>Apt.777</Address_Line_2>
#    <City>Waltham</City>
#    <State>Massachusetts</State>
#    <Zip_Code>02452</Zip_Code>
#    <Country>United States</Country>
#    <Email_Address>jmitchell@kensingtonllc.com</Email_Address>
#    <Telephone_Number>(781) 647-0136</Telephone_Number>
#    <Fax_Telephone_Number></Fax_Telephone_Number>
#    <Raw_Text>
#      Mitchell, James jmitchell@kensingtonllc.com
#      25 Forest Park Dr
#      Waltham, Massachusetts 02452
#      United States
#      (781) 647-0136 Fax --
#    </Raw_Text>
#  </Billing_Contact>
#
#  <Zone_Contact>
#    <Name>James Mitchell</Name>
#    <Address1>25 Forest Park Dr</Address1>
#    <Address2>Apt.777</Address2>
#    <City>Waltham</City>
#    <State>Massachusetts</State>
#    <Zip_Code>02452</Zip_Code>
#    <Country>United States</Country>
#    <Email_Address>jmitchell@kensingtonllc.com</Email_Address>
#    <Telephone_Number>(781) 647-0136</Telephone_Number>
#    <Fax_Telephone_Number></Fax_Telephone_Number>
#    <Raw_Text>
#      Mitchell, James jmitchell@kensingtonllc.com
#      25 Forest Park Dr
#      Waltham, Massachusetts 02452
#      United States
#      (781) 647-0136 Fax --
#    </Raw_Text>
#  </Zone_Contact>
#
#  <Name_Servers>
#    <Host_Names>
#      <Host_Name_Address>NS51.DOMAINCONTROL.COM</Host_Name_Address>
#      <Host_Name_Address>NS52.DOMAINCONTROL.COM</Host_Name_Address>
#    </Host_Names>
#    <Raw_Text>
#      NS51.DOMAINCONTROL.COM
#      NS52.DOMAINCONTROL.COM
#    </Raw_Text>
#  </Name_Servers>
#
#  <Registry_Data>
#    <WhoIs_Server>whois.godaddy.com</WhoIs_Server>
#    <Referral_URL>http://registrar.godaddy.com</Referral_URL>
#  </Registry_Data>
#
#</Domain_Name_Whois_Record>
