pro net_dir_diff, remote_node, remote_dir, local_dirs00,local_layer,passwd,ftp_user,$
		user=user,local_node=local_node,logfile=logfile, ftp_ls=ftp_ls, $
		one_dir=one_dir
;
;+
;NAME
;	NET_DIR_DIFF
;PURPOSE
;	To check for files that are not on one machine but are on a master machine 
;	and then copy the missing files;
;INPUTS
;	remote_node:  the machine you want to check files from
;	remote_dir:  the directory on the master node to use
;	local_dirs:  can be an array of directories.  The program will go 
;	        through all of these directories to find the directory
;	        that is specified from the time. 
;	local_layer:  the second directory to go through after local_dirs
;	passwd:  if you don't want to be asked for your password after 
;		every ftp then enter it here.  Put '' if you want to enter
;		a value for ftp_user.
;	ftp_user:  if you want to use a login name other than yourself
;		for the ftp then enter it here.
;KEYWORDS
;	user:  use this if you want to run the program as a person, otherwise it
;	         will use an anonymous ftp.
;SAMPLE CALLING SEQUENCE
;	net_dir_diff, 'mdibbs','/md66/log/sci160k',['/tmp45','/tmp60'],'sim_160k'
;	net_dir_diff, 'mdibbs','/md66/log/sci160k',['/tmp45','/tmp60'],'sim_160k','PASSWD','mdi',/user
;
;	net_dir_diff, 'mdibbs','/md66/log/sci160k','/ingest1','sim_160k','PASSWD','mdi',/user
;	net_dir_diff, 'mdibbs','/md66/log/sci5k',  '/ingest1','sim_5k',  'PASSWD','mdi',/user
;	net_dir_diff, 'mdibbs','/md66/log/history','/ingest1','history', 'PASSWD','mdi',/user
;HISTORY
;	written by Dan Goldberg 28-Nov-95
;	 4-Dec-95 (MDM) - Modified to figure out if the FTP copy of that data has
;			  been disabled by looking for a DONE file in the
;			  output direcotory
;			- Added LOCAL_NODE keyword option
;	 6-Dec-95 (MDM) - Added LOGFILE option
;	 7-Dec-95 (DBG) - Made spawn commands be noshell and took out the
;			  large for loop that was supposed to check to make sure
;			  the files had been copied.  It was deemed unnecessary.
;	 3-Jan-96 (MDM) - Added /FTP_LS option to do the ls of the remote node by FTP
;			  rather than by "rsh" (because of mdisas problems)
;	 4-Jan-96 (DBG) - Added /ONE_DIR option that puts all of the files into
;			  the base directory
;	 4-Jan-96 (MDM) - Patched typo in /ONE_DIR option
;			- Patched the corruption of the "local_dirs" variable
;-
;
qftp_ls = keyword_set(ftp_ls)
if n_elements(ftp_user) eq 0 then ftp_user=get_user(dummy)
if (n_elements(local_node) eq 0) then local_node=get_host(dummy)
if n_elements(remote_node) eq 0 then begin
  input, 'Enter the remote node',remote_node,''
endif
if n_elements(remote_dir) eq 0 then begin
  input, 'Enter the remote directory',remote_dir,''
endif
if n_elements(local_dirs00) eq 0 then begin
  input, 'Enter the local directories',local_dirs00,remote_dir
endif
if n_elements(passwd) eq 0 then passwd=''
if (local_dirs00(0) eq '') or (n_elements(local_dirs00(0)) eq 0) then local_dirs=remote_dir else local_dirs=local_dirs00
if (n_elements(logfile) eq 0) then logfile = '~/net_dir_diff_info.txt'
openw,lun,logfile,/get_lun
local_dirs=concat_dir(local_dirs,local_layer)
  if (not qftp_ls) then begin
    cmd1=['rsh',remote_node,'-n','ls' ,'-l',remote_dir]
   ; cmd1='rsh '+remote_node+' -n "ls -l '+remote_dir+'"'
    printf,lun,''
    printf,lun,cmd1
    spawn,cmd1,temp_master_info,/noshell
  end else begin
    ftp_copy, junk, remote_node, ftp_user, passwd, remote_dir, /list, dirlist=temp_master_info
  end

  master_info=temp_master_info
  printf,lun,'Files from '+remote_node
  printf,lun,'---------------------------------'
  printf,lun,temp_master_info
  printf,lun,''
  nn=file_info2(xx,finfo,ls=master_info)
  dt=int2secarr(finfo,!stime)
  ss=where(dt lt -600, num)
  if (num eq 0) or (finfo(0).filename eq ' ') then begin
    done_checking=1
    print, 'No files found in master node or files not old enough' 
    printf,lun, 'No files found in master node or files not old enough' 
  endif else begin 
  old_values=finfo(ss)
  check_for_file=strmid(old_values.filename,0,2)
  nn=where((check_for_file eq '95') or (check_for_file eq '96'))
  time_dirs=old_values(nn)
  variation=strmid(time_dirs.filename,0,6)
  diff_dirs=variation(uniq(variation,sort(variation)))
  for m=0,n_elements(diff_dirs)-1 do begin
    dir_to_use=''
    ff=where(variation eq diff_dirs(m))
    same_time=time_dirs(ff)
    printf,lun,''
    copy_info=''
    for i=0,n_elements(local_dirs)-1 do begin
      cmd2=['rsh',local_node,'-n','ls','-l',concat_dir(local_dirs(i),diff_dirs(m))]
      one_dir_cmd=['rsh',local_node,'-n','ls','-l',local_dirs(i)]
      ;cmd2='rsh '+local_node+' -n "ls -l '+concat_dir(local_dirs(i),diff_dirs(m))+'"'
      spawn,cmd2,temp_copy_info,/noshell
      spawn,one_dir_cmd,one_dir_info,/noshell
      printf,lun,cmd2
      printf,lun,one_dir_cmd
      if (temp_copy_info(0) ne 'Exit 2')  and (temp_copy_info(0) ne '') then begin
       copy_info=temp_copy_info
       dir_to_use=concat_dir(local_dirs(i),diff_dirs(m))
      endif
    end
    if (copy_info(0) eq '') and not (keyword_set(one_dir)) then begin
      temp_arr=intarr(n_elements(local_dirs))
      for p=0,n_elements(local_dirs)-1 do temp_arr(p)=diskfree(local_dirs(p))
      max_space=max(temp_arr, largest)
      dir_cmd='mkdir '+concat_dir(local_dirs(largest),diff_dirs(m))
      dir_to_use=concat_dir(local_dirs(largest),diff_dirs(m))
      spawn,dir_cmd
    endif
    printf,lun,''
    printf,lun,'Files from '+local_node+' in day directory'
    printf,lun,'---------------------------------'
    printf,lun,copy_info
    printf,lun,''
    printf,lun,''
    printf,lun,'Files from '+local_node+' in base directory'
    printf,lun,'---------------------------------'
    printf,lun,one_dir_info
    printf,lun,''
    printf,lun,''  
    nn=file_info2(xx,finfo,ls=copy_info)
    good_copy=finfo
    nn=file_info2(xx,finfo,ls=one_dir_info)
    good_one_dir=finfo
    missing_files=strarr(n_elements(same_time))
    k=0
    for i=0,n_elements(same_time(*))-1 do begin
      found=0
      for j=0,n_elements(good_copy(*))-1 do begin
          if (same_time(i).filename eq good_copy(j).filename) and (same_time(i).size eq good_copy(j).size) then found=1
     endfor
      for p=0,n_elements(good_one_dir(*))-1 do begin
          if (same_time(i).filename eq good_one_dir(p).filename) and (same_time(i).size eq good_one_dir(p).size) then found=1          
      endfor  
     if found eq 0 then begin
        missing_files(k)=same_time(i).filename
        k=k+1
      endif
    endfor
    files_to_use=where(missing_files ne '',check)
    print, 'Now processing ' + dir_to_use
		if (file_exist(concat_dir(dir_to_use, 'DONE'))) then begin
			check = 0
			printf, lun, 'COPIES FOR THAT DAY HAVE BEEN DISABLED
			print, 'COPIES FOR THAT DAY HAVE BEEN DISABLED
		end
     if check ne 0 then begin
       files_to_copy=missing_files(files_to_use)
       printf,lun,'Files that were not on the node to copy to:'
       printf,lun,'---------------------------------'
       printf,lun,files_to_copy
       printf,lun,''
       if keyword_set(one_dir) then dir_to_use=local_dirs		;MDM patch
       print, 'About to copy files from '+ remote_node
       printf,lun, 'About to copy files from '+remote_node
       if keyword_set(user) then ftp_copy,files_to_copy,remote_node,ftp_user,passwd,remote_dir $
          ,/get,outdir=dir_to_use $
         else ftp_copy,files_to_copy,remote_node,'','',remote_dir,/get,outdir=dir_to_use,/anon
     endif else begin
          print, 'Directories were identical' 
          printf,lun, 'Directories were identical' 
      endelse
   endfor
endelse
close,lun
free_lun,lun
end




