
//${SPLAT_DIR}/splatsh $0 ${1+"$@"}; exit;
// Note first line is blank, do not remove it as this starts "sh", which
// runs the next line, which runs splatsh on this file. This header
// section is ignored as it is a beanshell comment, that sh never sees.

/**
 * Name:
 *    splatdisp
 *
 * Purpose:
 *    Displays a spectrum in SPLAT.
 *
 * Usage:
 *    splatdisp spectrum1 [plot_number]
 *
 * Description:
 *    This command accepts the name of a single spectrum and
 *    displays it in SPLAT. If an instance of SPLAT cannot be
 *    contacted, then it creates a new one and uses that. A plot
 *    number can be given, if this already exists then the spectrum
 *    will be added to the plot, otherwise a new plot will be
 *    created.
 *
 * Language:
 *    Beanshell (Java-based scripting language).
 *
 * @since $Date: 2003/06/04 09:20:35 $
 * @since 12-JUL-2001
 * @author Peter W. Draper
 * @version $Id: splatdisp,v 1.1 2003/06/04 09:20:35 pwd Exp $
 * @copyright Copyright (C) 2001 Central Laboratory of the Research Councils
 */

/**
 * Print the usage message and exit.
 */
usage ()
{
    print( "Usage: splatdisp spectrum [plot_number]" );
    exit();
}

/**
 * Open up the connection and issue the command needed to display the
 * image.
 */
display()
{
    //  Import any classes that we need from SPLAT.
    import uk.ac.starlink.splat.util.RemoteUtilities;

    //  Read the contact details for the current SPLAT instance. These are
    //  the machine name, the server port and the authentication cookie.
    Object[] contactDetails = RemoteUtilities.readContactFile();

    //  See if SPLAT is around and listening.
    if ( contactDetails == null ||
         ! RemoteUtilities.isListening( contactDetails ) ) {

        //  No so start an instance.
        started = false;
        tried = 0;
        while ( tried < 100 ) {
            if ( ! started ) {
                print("Failed to connect to SPLAT, starting new instance...");
                proc = Runtime.getRuntime().exec( "/bin/sh -c $SPLAT_DIR/splat" );
                started = true;

                //  TODO: Check don't need this work around on Solaris...
                //  Locate the "detached_splat" script and run it up in a
                //  thread. Need to do this to stop blocking of the exec
                //  command . Note I think this script should live in a
                //  jar on release!
                //script = getResource("/star/splat/scripts/detached_splat");
                //bg( script.getPath() );
            }

            //  Wait a while, this blocks current thread.
            exec( "sleep 2" );
            contactDetails = RemoteUtilities.readContactFile();
            if ( contactDetails != null ) {
                if ( RemoteUtilities.isListening( contactDetails ) ) {
                    break;
                }
            }
            tried++;
        }
        if ( tried == 100 ) {
            print( "Failed to connect to SPLAT" );
            exit();
        }
    }

    //  Construct the command that the remote beanshell interpreter
    //  in SPLAT should execute.
    if ( bsh.args.length > 1 ) {
        command = "browser.displaySpectra(" +
            bsh.args[1] +
            ", \"" + bsh.args[0] +
            "\")";
    } else {
        command = "browser.displaySpectra(" +
            "\"" + bsh.args[0] +
            "\")";
    }
    //  And send the command. Trap connection errors etc.
    try {
        result = RemoteUtilities.sendRemoteCommand( contactDetails, command );
        Integer.parseInt( result );
        if ( ! result.equals( "-1" ) ) {
            print( "Displayed spectrum in plot: " + result );
        }
        else {
            print( "Failed to display spectrum in remote SPLAT" );
        }
    }
    catch ( Exception e ) {
        print( "Failed to display spectrum in remote SPLAT");
        print( e.getMessage() );
    }
}

//  Check that we have been given a spectrum to display.
if ( bsh.args == null || bsh.args == void ||
     bsh.args.length == 0 || bsh.args.length > 2 ) {
   usage();
}

//  Check second command-line argument is an integer.
if ( bsh.args.length == 2 ) {
    try {
        Integer.parseInt( bsh.args[1] );
    }
    catch ( Exception e ) {
        print( "Error: second optional parameter should be an integer ("
               + bsh.args[1] + ")" );
        usage();
    }
}

//  Contact SPLAT and display image.
display();

exit();

