	FUNCTION SUBMIT_IAP, FILENAME, ERRMSG=ERRMSG
;+
; Project     :	SOHO - CDS/SUMER
;
; Name        :	SUBMIT_IAP()
;
; Purpose     :	Submits an IAP file to the ECS.
;
; Explanation :	This routine is used to ftp an IAP file to the ECS workstation
;		soc.nascom.nasa.gov.
;
; Use         :	Result = SUBMIT_IAP( FILENAME )
;
;		IF NOT SUBMIT_IAP(FILENAME) THEN ...
;
; Inputs      :	FILENAME = The name of the IAP file to submit.  The default
;		directory is that given by either the CDS_IAP or SUMER_IAP
;		environment variable, and the default extension is ".IAP".
;
; Opt. Inputs :	None.
;
; Outputs     :	The result of the function is either 1 for (apparently)
;		successful, or 0 for not successful.
;
; Opt. Outputs:	None.
;
; Keywords    :	ERRMSG   = If defined and passed, then any error messages will
;			   be returned to the user in this parameter rather
;			   than being handled by the IDL MESSAGE utility.  If
;			   no errors are encountered, then a null string is
;			   returned.  In order to use this feature, the string
;			   ERRMSG must be defined first, e.g.,
;
;				ERRMSG = ''
;				Result = SUBMIT_IAP( FILENAME, ERRMSG=ERRMSG )
;				IF ERRMSG NE '' THEN ...
;
; Calls       :	FIND_WITH_DEF
;
; Common      :	None.
;
; Restrictions:	In order to use this routine, the user must have a file called
;		".netrc" in that user's home directory.   This file must have
;		the following lines in it.
;
;			machine soc.nascom.nasa.gov
;				login <NAME>
;				password <PASSWORD>
;
;		where <NAME> and <PASSWORD> are the login information needed to
;		access the SOC workstation.  The protection on this file should
;		be such that only the owner can read the file.
;
;		The file .netrc is actually a standard Unix file.  However,
;		its use in this program is strictly as an input file to be
;		read by IDL.  Any other directives in the file are ignored.  It
;		has no meaning in VMS, except as an input file to this IDL
;		program.
;
; Side effects:	A temporary file called FTP.TEMPORARY is written to the user's
;		home directory.  It is deleted upon exit of the program.
;
; Category    :	Planning, Activity Plan.
;
; Prev. Hist. :	None.
;
; Written     :	William Thompson, GSFC, 24 May 1995
;
; Modified    :	Version 1, William Thompson, GSFC, 24 May 1995
;		Version 2, William Thompson, GSFC, 8 August 1995
;			Modified for changed directory on ECS system.
;               Version 3, Liyun Wang, GSFC/ARC, June 4, 1996
;                       Modified such that the .netrc file is required
;                          to be located in PLROOT:[SUMER_PLAN.PLAN]
;                          for SUMER case
;                       Modified to supress the output of spawned commands
;               Version 4, Zarro, GSFC, Jan 14 1997, modified to ensure
;                       ASCII mode transfer in FTP
;                 
; Version     :	Version 4
;-
;
	ON_ERROR, 2
	MESSAGE = ''
;
;  Check the input parameter.
;
	IF N_PARAMS() NE 1 THEN BEGIN
		MESSAGE = 'Syntax:  Result = SUBMIT_IAP(FILENAME)'
		GOTO, HANDLE_ERROR
	ENDIF
;
;  Make sure the IAP file exists, and get its full filename.
;
	IAP_FILE = FIND_WITH_DEF(FILENAME,'CDS_IAP,SUMER_IAP','.IAP')
	IF IAP_FILE EQ '' THEN BEGIN
		MESSAGE = 'Unable to find IAP file ' + FILENAME
		GOTO, HANDLE_ERROR
	ENDIF
;
;  Find the .netrc file.
;
;---------------------------------------------------------------------------
;       For SUMER, the .netrc file is centralized to plroot:[sumer_plan.plan]
;---------------------------------------------------------------------------
        IF !VERSION.OS EQ 'vms' THEN $
           HOME = 'PLROOT:[SUMER_PLAN.PLAN]' $
        ELSE $
           HOME = '$HOME'
	NETRC_FILE = FIND_WITH_DEF('.netrc', HOME)
	IF NETRC_FILE EQ '' THEN BEGIN
		MESSAGE = 'Unable to open file .netrc in home directory'
		GOTO, HANDLE_ERROR
	ENDIF
;
;  Open the .netrc file, and find the login and password lines for the
;  soc.nascom.nasa.gov machine.
;
	OPENR, UNIT, NETRC_FILE, /GET_LUN
	LINE = ''
	MACHINE = ''
	LOGIN = ''
	PASSWORD = ''
	WHILE (NOT EOF(UNIT)) AND ((LOGIN EQ '') OR (PASSWORD EQ '')) DO BEGIN
		READF, UNIT, LINE
		WORDS = STR_SEP( STRTRIM(LINE,2), ' ' )
		COMMAND = STRLOWCASE(WORDS(0))
		IF COMMAND EQ 'machine' THEN BEGIN
			IF STRLOWCASE(WORDS(1)) EQ 'soc.nascom.nasa.gov' THEN $
				SEARCH = 1 ELSE SEARCH = 0
		ENDIF
		IF SEARCH THEN BEGIN
			IF COMMAND EQ 'login'    THEN LOGIN = WORDS(1)
			IF COMMAND EQ 'password' THEN PASSWORD = WORDS(1)
		ENDIF
	ENDWHILE
	CLOSE, UNIT
;
;  Make sure that both the login and password lines were found.
;
	IF (LOGIN EQ '') OR (PASSWORD EQ '') THEN BEGIN
		MESSAGE = 'Unable to find the login/password information'
		GOTO, HANDLE_ERROR
	ENDIF
;
;  Sort out the differences between VMS and Unix.
;
	IF !VERSION.OS EQ 'vms' THEN BEGIN
		TEMP_FILE = 'SYS$LOGIN:FTP.TEMPORARY;'
		PROT_COMMAND = 'SET FILE/PROT=(S,G,W)'
		DIR_COMMAND = 'SET DEFAULT'
		FTP_COMMAND = '$ FTP /USERNAME=' + LOGIN + ' /PASSWORD=' + $
			PASSWORD
		DELETE_COMMAND = '$ DELETE /NOCONFIRM'
		SPAWN_COMMAND = '@'
	END ELSE BEGIN
		TEMP_FILE = '$HOME/FTP.TEMPORARY'
		PROT_COMMAND = 'chmod 600'
		DIR_COMMAND = 'cd'
		FTP_COMMAND = 'ftp -n << done'
		DELETE_COMMAND = 'rm -f'
		SPAWN_COMMAND = 'source '
	ENDELSE
;
;  Open the temporary file, and change the protection so that only the owner
;  can read it.
;
	OPENW, UNIT, TEMP_FILE
	TFILE_OPENED = 1
	SPAWN, PROT_COMMAND + ' ' + TEMP_FILE, junk
;
;  Write out the initial lines depending on the operating system.
;
	IF !VERSION.OS EQ 'vms' THEN BEGIN
		PRINTF, UNIT, '$ FTP :== FTP'
	END ELSE BEGIN
		PRINTF, UNIT, '#!/bin/csh'
	ENDELSE
;
;  Break out the name of the IAP file into its directory and name components.
;  Print out the command to change to the directory containing the IAP file.
;
	BREAK_FILE, IAP_FILE, DISK, DIR, NAME, EXT
	IAP_DIR = DISK + DIR
	IAP_NAME = NAME + EXT
	IF IAP_DIR NE '' THEN PRINTF, UNIT, DIR_COMMAND + ' ' + IAP_DIR
;
;  Print the commands needed to submit the IAP file.
;
	PRINTF, UNIT, FTP_COMMAND + ' soc.nascom.nasa.gov'
	IF !VERSION.OS NE 'vms' THEN	$
		PRINTF, UNIT, 'user ' + LOGIN + ' ' + PASSWORD
	PRINTF, UNIT, 'cd /iws_files/input_actplan'
        PRINTF, UNIT,'ascii'
	PRINTF, UNIT, 'put ' + IAP_NAME + ' "' + IAP_NAME + '"'
	PRINTF, UNIT, 'quit'
	IF !VERSION.OS NE 'vms' THEN PRINTF, UNIT, 'done'
	CLOSE, UNIT
;
;  Spawn the command file.
;
;	spawn,'type ' + TEMP_FILE, junk
	SPAWN, SPAWN_COMMAND + TEMP_FILE, junk
	RESULT = 1
	GOTO, FINISH
;
;  Error handling point.
;
HANDLE_ERROR:
	IF N_ELEMENTS(ERRMSG) NE 0 THEN ERRMSG = 'GET_STUDY: ' + MESSAGE $
		ELSE MESSAGE, MESSAGE, /CONTINUE
	RESULT = 0
;
;  Free the logical unit number, delete the temporary file, and return.
;
FINISH:
	IF N_ELEMENTS(UNIT) EQ 1 THEN FREE_LUN, UNIT
	IF KEYWORD_SET(TFILE_OPENED) THEN SPAWN,	$
		DELETE_COMMAND + ' ' + TEMP_FILE, junk
;
        RETURN, RESULT
	END
