pro get_sirius, st_tim0, en_tim0, error, runtime=runtime, dir_out=dir_out, $
		flare2=flare2, temp=temp, nodsn=nodsn, $
		outfil=outfil, srsget=srsget, ftp=ftp, node_ftp=node_ftp, $
	        oldsrsget=oldsrsget
;+
;NAME:
;	get_sirius
;PURPOSE:
;	To copy YOHKOH raw telemetry data from SIRIUS to the workstation
;INPUT:
;	flare2	- If set (to 1), use SRSGET on FLARE2
;	ftp	- If set, use SRSFTP2 of FLARE2
;OUTPUT:
;	error	- Success information
;		   =  0 is successful
;		   =  5 "jbopen" or similar error - retry?
;		        or "can't create" error, or ABORT
;		   =  9 The user prompt was not what was expected after
;			starting to run "srsget"
;		   = 10 "srsget" is begin run already on flare1
;		   = 11 Segmentation fault (core dump)
;OPTIONAL KEYWORD INPUT:
;	nodsn	- If set, answer "no" to copying DSN files
;HISTORY:
;	Written Dec-91 by M.Morrison
;	 3-Mar-92 (MDM) - changed default answer for "KSC Temporary Real data"
;			 to yes.
;	17-Mar-92 (MDM) - Added "flare2" option
;	20-Mar-92 (MDM) - changed default answer for "KSC Temporary Playback 
;			  data" to yes.
;	19-Apr-92 (MDM) - Changed default answer for Temporary data to no
;			  Added keyword option /TEMP to access the temporary
;			  data
;	22-Apr-92 (MDM) - Added "outfil" in parameter list
;	 1-May-92 (MDM) - Removed check to see if someone is running SRSGET
;			  since the script handles that.
;	 3-May-92 (MDM) - Added "srsget" option to enable running SRSGET2
;	30-May-92 (MDM) - Fixed bug with commanding SRSGET=1
;	18-Jul-92 (MDM) - Added "ftp" option
;       22-Aug-92 (slf) - Increased wait between flare1 reads to avoid hanging
;	25-Aug-92 (slf) - Old wait reinstalled execept for $ys host check
;			  (additional 10 second wait inserted)
;	27-Aug-92 (slf) - Minor mod for srsftp2 output
;	14-Oct-92 (MDM) - Added "nodsn" option
;       20-Oct-92 (slf) - Changed flare1 rlogin timings again (20,.1,.1)
;			  Based on benchmarking with test program
;	11-Jan-93 (MDM) - Added capability to recognize and exit on
;			  segmentations fault (core dump) error
;	24-wed-93 (slf) - check environ ys_flarewait for rlogin wait
;	16-Jun-93 (MDM) - Modified to allow any format time range as input
;	11-Aug-93 (MDM) - Trap on "Connection closed" message which can
;			  occur at the wrong time.
;        2-apr-94 (SLF) - update for srsgetN_ver2 (wallops/santiago)
;			  backward compat via OLDSRSGET switch
;                         minor logic changes in srsget questions
;-
;
error = 0
abort = string(3b)		;a control c
start_time = systime(1)
;				; make the default srsgetN_ver2
if keyword_set(oldsrsget) then set_logenv,'srsget','srsget'
;
cmd_srsget=get_logenv('srsget')			  ; allow setting via environ
if cmd_srsget eq '' then cmd_srsget='srsget_ver2'

oldver=cmd_srsget eq 'srsget'

;
node = 'flare1'
if (keyword_set(flare2)) then node = 'flare2'
if (keyword_set(srsget)) then if (srsget ne 1) then $
   cmd_srsget = str_replace(cmd_srsget,'srsget','srsget' + strtrim(srsget,2))

if (keyword_set(ftp)) then begin
    node = 'flare2'
    cmd_srsget = 'srsftp2'
end
if (n_elements(node_ftp) eq 0) then node_ftp = 'isass0'		;or should it be flare2?
;

st_tim = anytim2ints(st_tim0)
en_tim = anytim2ints(en_tim0)
int2ex, st_tim.time, st_tim.day, st_tarr
int2ex, en_tim.time, en_tim.day, en_tarr
;
if (n_elements(dir_out) eq 0) then begin
    dir_out = '/raw_tlm/v1/' + string(st_tarr(6), st_tarr(5), format='(2i2.2)')
end
filnam = string(st_tarr(5), st_tarr(4), st_tarr(0:1), format='(5i2.2)') + '.' + $
                string(en_tarr(5), en_tarr(4), en_tarr(0:1), format='(5i2.2)')
outfil = concat_dir(dir_out, filnam)
;
ans_arr = strarr(14)
ans_arr(0) = 'n'		;not standard output file name
ans_arr(1) = outfil 
ans_arr(2) = string(st_tarr(6), st_tarr(5), st_tarr(4), st_tarr(0:1), format='(5i2.2)')
ans_arr(3) = string(en_tarr(6), en_tarr(5), en_tarr(4), en_tarr(0:1), format='(5i2.2)')
ans_arr(4) = 'y'		;KSC real time data
ans_arr(5) = 'y'		;KSC play back data
ans_arr(6) = 'y'		;DSN Goldstone
ans_arr(7) = 'y'		;DSN Canberra
ans_arr(8) = 'y'		;DSN Madrid
ans_arr(9) = 'y'		;DSN Wallops
ans_arr(10)= 'y'		;DSN Santiago
ans_arr(11) = 'n'		;KSC Temporary Real data
ans_arr(12) = 'n'		;KSC Temporary play backack data
ans_arr(13) = 'y'		;Ok to start
;
if (keyword_set(ftp)) then ans_arr(0) = node_ftp
;
; ksc temporary data (add on request)
ans_arr(11:12)=(['n','y'])(keyword_set(temp))

; dsn (remove on request)
dsn=1-keyword_set(nodsn)
ans_arr(6:10)=(['n','y'])(dsn)

if oldver then ans_arr=[ans_arr(0:8),ans_arr(11:13)] ; remove wallops/santiago

;
;---- Log in
print, '------- Logging into ', strupcase(node)
cmd = 'rlogin ' + node + ' -l sxt'
spawn, cmd, unit=lun
; slf - increased initial wait
; slf - check environ for additional wait 24-mar-1993
iwait=getenv('ys_flarewait')
if iwait eq '' then iwait=25 else begin
   mail, 'long flare login wait set', users='freeland,morrison', $
	subj='long flare login wait set'
   iwait=fix(iwait)
endelse
message,/info,'Waiting ' + strtrim(iwait,2) + $
	' seconds for flare 1 remote login...'
wait, iwait
printf, lun, '  '
;
done = 0
while (not done) do begin
    lin = '  '
    readf, lun, lin & print, lin
    ;wait, 2.5	;increased to 2.5 sec 30-Apr-92
    wait,.1	;decreased to .1      25-oct-92 (increased initial wait, slf)
    ; slf, 25-Aug-1992 - only add wait for times when ys host is checked
    if strpos(lin,'Checking Yohkoh SW host:') ne -1 or $
       strpos(lin,'You have') ne -1 then wait,.1	
    if (strmid(lin,0,6) eq node) then done = 1
end
;
lin=' '
;--- Start copy routine
print, '------- Calling SRSGET
printf, lun, cmd_srsget
readf, lun, lin		& print, lin
readf, lun, lin		& print, lin	;Added 19-Dec-91 (print statement from new srsget script - extra read)
wait, 10
if (keyword_set(ftp)) then wait, 10	;wait an additional 10 seconds
for i=0,n_elements(ans_arr)-1 do begin
    printf, lun, ans_arr(i)		& readf, lun, lin	& print,lin	;TODO - check that answering right question
    if (i eq 0) then begin
	temp = strmid(lin, 0, 12)
	trouble = 0
	if (not keyword_set(ftp)) then begin
	    if (temp ne 'Default file') then trouble = 1
	end else begin
	    if (temp ne 'receiver hos') then trouble = 1
	end
	if (trouble) then begin
	    error = 9
	    printf, lun, abort
	    goto, err
	end
    end
    wait, 2
end
;
done = 0
while (not done) do begin
    readf, lun, lin & print,lin
    if (strpos(lin, 'error') ne -1) then error = 5
    if (strpos(lin, "can't") ne -1) then error = 5
    if (strpos(lin, "ABORT") ne -1) then error = 5
    if (strpos(lin, "abort") ne -1) then error = 5
    if (strpos(lin, "Connection closed") ne -1) then error = 5	;added 11-Aug-93
    if (strpos(strupcase(lin), "CORE DUMP") ne -1) then error = 11
    if (error ne 0) then begin
	printf, lun, abort
	done = 1
    end
;   slf, 27-Aug-1992 - added strupcase below to handle srsftp2 output
    if (strpos(strupcase(lin), 'BYE') ne -1) then begin
	done = 1
	print, '*********** SUCCESSFUL COPY!!!!!!!!! *************'
    end
end
;
err:
printf, lun, 'logout'
free_lun, lun
;
end_time = systime(1)
runtime = (end_time-start_time)/60.
end
