
//${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:
 *    splatdispmany
 * 
 * Purpose:
 *    Display a list of spectra in SPLAT.
 *
 * Usage:
 *    splatdispmany spectrum1 [spectrum2] ... [spectrumN]
 *
 * Description:
 *    This command accepts the names of a list of spectra and 
 *    displays them in a new window in SPLAT. If an instance of SPLAT
 *    cannot be contacted, then it creates a new one and uses that.
 *
 * Language:
 *    Beanshell (Java-based scripting language).
 *
 * @since $Date: 2006-11-24 10:55:44 +0000 (Fri, 24 Nov 2006) $
 * @since 12-JUL-2001
 * @author Peter W. Draper
 * @version $Id: splatdispmany 6187 2006-11-24 10:55:44Z pwd $
 * @copyright Copyright (C) 2001 Central Laboratory of the Research Councils
 */

/**
 * Print the usage message and exit.
 */
usage () 
{
    print( "Usage: splatdispmany spectrum1 [spectrum2] ... [spectrum3]" );
    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;
            }
            
            //  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.
    names = new StringBuffer();
    for ( int i = 0; i < bsh.args.length; i++ ) {
       names.append( bsh.args[i] ).append( " " );
    }
    command = "browser.displaySpectra(\"" + names + "\")";
    
    //  And send the command. Trap connection errors etc.
    try {
        result = RemoteUtilities.sendRemoteCommand( contactDetails, command );
        if ( ! result.equals( "-1" ) ) {
            print( "Displayed spectra in plot: '" + result +"'" );
        } 
        else {
            print( "Failed to display spectra in remote SPLAT" );
        }
    } 
    catch (Exception e) {
        print("Failed to display spectra in remote SPLAT ("+e.getMessage()+")");
    }
}

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

// Contact and display the images. 
display();

exit();
