# Custom Perl include file
# Add your Perl customizations here

# Custom Perl include file
# Add your Perl customizations here

package LAS::Server;
add_mime('test','image/gif');    #Add a new mime type for 'test' op

package LAS::Server::Newserver;
use strict;
use LAS;
use LAS::Server;
use CLC;
@LAS::Server::Newserver::ISA = qw(LAS::Server::Handler);

my $GradsDir = "/var/stuff/lasxml/grads";
my $Script   = "$GradsDir/script.gs";

$ENV{GADDIR} = "$GradsDir";

sub init {
    my ($self, $req, $output) = @_;
    my $props = $self->{props};
    $self->{req} = $req;
    $self->{output_file} = $output;
    $self->{temp_file} = $output . "_foobar";
#
# Get the variable and region. For now, there should only one variable and
# one region
#
    my @children = $req->getChildren;
    foreach (@children){
	my $class = ref ($_);
	if ($class eq "LAS::Variable"){
	    die "Duplicate variable argument" if $self->{var};
	    $self->{var} = $_;
	} elsif ($class eq "LAS::Region"){
	    die "Duplicate region argument" if $self->{region};
	    $self->{region} = $_;
	}
    }
    die "Missing variable argument" if ! $self->{var};
    die "Missing region argument" if ! $self->{region};
#
# Get dataset_name, variable_name
#
    my $url = $self->{var}->getURL;
    $url =~ s/file://;
    $props->{dataset_name} = $url;
    $props->{variable_name} = $self->{var}->getName;
#
# Dump to debug
    foreach (keys %{$props}){
	debug("Grads prop: ", $props, " $_ = $props->{$_}\n");
    }
}

sub command {
    my ($self, @args) = @_;
    debug("Grads: ");
    my $outstr = "";
    foreach (@args){
	$outstr .= "$_ ";
    }
    debug("$outstr\n");
    $self->{grads}->do_command($outstr);
}

sub close {
    my $self = shift;
    my $kill = shift;
    my $grads = $self->{grads};
    if ($grads){
#	$grads->close($kill);
	$grads->close;
    }
}
sub preExecute {
    my ($self) = @_;
    my $props  = $self->{props};
    my $output = $self->{output_file};
    my $outpath = "/var/stuff/lasxml/server/";
    my $outfile = $outpath.$output;

    open(SCR,"+>$Script") || die debug("Error opening $Script: $!\n");
    print SCR "'open $props->{dataset_name}'\n";

    my $region = $self->{region};
    my $var = $self->{var};
    my @axes = $var->getChildren;
    my @regChildren = $self->{region}->getChildren;
    my $count = 0;
    foreach (@axes){
	my $type = $_->getAttribute("type");
	my $arg = $regChildren[$count++];
	die "No axis type for $_->getName" if ! $type;
	die "Invalid axis type: $type" if ($type !~ /x|y|z|t/);
	my ($lo,$hi) = ($arg->getLo, $arg->getHi);
	debug("Region: $lo $hi $type\n");
	if ($type ne 't'){
	    if ( $lo =~ /([0-9\.]*)s/i ) { $lo = -1.0 * $1; }
	    if ( $lo =~ /([0-9\.]*)n/i ) { $lo = $1; }
	    if ( $hi =~ /([0-9\.]*)s/i ) { $hi = -1.0 * $1; }
	    if ( $hi =~ /([0-9\.]*)n/i ) { $hi = $1; }
	}
	if ($type eq 'x'){
	    print SCR "'set lon $lo $hi'\n";
	} elsif ($type eq 'y'){
	    print SCR "'set lat $lo $hi'\n";
	} elsif ($type eq 'z'){
	    print SCR "'set lev $lo $hi'\n";
	} elsif ($type eq 't'){
	    $lo =~ s/\-//g;
	    $hi =~ s/\-//g;
	    print SCR "'set t $lo $hi'\n";
	}
    }
    print SCR "'set gxout shaded'\n";
    print SCR "'d $props->{variable_name}'\n";
    print SCR "'printim $outfile gif x600 y400 white'\n";
    print SCR "'quit'\n";
    close SCR;

    $self->{grads} =
	my $grads = new CLC("$GradsDir/grads", "ga> ");
    debug("Starting...\n");
    $grads->start("-lbc $Script");
#    $grads->wait_for_prompt;
    debug("All done!\n");
}

sub test {
#    my ($self) = @_;
#    my $props = $self->{props};
#    $self->command("set gxout shaded");
#    $self->command("d ", $props->{variable_name});
}

sub execute {
    my ($self, $method) = @_;
    eval('$self->' . $method);
    die $@ if $@;
}

sub postExecute {
#    my $self = shift;
#    my $kill = shift;
#    my $grads = $self->{grads};
#    if ($grads){
#	$grads->close($kill);
#    }
    my ($self) = @_;
    $self->close;
}


1;
