;+
; PROJECT: SPEX
;
; NAME: chk_batse_env
;
; PURPOSE:
; 	Check that BATSE environment variables are properly defined and return
;	full name of read_flare.pro.  If not, generate an error message.
;
; CALLING SEQUENCE:
; 	chk_batse_env, err_msg, file
;
; INPUTS:
; 	none
;
; OUTPUTS:
; 	err_msg - string array containing any errors,
;			single element an empty string otherwise
;   file - string with full path of read_flare.pro
;
; KEYWORDS:
;	none
;
; OPTIONAL OUTPUTS:
;	none
;
; CALLS:
;	concat_dir
;	get_logenv
;	uniq
;
; RESTRICTIONS:
;	none
;
; MODIFICATION HISTORY:
;	Written by Paul Bilodeau, RITSS 21-June 2000.
;-


PRO chk_batse_env, err_msg, file

; assume an error
err_msg=strarr(1)
file=''

; are the environment variables defined?
;reqd_envs=["SSW_BATSE","SSWDB_BATSE","BATSE_DATA","BATSE_FLARE_CATALOG"]
reqd_envs=["SSW_BATSE","SSW_BATSE","BATSE_FLARE_CATALOG"]
dirs=['idl','idl','']
files=['read_flare.pro', 'batse_read_cat.pro', '']

envs=get_logenv(reqd_envs, status=env_stat)

bad=where(env_stat eq 0, nbad)
if nbad gt 0 then begin
	errs=make_array(nbad, /string, value='UNDEFINED ENVIRONMENT VARIABLE: ')+reqd_envs[bad]
	err_msg=[errs[uniq(errs)]]
	return
	endif

n_files=n_elements(files)
targets=strarr(n_files)

for i=0,n_files-1 do begin
	tmp=envs[i]
	if dirs[i] ne '' then tmp=concat_dir(tmp, dirs[i], /dir)
	if files[i] ne '' then tmp=concat_dir(tmp, files[i])
	targets[i]=findfile( tmp )
	endfor

q=where(targets eq '', nq)

if nq gt 0 then begin
	errs=make_array(nq, /string, value='Could not find file: ')+files[q]
	err_msg=[err_msg, errs ]
	endif

if env_stat[0] ne 0 then file=strmid(targets[0], strpos(targets[0], files[0]), 10)

if n_elements(err_msg) gt 1 then err_msg=err_msg[1:*]

return

END
