pro mk_mo_log, mo_node, mo_dev, operator=operator, dir_date=dir_date, $
		qstop=qstop, mklog=mklog
;
;+
;NAME:
;	mk_mo_log
;PURPOSE:
;	To make a log entry when an MO disk is made to log the date, person,
;	and reformatter version of the data on the MO disk.
;SAMPLE CALLING SEQUENCE:
;	mk_mo_log, 'flare13', 0
;	mk_mo_log, 'flare4', 1, operator='linford', /dir_date, /mklog
;INPUT:
;	mo_node	- the name of the node which has the MO disk
;	mo_dev	- the device number of the MO disk (0 or 1 normally)
;OPTIONAL KEYWORD INPUT:
;	operator - the name of the person who made the MO disk.  If not
;		   passed, it uses "getenv('USER')"
;	dir_date - if set, then do not use the current date as the time
;		   that the MO disk was created, instead, use the lastest
;		   date of the directories in /.../yohkoh
;	mklog	 - if set, forces an update of the mos_made.log file.
;HISTORY:
;	Written 31-May-93 by M.Morrison
;	 2-Jun-93 (MDM) - Minor modifications
;	 10-Feb-94 (GAL) - bug fix for dir and week check
;	 11-Feb-94 (gal) - added mklog switch to update ascii log
;	 12-Feb-94 (gal) - apparently someone has changed break_file
;			which change file_info... so add checks for
;			using two tags of finfo.
;        18-Jul-94 (SLF) - get rid of rm  (PLEASE USE FILE_DELETE!)
;			   rm can hang the friggin thing
;			   mo_dev = 0 by default
;	 3-Oct-95 (LS) - changed path to new mo_mount style /mo/flareN.X
;
;-
;
mo_dev=keyword_set(mo_dev)		; dont make me enter a zero
dir = '/mo/' + mo_node + '.' + strtrim(mo_dev,2) + '/yohkoh'
spawn, 'unalias ls; ls -l ' + dir, ls
print,'LS results for ', dir
prstr, '   ' + ls
nn = file_info2(xx, finfo, ls=ls)
;
if (n_elements(operator) eq 0) then operator = getenv('USER')
if (keyword_set(dir_date)) then begin
    xx = int2secarr(finfo)
    dummy = max(xx, imax)
    daytim = anytim2ints(finfo(imax))
end else begin
    daytim = anytim2ints(!stime)
end
;
infil = concat_dir('$DIR_GEN_MO', 'mdm')
restgen, mdm, file=infil
weeks = string(mdm.st$week)
;
gen_struct, mo_disk_map=map_ref, mo_disk_log=log_ref
out_ref = {mk_mo_log, map: map_ref, log: log_ref}
;
outfil = concat_dir('$DIR_GEN_MO', 'mdm_log.genx')
qexist = file_exist(outfil)
if (qexist) then restgen, mdm_log, file=outfil
;
for i=0,nn-1 do begin
    if finfo(i).directory eq '' then begin
      weekdir = finfo(i).filename		;12-Feb-94-this didnt work yesterday!
    endif else begin
      weekdir = finfo(i).directory		;added code gal
    endelse
    pos = strpos(weekdir,'/')
    if pos ne -1 then weekdir = strmid(weekdir, 0, pos)
; --- changed ref from filename to dir
;    ss = where(weeks eq finfo(i).filename)
;	stop,'check weeks and dir'
    ss = where(weeks eq weekdir)
    if (ss(0) eq -1) then begin
;	print, 'Week number: ', finfo(i).filename, ' does not exist in MDM database'
	print, 'Week number: ', weekdir , ' does not exist in MDM database'
	stop
    end

;    dir1 =  dir + '/' + finfo(i).filename
    dir1 =  dir + '/' + weekdir		;gal mod
    print, 'Looking for ada files in: ', dir1
    ff = file_list(dir1, 'ada*')
    if (ff(0) ne '') then begin
	print, 'Found file: ', ff(0)
	rd_fheader, ff(0), fheader
	refver = fheader.refverno
	if (refver eq 0) then refver = fheader.progverno

	map = str_copy(map_ref, mdm(ss))
	out_ref.map = map

	out_ref.log.time = daytim.time
	out_ref.log.day  = daytim.day
	out_ref.log.st$operator = byte(strmid(operator, 0, 10))
	out_ref.log.refverno = refver
	;
	if (n_elements(mdm_log) eq 0) then begin
	    mdm_log = out_ref
	end else begin
	    mk_mo_list, list_arr, mdm=mdm_log.map
	    label_arr = strmid(list_arr, 0, 27)
	    mk_mo_list, list0, mdm=out_ref.map
	    label0 = strmid(list0, 0, 27)
	    ss = where(label_arr ne label0(0))
	    if (ss(0) eq -1) then begin
		print, 'The only value in the log is the one being replaced
		mdm_log = out_ref
	    end else begin
		if (n_elements(ss) ne n_elements(mdm_log)) then begin
		    print, 'Removing a previous entry for this week'
		    ;print, 'Existing Entries: '
		    ;prstr, '   ' + label_arr
		    print, 'New Entry:
		    prstr, '   ' + label0
		    ;stop
		end
		temp = mdm_log(0)
		temp = str_copy(temp, out_ref)
		mdm_log = [mdm_log(ss), temp]
	    end
	end
    end
end
;
if (keyword_set(qstop)) then stop
file_delete,outfil
savegen, mdm_log, file=outfil

if (keyword_set(mklog)) then begin	;force an update of ascii log
	outfil2 = getenv('DIR_GEN_MO')+ '/mos_made.log'	;added 11-feb-94
	file_delete, outfil2				;added 11-feb-94
	list_mo_log, outfil=outfil2			;added 11-feb-94
endif

end

