Util::Xanadu (version $)


package Util::Xanadu;
##############################################################################
#
# DESCRIPTION: This Tool subclass adds functionality for running
# DESCRIPTION: xspec and ximage
#
# HISTORY
# HISTORY: $Log: Xanadu.pm,v $
# HISTORY: Revision 1.7  2014/02/27 07:01:07  apsop
# HISTORY: VERSION header now shows CVS Revision
# HISTORY:
# HISTORY: Revision 1.6  2011/01/18 20:34:54  apsop
# HISTORY: Module changed to better handle calls to qdp and xselect
# HISTORY: Also, environment parameter $xanadu is set to $ENV{HEADAS}
# HISTORY:
# HISTORY: Revision 1.5  2009/06/09 15:04:52  apsop
# HISTORY: added command line qdp option
# HISTORY:
# HISTORY: Revision 1.4  2009/06/05 22:12:37  apsop
# HISTORY: added qdp block to script method
# HISTORY:
# HISTORY: Revision 1.3  2006/09/10 20:11:21  apsop
# HISTORY: Set environmental variables PGPLOT_FONT, PGPLOT_RGB and PFILES.
# HISTORY:
# HISTORY: Revision 1.2  2006/08/01 20:35:34  apsop
# HISTORY: Add in CVS history indicator.
# HISTORY:
# HISTORY: 1.0 -> 1.1 2002-04-01
# HISTORY: Now allows an Xserver display to be specified when initializing
# HISTORY: 
# HISTORY: 1.1 -> 1.2 2004-02-11
# HISTORY: Modified environment setup to conform to the new Tool scheme.
# 
# VERSION: $Revision: 1.7 $
#
##############################################################################

use Util::Tool;
@ISA=("Util::Tool");
use strict;

my $INVOCATION;
my $BIN;
my $LIB;
my %ENVIRONMENT=();

sub new {
    my $self=shift;
    my $program=shift;

    $self=$self->SUPER::new($BIN,$program);

    #####################################
    # set the paths 
    #####################################
    $self->bins($BIN);
    $self->libs($LIB);
    
    ##################################
    # set the temporary command file
    ##################################
    $INVOCATION++;
    $self->{COMMAND_FILE} = "${program}_${INVOCATION}_commands.tmp";

    if($program eq "xspec" ) {
        $self->command_line("-", $self->{COMMAND_FILE} );

    } elsif($program eq "ximage") {
        $self->command_line("@".$self->{COMMAND_FILE} );
    } 

    return $self;
   
}


######################
# ACCESSORS:
######################

########################################################################
# Returns a hash of environment variables which must
# be set before running this tool
########################################################################
sub environment {
    my $self=shift;

    return {%ENVIRONMENT};

} # end of environment method

####################################################################
# get or set the executable directories in the class data.
# This method also sets a number of environment variables
# required by XANADU.
##################################################################
sub bin { #(directory, [display])
    my $self=shift;
    if (@_) {
        #######################################################
        # set the bin directory and some environment variables
        #######################################################
        $BIN = shift;
        my $display=shift || ":0.0";


        my $xanadu=$BIN;
        $xanadu =~ s/\/[^\/]*$//;
        $ENVIRONMENT{XANBIN}=$xanadu;
	
	$ENVIRONMENT{HEADAS}=$xanadu;

        $ENVIRONMENT{XRDEFAULTS} ="$xanadu/xrdefaults";
        $ENVIRONMENT{POW_LIBRARY}="$xanadu/lib/pow";
        $ENVIRONMENT{PGPLOT_DIR} ="$xanadu/lib";
        $ENVIRONMENT{PGPLOT_FONT} ="$xanadu/lib/grfont.dat";
        $ENVIRONMENT{PGPLOT_RGB} ="$xanadu/lib/rgb.txt";

	$ENVIRONMENT{PFILES} = ".;$xanadu/syspfiles";

        $LIB="$xanadu/lib";

        $xanadu =~ s/\/[^\/]*$//;
        $ENVIRONMENT{XANADU}=$xanadu;



       #################################################################
       # Ximage expects an X server, even if it never actually
       # displays anything on it. If there isn't one it complains.
       # Here we simply set the display and assume that it is actually
       # running on the system.
       #################################################################
       $ENVIRONMENT{DISPLAY} = $display;

    }

    return $BIN

} # end of bin method



#########################################################################
# set the command script to be fed to the xanadu tool
#########################################################################
sub script { #(commands)
    my $self = shift;

    unlink $self->{COMMAND_FILE};
    open  SCRIPT, ">$self->{COMMAND_FILE}";
    print SCRIPT join("\n", @_) ."\n";
    close SCRIPT;

    ########################################
    # For QDP dump script to $self->{STDIN}
    ########################################
    if ( $self->name( ) eq 'qdp') {
        $self->command_line( shift @_ );
        $self->stdin( join("\n", @_) ."\n" );
    } elsif($self->name( ) eq 'xselect') {
      $self->command_line( "\@".$self->{COMMAND_FILE} );
    }

    return $self;

} # end of script method

############################################################################
############################################################################
# clean up temporary files
############################################################################
sub DESTROY {
    my $self=shift;

    unlink $self->{COMMAND_FILE};

    unlink "xautosav.xcm"; # xspec file

    # check for an overridden destructor...
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}


1;