;+
; NAME:
;     OVIEW2WEB
; PURPOSE:
;     Creates an FTP batch file that is used to transfer files for
;     a given day to the OVRO Solar Array web server.  Also initiates
;     the sending of the files by spawning a batch job.
; CATEGORY:
;     OVRO APC WEB
; CALLING SEQUENCE:
;     oview2web,filename
; INPUTS:
;     filename   the name of the OVRO data file (.ARC) for which to
;                  create an overview web page.
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
;     break_file
; OUTPUTS:
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 27-Oct-1999 by Dale E. Gary
;       Replacement for a much more involved routine of the same name
;     31-Oct-1999  DG
;       Slight change to reflect new location for files on bbsoweb.
;     06-Nov-1999  DG
;       Updates to send snapshot files from SOLAID.
;     11-Jan-2000  DG
;       Eliminated hardwired directory locations.
;     29-Jan-2000 DG
;       Fixed bug that caused spawn not to work because we were in the
;       wrong disk.
;     19-Apr-2000  DG
;       Added handling of FITS files via a small change to add different
;       relative path to FITS directory.  Assumes FITS is parallel to DATA.
;     15-Dec-2000  DG
;       Changed the root directory of web server to reflect change to
;       web server file structure, after adding 20 GB disk.
;-

pro oview2web,filename

   line = ''
   outdir = !defaults.webdir

   ; Determine the date from the filename
   datestr = filename2date(filename)
   ; Eliminate '/' separators
   junk = str_sep(datestr,'/')
   datestr = junk[0]+junk[1]+junk[2]

   ; Ensure that there are some files to transfer
   fnames = findfile(outdir+'*'+datestr+'*',count=nfiles)
   if (nfiles eq 0) then return

   ; There are some files.
   ; Remove directory info from file names, then generate PUT lines for each.
   putlines = strarr(nfiles)
   for i = 0, nfiles-1 do begin
      break_file,fnames[i],disk,dir,file,ext
      fnames[i] = file+ext
      if (ext eq '.fts') then begin
         ; Put FITS files into a separate location, parallel to DATA location
         putlines[i] = 'put '+fnames[i]+' '+'../../fits/'+strmid(datestr,0,6)+'/'+fnames[i]
      endif else begin
         putlines[i] = 'put '+fnames[i]+' '+fnames[i]
      endelse
   endfor

   ; Initiate construction of new FTPOUT.BAT
   ; Open the existing FTPOUT file as a template
   openr,lun,/get_lun,!defaults.webdir+'ftpout.bat'
   if (lun eq 0) then begin
      ans = dialog_message('Cannot find file '+!defaults.webdir+'FTPOUT.BAT',/error)
      return
   endif

   ; Attempt to read a maximum of 50 lines, and on error eliminate
   ; empty lines.  This allows certain changes to the ftpout file
   ; without requiring changes to this code.
   ON_IOERROR,fileread
   lines = strarr(50)
   readf,lun,lines

fileread:
   free_lun,lun
   ON_IOERROR,NULL

   ; Eliminate empty lines and determine how many lines are not empty
   lines = lines(where(lines ne '',nlines))

   ; Write the lines out into a file ftpout.old just to save it in
   ; case of disaster, since we are about to overwrite the existing
   ; file.
   openw,/get_lun,lun,!defaults.webdir+'ftpout.old'
   printf,lun,lines,format='(a)'
   free_lun,lun

   ; The lines that need to be changed are the CD to directory YYYYMM,
   ; and the PUT lines.  All others can be left unchanged.
   ; Find the existing PUT lines--return with an error if there are none
   pindx = where(strmid(lines,0,3) eq 'put',nput)
   if (nput ne 0) then begin
      pstart = pindx[0]
      pend   = pindx[nput-1]
   endif else begin
      ans = dialog_message(['OVIEW2WEB: Error in file '+outdir+'ftpbat.out.',$
                'No "put" lines in ftp file.'],/error)
      return
   endelse

   ; Declare storage for the output lines
   outlines = strarr(nlines-nput+nfiles)

   ; Transfer lines up to first PUT line to outlines.
   for l = 0, pstart-1 do begin
      if (strmid(lines[l],0,2) eq 'cd') then begin
         ; This is the CD line, so change to the appropriate date
         outlines[l] = 'cd /var/www/html/data/gifs/'+strmid(datestr,0,6)
      endif else begin
         outlines[l] = lines[l]
      endelse
   endfor

   ; Enter new PUT lines (in place of old PUT lines)
   for i = 0, nfiles-1 do begin
      outlines[i+pstart] = putlines[i]
   endfor

   ; Transfer lines after last PUT line to outlines
   for l = pend+1, nlines-1 do begin
      outlines[l - (pend+1) + pstart + nfiles] = lines[l]
   endfor

   ; Now we just need to write out the lines
   openw,lun,/get_lun,!defaults.webdir+'ftpout.bat'
   printf,lun,outlines,format='(a)'
   free_lun,lun

   cd,!defaults.webdir,current=cwd
   ; Initiate the sending of the data
   spawn,!defaults.cmddir+'putweb.bat'
   cd,cwd

return
end