pro get_ads, infil, outdir=outdir, result=result, fail=fail, $
		odisk=odisk, main2=main2, mtl=mtl
;
;+
;NAME:
;	get_ads
;PURPOSE:
;	To read the ADS file from the mainframe
;CALLING SEQUENCE:
;	get_ads, infil
;INPUT:
;	infil	- The filename on the mainframe
;		  Example: "PSK239.SAADF.A9206(T2200003)"
;OPTIONAL KEYWORD INPUT:
;	outdir	- The output directory for the file
;       odisk   - If set, correct infile for Optical Disk conv.
;KEYWORD OUTPUT:
;	fail	- Equals 1 if failed (otherwise = 0)
;HISTORY:
;	Written 18-Jul-92 by M.Morrison
;	27-Aug-92 (MDM) - Added fail keyword
;        2-Sep-92 (slf) - modified get_ads for FACOM2 
;			  added odisk keyword
;       14-Sep-92 (slf) - added main2 keyword
;	 9-Oct-92 (slf) - change password
;       20-Oct-92 (slf) - changed name from get_ads2 to get_ads
;	27-Oct-92 (slf) - chmod 666 for all
;	10-Nov-92 (slf) - changed account info
;	13-Nov-92 (slf) - changed account info (dedicated ads account)
;			  force use of 133.74.2.3 (main2 keyword has no effect
;			  change naming convention for old files
;	25-Nov-92 (slf) - change node due to ISAS HW upgrades
;	30-Nov-92 (MDM) - changed FTP command to use ' around file name
;	27-Jan-92 (slf) - modified ftp commands for MTL access
;        8-Dec-93 (SLF) - change password - add code to mail warning when
;			  password about to expire (once per session via common)
;	1-Jun-94  (SLF) - update password
;      18-Nov-94  (SLF) - update password
;      16-may-95  (slf) - read this from a file
;-
;
common get_ads_blk, exp_warn

pwf=(concat_dir('$DIR_SITE_DOC','ads_account.pw'))(0)
if not file_exist(pwf) then begin
   message,/info,"NO ADS PW FILE: " + pwf
   return
endif

dat=rd_tfile(pwf,nocomment=';',/auto)
nd=n_elements(dat(0,*))
last=nd-1
node=dat(0,last)
user=dat(1,last)
pw=dat(2,last)
updated=dat(3,last)
interval=fix(dat(4,last))

dummy=tim2dset(updated,!stime,delta=delta)
if abs(delta(0)) gt interval*24.*60*60 and n_elements(exp_warn) eq 0 then begin
   mess=['FACOM account for user ' + user + ' Password About to Expire', $
	 'Change password on mainframe and update file: ' + pwf]
   mail,mess,subj="ADS ACCT PASSWORD EXPIRATION", users="software@isass0"
   exp_warn=1
endif

;-----------------------------------------------------------------------------
if (n_elements(outdir) eq 0) then outdir=getenv('DIR_GEN_ADS')
;
getfil=infil
odisk=keyword_set(odisk) or keyword_set(mtl)			;slf,13-Nov-92
if odisk then $
    getfil=str_replace(getfil,'PSK239.SAA','SAA')	  ;slf,27-jan-93
ftp_fil = '/ydb/ads/ads_ftp'
outfil=concat_dir(outdir,infil)
outfil=outfil(0)
openw, lun, ftp_fil, /get_lun
printf, lun, 'open ' +  node		; may be facom 1 or 2
printf,  lun, 'user ' + user + ' ' + pw	;slf,  16-may-1995

printf, lun, 'binary'
;
; mtl access modification, slf, 27-jan-1993, per FACOM 
if odisk then begin 				
   printf,lun,'cd /solara'					
   printf, lun, 'get ' + getfil + ' ' +  outfil		   ; no quotes
endif else printf, lun, "get '" + getfil + "' " +  outfil  ;mdm, 30-Nov-92
;
printf, lun, 'close
printf, lun, 'bye
free_lun, lun
;
cmd = 'ftp -vn < ' + ftp_fil
spawn, cmd, result
spawn,'chmod 666 "' + outfil + '"'
;
print, result
fail = 0
for i=0,n_elements(result)-1 do if (strpos(result(i), 'fail') ne -1) then fail=1
;
end
