#!/opt/perl/bin/perl
use strict;
use warnings;

use GPS::DATES;

my $usage = qq{
	Usage : log2sta_id site [agency]
	site   : 4 char. site name
	agency : unavco,sopac,cors,panga,igscb (default: unavco)
};


my $site = shift || die "$usage";

my $agency = shift || "unavco";

my $today = get_date("today","cal");
 $site = lc($site);
my $SITE = uc( $site );

my ($year, $mon, $mday, $hour, $min, $sec, $duration , @xyz, @v); 
$duration = 1000001.00;
@v=(0,0,0);
$hour=0;
$min=0;
$sec=0;

my @secs = ( "'site identification'", "receiver", "antenna" );
my @date;
foreach my $ssec ( @secs ) {
   @date = `parse_log -s $site -a $agency -section $ssec -field 'date'`;
   ($year,$mon,$mday) = getdate(@date);
   last if (  $year &&  $mon && $mday );
};
die "Couldn't parse date\n" if  ! $year && $mon && $mday;
#exit;
#my $date;
#my (undef,undef,$cal)= split(/\:/,$date);
#chomp($cal);
#$cal =~ s/^\s+//;
#$cal =~ s/\s+$//;
#($year,$mon,$mday)=unpack("a4x1a2x1a2",$cal) if $cal =~ /\d{4}-\d+-\d+/;
my @output;

push @output,grep /city/i, `parse_log -s $site -a $agency -section 'site,location' -field city 2> /dev/null | sed 's/^2.//' `;
push @output,grep /state or province/i, `parse_log -s $site -a $agency -section 'site,location' -field 'state,province' 2> /dev/null | sed 's/^2.//' `;
push @output,grep /country/i, `parse_log -s $site -a $agency -section 'site,location' -field country 2> /dev/null | sed 's/^2.//' `;
push @output,grep /site name/i, `parse_log -s $site -a $agency -section 'site,identification' -field site 2> /dev/null | sed 's/^1.//' `;
push @output,grep /iers domes number/i, `parse_log -s $site -a $agency -section 'site,identification' -field iers 2> /dev/null | sed 's/^1.//' `;
push @output,grep /four character id/i, `parse_log -s $site -a $agency -section 'site,identification' -field id 2> /dev/null | sed 's/^1.//'` ;

#if ( ! @coords ) { die "Couldn't parse coordinates\n"; };

my %id;
foreach my $c ( @output ) {
	chomp($c);
	$c =~ s/:\s+/:/g;
	$c =~ s/\s+:/:/g;
	my ($key,$field,$value)=split(/\:/,$c);
	$field = lc(trim($field));
	$value = trim($value);
	$id{$field}=$value;
}
my $comment = "| $today $agency sitelog";
$id{country} =~ s/united states/USA/i;
$id{country} =~ s/of america//i;
#my $alias = sprintf("%-16s %-15s %-10s %-20s"
my $alias = pack('A16 x1 A9 x1 A22 x1 A10 x1  A*',$id{'site name'},
	                                      $id{'iers domes number'},
	                                      $id{'city or town'},
					      $id{country},
					      $id{'state or province'} 
				              );
$alias =~ s/\0/ /g;					
$alias =~ s/\(A9\)/    /;
$alias = pack("A60",trim($alias));
printf (" %4s%6d %-60s %-s\n",$id{'four character id'},0,$alias ,$comment);


sub getdate {
  my @d = @_;
  @d = grep /date installed/, map { lc($_) } @d;
  my ($year,$mon,$mday);
  foreach my $date ( @d ) {
     my (undef,undef,$cal)= split(/\:/,$date);
     if ( $cal ) {
        chomp($cal);
        $cal =~ s/^\s+//;
        $cal =~ s/\s+$//;
        ($year,$mon,$mday)=unpack("a4x1a2x1a2",get_date($cal,'cal')) ;
        #($year,$mon,$mday)=unpack("a4x1a2x1a2",$cal) if $cal =~ /\d{4}-\d+-\d+/;
     };
  };
  
  return ($year,$mon,$mday)

}
sub trim {
    my $str = shift;
    $str =~ s/^\s+//;
    $str=~ s/\s+$//;
    return $str
}
