#!/bin/perl
#
# Usage: fd [city] [type] [day]
#

$now = time;
@current = gmtime($now);

if ($#ARGV < 2) {
    if ($current[2] < 2) {@current = gmtime($now-86400);}
}
elsif ($#ARGV == 2) {
    if ($current[3] < $ARGV[2]) {@current = gmtime($now-$current[3]*86400);}
    $current[3] = $ARGV[2];
}
else {
    $current[3] = $ARGV[2];
    $current[4] = $ARGV[3]-1;
    if ($#ARGV > 4) { $current[5] = $ARGV[4];}
}

if ($#ARGV < 0) {$ARGV[0] = "SEA";}
else {
    $ARGV[0] =~ tr/a-z/A-Z/;
    if ($ARGV[0] eq "ANN") {$ARGV[0] = "ANT";}
    elsif ($ARGV[0] eq "FAI") {$ARGV[0] = "AFA";}
    elsif ($ARGV[0] eq "JNU") {$ARGV[0] = "AJN";}
}

if ($#ARGV < 1) {$ARGV[1] = "zone";}

if ($ARGV[1] eq "st") {$type = "fp01";}
elsif ($ARGV[1] eq "disc") {$type = "fp03";}
elsif ($ARGV[1] eq "tp") {$type = "fp04";}
elsif ($ARGV[1] eq "zone") {$type = "fp05";}
elsif ($ARGV[1] eq "mar") {$type = "fz";}
elsif ($ARGV[1] eq "av") {$type = "fa";}
elsif ($ARGV[1] eq "warn") {$type = "warn";}
elsif ($ARGV[1] eq "extend") {$type = "fe";}

$file = sprintf("/usr/local/ldm/data/forecasts/%s/%2.2d%2.2d%2.2d.%s.%s",
    $type, $current[5], $current[4]+1, $current[3], $ARGV[0], $type);

if( open(DATA, $file) ) {
    open(OUT, "|more");
    undef ($/);
    @all = split( /\003/, <DATA> );
    for ($n=$#all; $n >= 0; $n--) {
	$all[$n] =~ s/\r//g;
	print OUT $all[$n];
    }
    close OUT;
} 
else {print "No forecast available\n";}

exit 0;   
