FUNCTION EIS_POPULATE_CATALOG, path,                $
                               BEFORE = before,     $
                               AFTER = after,       $
                               ADD_YEAR = add_year, $
                               LOG = log

; Make sure that path is set.

IF N_ELEMENTS (path) EQ 0 THEN path = '.'

; Get the current time.

GET_UTC, utc

; Convert it to a string

utc_string = UTC2STR (utc, /TRUNC)

; Check if the ADD_YEAR keyword is set.  If it is, then we will add a
; 4 diget year on to the path.  This will allow us avoid adding
; calibration files into the database.

IF KEYWORD_SET (add_year) THEN BEGIN

   year = STRMID (utc_string, 0, 4)

   path = path + '/' + year

ENDIF

; Set the FITS file name glob.  Can't just look for files ending in
; .fits becuase some files GZiped.

FITS_FILE_GLOB = 'eis_*.fits*'

; Set the flag today to 0.  Will use this flag to indicate that neither the
; BEFORE or AFTER keywords were passed to us and we should process files from
; the current day.

; Make sure that either the BEFORE or AFTER keyword is set.  If neither is
; is set, then set the today flag.

today = 0

IF NOT KEYWORD_SET (before) AND NOT KEYWORD_SET (after) THEN today = 1

; Check if we need to open a log file.  If we do, then we will create an unique
; name for the log file based on the current date and time.

IF KEYWORD_SET (log) THEN BEGIN

   log_file_name = 'eis_db_update_log_' +              $
                   STRMID (utc_string, 0,  4) +        $
                   STRMID (utc_string, 5,  2) +        $
                   STRMID (utc_string, 8,  2) + '__' + $
                   STRMID (utc_string, 11, 2) +        $
                   STRMID (utc_string, 14, 2) +        $
                   STRMID (utc_string, 17, 2)

   OPENW, lun, log_file_name, /GET_LUN

   log = 1

   PRINTF, lun, 'EIS SCIENCE CATALOG log file
   PRINTF, lun, '============================================================='
   PRINTF, lun, ''
   PRINTF, lun, 'FILE NAME:  ' + log_file_name
   PRINTF, lun, 'DATE:       ' + utc_string
   PRINTF, lun, ''
   PRINTF, lun, ''

ENDIF ELSE BEGIN

   log = 0

ENDELSE

; Set the result to 1 for success.  We will reset it if the function fails to
; add the data from any FITS file into the database.

result = 1

; Set !PRIV to 3.  It has to be some number above 1 or we will not be able to
; add any studies into the database.

!PRIV = 3

; Construct a shell command to find all fits files meeting the search criteria.

cmd = 'find ' + path + " -name '" + FITS_FILE_GLOB + "' -follow -type f"

IF KEYWORD_SET (before) THEN BEGIN

   cmd = cmd + ' -mtime +' + STRTRIM (STRING (before), 2)

ENDIF

IF KEYWORD_SET (after) THEN BEGIN

   cmd = cmd + ' -mtime -' + STRTRIM (STRING (after), 2)

ENDIF

IF today THEN cmd = cmd + ' -mtime 1'

print, cmd

; Use ESPAWN to execute the shell command we just created.

ESPAWN, cmd, out, /NOSHELL

; Find the number files that were found by the find command.

nfile = N_ELEMENTS (out)

; If no files were found then just exit.

IF out [0] EQ "" THEN BEGIN

   MESSAGE, 'No files were found meeting search criterion.', /INFORMATIONAL

   IF log THEN BEGIN

       PRINTF, lun, 'No files were found meeting search criterion.'

       FREE_LUN, lun

    ENDIF

    RETURN, 0

ENDIF

o = OBJ_NEW ('eis_db')

; Loop through each file that we found and add the data from its header
; into the database.

FOR i = 0, nfile - 1 DO BEGIN

; Read the header information from the designated FITS file.

    h = EIS_GET_HDR (out [i], path)

; Check for any errors

    IF h [0] EQ "" THEN RETURN, result

; Get the record structure for the MAIN databases.

    m = o->get_main_record ()

; Convert the header into an appropiate record structure to add to the MAIN database

    r = EIS_FXH_2_REC (h, m)

; Check for any errors

;    IF N_TAGS (r) LT 1 THEN RETURN, result
    IF N_TAGS (r) LT 1 THEN stop

; Check if the a record for the study already exists in the MAIN
; database.  If it does not exist, we will add it.  If it does exist
; then we will update it.

    IF o->db_rec_exists_main ( 'TL_ID', r.tl_id, INDEX = ind, /QUIET) EQ 0 THEN BEGIN

; Add the record into the main database

       o->add_main, r

       BREAK_FILE, out [i], disk, dir, fname, ext

       msg = 'Added record to MAIN table for file: ' + fname

       MESSAGE, msg, /INFORMATIONAL

       IF log THEN PRINTF, lun, msg

    ENDIF ELSE BEGIN

; Get the original copy of the record that is stored in the database..

;       rec = o->get_main_record ()

;       o->db_get_record, o->main_db_name (), ind, rec

; Compare the original record with the new record.  If they are different, then we have to
; update the database.

;       comp = o->compare_records (r, rec, IGNORE = [22, 23], /CORRECT_STR_SIZE)

;       IF  comp [0] NE -1 THEN BEGIN

; Update the database record.

           o->update_main, r, ind

; Write some informational messages.

           BREAK_FILE, out [i], disk, dir, fname, ext

           msg = 'MAIN Database record for FITS file: ' + fname + ' has been updated.'

           MESSAGE, msg, /INFORMATIONAL

           IF log THEN BEGIN

              PRINTF, lun, msg

;              tnames = TAG_NAMES (r)

;              FOR tag = 0, N_ELEMENTS (comp) - 1 DO BEGIN

;                 PRINTF, lun, 'Updated field: ', tnames [comp [tag]], ' with value: ', r.(comp [tag])

;              ENDFOR

           ENDIF

;       ENDIF

    ENDELSE

; Get the record structure for the EXPERIMENT databases.

    e = o->get_exper_record ()

; Convert the header into an appropiate record structure to add to the EXPER database

    r = EIS_FXH_2_REC (h, e)

; Check for any errors

;    IF N_TAGS (r) LT 1 THEN RETURN, result
    IF N_TAGS (r) LT 1 THEN stop


; Check if the a record for the raster already exists in the EXPERIMENT database

    IF o->db_rec_exists_exp ( 'FILENAME', r.filename, /QUIET) EQ 0 THEN BEGIN

; Add the record into the EXPER datahbase

       o->add_exper, r, STATUS = status

       IF status EQ 1 THEN BEGIN

          BREAK_FILE, out [i], disk, dir, fname, ext

          msg = 'Added FITS file: ' + fname + ' to database.'

          MESSAGE, msg, /INFORMATIONAL

          IF log THEN PRINTF, lun, msg

       ENDIF ELSE BEGIN

          result = 0

       ENDELSE

    ENDIF ELSE BEGIN

; Get the original copy of the record that is stored in the database..

;       rec = {EIS_EXPER_LIST}

;       o->db_get_record, o->exp_db_name (), ind, rec

; Compare the original record with the new record.  If they are different, then we have to
; update the database.

;       comp = o->compare_records (r, rec, IGNORE = [0, 55], /CORRECT_STR_SIZE)

;       IF  comp [0] NE -1 THEN BEGIN

; Update the database record.

;           o->update_exper, r, ind

; Write some informational messages.

;           msg = 'EXPERIMENT Database record for FITS file: ' + r.filename + ' has been updated.'

;           MESSAGE, msg, /INFORMATIONAL

;           IF log THEN BEGIN

;              PRINTF, lun, msg

;              tnames = TAG_NAMES (r)

;              FOR tag = 0, N_ELEMENTS (comp) - 1 DO BEGIN

;                 PRINTF, lun, 'Updated field: ', tnames [comp [tag]], ' with value: ', r.(comp [tag])

;              ENDFOR

;           ENDIF

;       ENDIF

    ENDELSE

ENDFOR

IF log THEN FREE_LUN, lun

OBJ_DESTROY, O

RETURN, result

END
