
//${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] [clear]
 *
 * 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. If clear is set to "true", then any existing spectra will be
 *    removed from the plot.
 *
 * Language:
 *    Beanshell (Java-based scripting language).
 *
 * @since $Date: 2006-12-01 10:54:46 +0000 (Fri, 01 Dec 2006) $
 * @since 12-JUL-2001
 * @author Peter W. Draper
 * @version $Id: splatdisp 6240 2006-12-01 10:54:46Z pwd $
 * @copyright Copyright (C) 2001 Central Laboratory of the Research Councils
 */

/**
 * Print the usage message and exit.
 */
usage ()
{
    print( "Usage: splatdisp spectrum [plot_number] [clear]" );
    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...");
                if ( File.separator.equals( "/" ) ) {
                   sh = "/bin/sh";
                } else {
                   //  Cygwin or bust, sh better be on PATH.
                   sh = "sh.exe";
                }
                proc = Runtime.getRuntime().exec( 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.
    if ( bsh.args.length > 2 ) {
        command = "browser.displaySpectra(" +
            bsh.args[1] +
            ", \"" + bsh.args[0] +
            "\", " + bsh.args[2] +
            ")";
    } else if ( bsh.args.length > 1 ) {
        command = "browser.displaySpectra(" +
            bsh.args[1] +
            ", \"" + bsh.args[0] +
            "\", false)";
    } else {
        command = "browser.displaySpectra(" +
            "\"" + bsh.args[0] +
            "\")";
    }

    //  Protect windows \ from beanshell (replaces "\" with "\\"!).
    if ( ! File.separator.equals( "/" ) ) {
        command = command.replaceAll( "\\\\", "\\\\\\\\" );
    }

    //  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 > 3 ) {
   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();

