	FUNCTION SUBMIT_CAP, FILENAME, ERRMSG=ERRMSG
;+
; Project     :	SOHO - CDS/SUMER
;
; Name        :	SUBMIT_CAP()
;
; Purpose     :	Submits a CAP file to the ECS.
;
; Explanation :	This routine is used to ftp a CAP file to the ECS workstation
;		soc.nascom.nasa.gov.
;
; Use         :	Result = SUBMIT_CAP( FILENAME )
;
;		IF NOT SUBMIT_CAP(FILENAME) THEN ...
;
; Inputs      :	FILENAME = The name of the CAP file to submit.  The default
;		directory is that given by the SOHO_CAP 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_CAP( 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, 3 August 1995
;
; Modified    :	Version 1, William Thompson, GSFC, 3 August 1995
;		Version 2, William Thompson, GSFC, 8 August 1995
;			Modified for changed directory on ECS computers.
;
; Version     :	Version 2, 8 August 1995
;-
;
	ON_ERROR, 2
	MESSAGE = ''
;
;  Check the input parameter.
;
	IF N_PARAMS() NE 1 THEN BEGIN
		MESSAGE = 'Syntax:  Result = SUBMIT_CAP(FILENAME)'
		GOTO, HANDLE_ERROR
	ENDIF
;
;  Make sure the CAP file exists, and get its full filename.
;
	CAP_FILE = FIND_WITH_DEF(FILENAME,'SOHO_CAP','.CAP')

	IF CAP_FILE EQ '' THEN BEGIN
		MESSAGE = 'Unable to find CAP file ' + FILENAME
		GOTO, HANDLE_ERROR
	ENDIF
;
;  Find the .netrc file.
;
	IF !VERSION.OS EQ 'vms' THEN HOME = 'SYS$LOGIN:' 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
;
;  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 CAP file into its directory and name components.
;  Print out the command to change to the directory containing the CAP file.
;
	BREAK_FILE, CAP_FILE, DISK, DIR, NAME, EXT
	CAP_DIR = DISK + DIR
	CAP_NAME = NAME + EXT
	IF CAP_DIR NE '' THEN PRINTF, UNIT, DIR_COMMAND + ' ' + CAP_DIR
;
;  Print the commands needed to submit the CAP 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, 'put "' + CAP_NAME + '"'
	PRINTF, UNIT, 'quit'
	IF !VERSION.OS NE 'vms' THEN PRINTF, UNIT, 'done'
	CLOSE, UNIT
;
;  Spawn the command file.
;
	SPAWN, SPAWN_COMMAND + TEMP_FILE
	RESULT = 1
	GOTO, FINISH
;
;  Error handling point.
;
HANDLE_ERROR:
	IF N_ELEMENTS(ERRMSG) NE 0 THEN ERRMSG = 'SUBMIT_CAP: ' + 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
;
	RETURN, RESULT
	END
