pro web_seq, who, httpdir=httpdir, outdir=outdir, indir=indir, $
        qtitle_only=qtitle_only, all_frames=all_frames, $
        quick=quick, time_line=time_line, qstop=qstop, $
        prep=prep, dev=dev
;
;
common web_seq_blk1a, label, link, link2, dirs, all_dirs
;
if (keyword_set(quick)) and (n_elements(link) ne 0) then goto, quick
;
if (n_elements(who) eq 0) then who = 'cjs'
;;if (n_elements(httpdir) eq 0) then httpdir = concat_dir(getenv('top_http'), 'dbase/source')   ;web
;;if (n_elements(outdir) eq 0) then outdir = '$path_http/dbase/' + who              ;physical dir
;;if (n_elements(indir) eq 0) then indir = '/tsw/obs_dev/' + who
;;outdir2 = '$path_http/dbase/source'       ;concat_dir(outdir, 'source')
if (keyword_set(dev)) then begin
    if (n_elements(httpdir) eq 0) then httpdir = concat_dir(getenv('WEB_SEQ_OBS_DEV'), 'source')
    if (n_elements(outdir) eq 0) then outdir = concat_dir(getenv('WEB_PREPEND_DIR'), $
                        concat_dir(getenv('WEB_SEQ_OBS_DEV'), who) )
    if (n_elements(indir) eq 0) then indir = '/tsw/obs_dev/' + who
    httpdir2 = getenv('WEB_SEQ_OBS_DEV')
    bdir = '/tsw/obs_dev'       ;could be /tsw_shaw/obs_dev
end else begin
    if (n_elements(httpdir) eq 0) then httpdir = concat_dir(getenv('WEB_SEQ_OBS'), 'source')
    if (n_elements(outdir) eq 0) then outdir = concat_dir(getenv('WEB_PREPEND_DIR'), $
                        concat_dir(getenv('WEB_SEQ_OBS'), who) )
    if (n_elements(indir) eq 0) then indir = '/tsw/obs/'
    httpdir2 = getenv('WEB_SEQ_OBS')
    bdir = '/tsw/obs'
end

outdir2 = concat_dir(getenv('WEB_PREPEND_DIR'), httpdir)        ;physical dir
;
if (not file_exist(outdir)) then spawn, 'mkdir ' + outdir
if (not file_exist(outdir2)) then spawn, 'mkdir ' + outdir2
;
;dirs = file_list(indir, '*')
if (who eq '') then dirs = concat_dir(indir, ['seq', 'frame']) $    ;only seq/frame
        else dirs = get_subdirs(indir)
if (keyword_set(qstop)) then stop
;
;--------------- Define the labels/links pairs
;
all_dirs = [dirs, get_subdirs(bdir)]
all_dirs = all_dirs(uniq(all_dirs, sort(all_dirs)))
;
label = ''
web_seq_map_links, httpdir, all_dirs, '.useq', label, link
web_seq_map_links, httpdir, all_dirs, '.ufrm', label, link
web_seq_map_links, httpdir, all_dirs, '.utim', label, link
if (n_elements(label) eq 0) then return     ;nothing found

link2 = link
link2 = str_replace(link2, '.ufrm.html" target="frm_src"', '.ufrm.html"')   ;remove target part for frames
link2 = str_replace(link2, '.useq.html" target="seq_src"', '.useq.html"')   ;remove target part for sequences
link2 = str_replace(link2, '.useq.html', '.useq.split.html')        ;update links to point to split screen

quick:
if (keyword_set(prep)) then return
if (keyword_set(qstop)) then stop
if (keyword_set(qtitle_only)) then goto, title
if (keyword_set(time_line)) then goto, time_line
;
;--------------- Do frames and sequences sources
;
ff = file_list(dirs, ['*.ufrm', '*.useq'], file=file)
outfiles = file + '.html'
;
regen = 1
for i=0,n_elements(ff)-1 do begin
    infil = ff(i)
    web_seq_do1, infil, outdir2, outfiles(i), file(i), label, link, regen=keyword_set(regen) and (i eq 0), httpdir=httpdir
end
;
;--------------- Do user timeline sources
;
ff = file_list(dirs, ['*.utim'], file=file)
outfiles = file + '.html'
;
for i=0,n_elements(ff)-1 do begin
    infil = ff(i)
    web_seq_do1, infil, outdir2, outfiles(i), file(i), label, link2, httpdir=httpdir
end
;
;
;--------------- Do Sequences split screen master
;
ff = file_list(dirs, '*.useq', file=file)
outfiles = file + '.split.html'

title:
;
for i=0,n_elements(ff)-1 do begin
    infil = ff(i)
    outfil = concat_dir(outdir2, outfiles(i))
    out = ['<HTML>', $
    '<HEAD>', $
    '<TITLE>' + file(i) + '</TITLE>', $
    '</HEAD>', $
    '<FRAMESET rows="4%,70%,*">', $
        '  <FRAME name="idx_src" src="' + concat_dir(httpdir2, 'split_top.html') + '">', $
        '  <FRAME name="seq_src" src="' + file(i) + '.html">', $
        '  <FRAME name="frm_src" src="blank.html">', $
    '</FRAMESET>', $
    '</HTML>']
    file_delete, outfil
    prstr, out, file=outfil
end
;
;---------------
;
web_seq_index, dirs, label, link2, out1, exten='*.useq', /seq
web_seq_index, dirs, label, link2, out2, exten='*.ufrm', /frame
;
outfil = concat_dir(outdir, 'alph.html')
file_delete, outfil
html_basics, outfil, title=strupcase(who) + ' Alphabetical Index of TRACE Sequences and Frames', /head, /simple
file_append, outfil, '<h1> Sequences </h1> '
file_append, outfil, out1
file_append, outfil, '<h1> Frames </h1> '
file_append, outfil, out2
html_basics, outfil, /trail, /simple

; ----  by ID
;
out1 = rd_tfile(concat_dir(bdir, 'dbase/id_map_seq.tab'))
ss = wc_where(out1, strupcase(who)+'*', nss)
if (nss eq 0) then out1 = '--- NONE ---' else out1 = out1(ss)
p = strpos(out1(0), '=')
if (p ne -1) then out1 = out1(sort(strmid(out1, p, 99)))

out2 = rd_tfile(concat_dir(bdir, 'dbase/id_map_frm.tab'))
ss = wc_where(out2(0), who+'*', nss)
if (nss eq 0) then out2 = '--- NONE ---' else out2 = out2(ss)
p = strpos(out2(0), '=')
if (p ne -1) then out2 = out2(sort(strmid(out2, p, 99)))

out = ['<h1> Sequences </h1>', out1, '<h1> Frames </h1> ', out2]
outfil = concat_dir(outdir, 'by_id.html')
web_seq_do_array, label, link2, out, outfil, tit=strupcase(who) + ' Index of TRACE Sequences and Frames by ID'

;
;---------------
;

web_seq_gen_index, dirs, label, link2, out, concat_dir(outdir, 'targ_used.html'), $
            tit=strupcase(who) + ' Targets Used Listing', /targets
web_seq_gen_index, dirs, label, link2, out, concat_dir(outdir, 'key_used.html'), $
            tit=strupcase(who) + ' Keyword Listing', /keywords

time_line:
if (who eq 'cjs') then web_seq_do_file, label, link2, $
    '/tsw/obs_dev/cjs/TRACE_30_day_timeline.txt', $
    concat_dir(outdir, 'TRACE_30_day_timeline.html'), $
    tit='TRACE First 30 Days of Science Timeline'
if (who eq 'com') then web_seq_do_file, label, link2, $
    '/tsw/obs_dev/com/trace_comm_timeline.txt', $
    concat_dir(outdir, 'trace_comm_timeline.html'), $
    tit='TRACE First 20 Days of Commisioning Timeline'
    


if (keyword_set(all_frames)) then web_seq_all_users
;
end
;------------------------------------------------------
pro web_seq_do_timelines_s1, infilnam, outfilnam, tit

common web_seq_blk1a, label, link, link2, dirs, all_dirs

if (n_elements(label) eq 0) then web_seq, /prep

if (not file_exist(concat_dir('/tsw/obs_dev/timeline', infilnam))) then begin
    print, 'Cannot find file: ' + concat_dir('/tsw/obs_dev/timeline', infilnam)
    return
end
;
web_seq_do_file, label, link2, $
    concat_dir('/tsw/obs_dev/timeline', infilnam), $
    concat_dir('$path_http/dbase/timeline', outfilnam), $
    tit=tit

end
;------------------------------------------------------
function web_seq_tlin_lin, mat, qin
;
;
;
dir = '/www/TRACE/dbase/timeline/'
fil = concat_dir(dir, mat(0))
if (qin) then out = '<tr>' $
    else out = ['<table border=2 cellpadding=2 cellspacing=1><tr>', $
        '<B><td>Description<td>Run Date<td>S/C Date<td>.<td>.<td>.<td>.<td>.</B><tr>']
if (file_exist(fil+'.html')) then out = [out, '<td><a HREF="' + mat(0)+'.html"><B>' + mat(1) + '</B> </a> </td>'] $
                else out = [out, '<td>.']
if (mat(2) ne '') then out = [out, '<td>' + mat(2)] $
                else out = [out, '<td>.']
if (mat(3) ne '') then out = [out, '<td>' + mat(3)] $
                else out = [out, '<td>.']
if (file_exist(fil+'.gif')) then out = [out, '<td><a HREF="' + mat(0)+'.gif"><B>M.Usage</B> </a> </td>'] $
                else out = [out, '<td>.']
if (file_exist(fil+'_1h.gif')) then out = [out, '<td><a HREF="' + mat(0)+'_1h.gif"><B>1st_Half</B> </a> </td>'] $
                else out = [out, '<td>.']
if (file_exist(fil+'_2h.gif')) then out = [out, '<td><a HREF="' + mat(0)+'_2h.gif"><B>2nd_Half</B> </a> </td>'] $
                else out = [out, '<td>.']
if (file_exist(fil+'.atim.html')) then out = [out, '<td><a HREF="' + mat(0)+'.atim.html"><B>ATIM</B> </a> </td>'] $
                else out = [out, '<td>.']
if (file_exist(fil+'.tlin')) then out = [out, '<td><a HREF="' + mat(0)+'.tlin"><B>TLIN</B> </a> </td>'] $
                else out = [out, '<td>.']
;
qin = 1
return, out
end
;------------------------------------------------------
pro web_seq_tlin_index
;
;
;
outfil = '/www/TRACE/dbase/timeline/index.html'
mat = rd_ulin_col('/tsw/obs_dev/timeline/index.tab', nocomment=';')
;
html_basics, outfil, title='TRACE Timelines', /head, /simple
;
n = n_elements(mat(0,*))
qin = 0
for i=2,n-1 do begin
    case mat(0,i) of
    'SECTION': begin
            if (qin) then file_append, outfil, '</table><br><br>'
            file_append, outfil, '<h1>' + mat(1,i) + '</h1>'
            qin = 0
           end
    '': begin
            if (not qin) then file_append, outfil, '<br>'
        end
    else: begin
            file_append, outfil, web_seq_tlin_lin(mat(*,i), qin)
        end
    endcase
end

if (qin) then file_append, outfil, '</table><br><br>'
html_basics, outfil, /trail, /simple

end
;------------------------------------------------------
pro web_seq_do_timelines

mat = rd_ulin_col('/tsw/obs_dev/timeline/index.tab', nocomment=';')
n = n_elements(mat(0,*))
for i=2,n-1 do begin
    case mat(0,i) of
    'SECTION': 
    '': 
    else: web_seq_do_timelines_s1, mat(0,i)+'.tim',  mat(0,i)+'.html', mat(1,i)
    endcase
end


;web_seq_do_timelines_s1, 'octsim1.tim',      'octsim1.html',        'Operations Simulation 9-Sep-97'

;web_seq_do_timelines_s1, 'barker1.tim',      'barker_970508.html',  'TRACE Barker on 8-May-97'
;web_seq_do_timelines_s1, 'ops_sim4.tim',     'ops_sim_970514.html', 'TRACE Ops-Sim on 14-May-97'
;web_seq_do_timelines_s1, 'logic_series1.tim','logic_series1.html',  'Logic Series'
;
;web_seq_do_timelines_s1, 'tv_trans1.tim',    'tv_trans1.html',      'Thermal Vacuum Transition #1'
;web_seq_do_timelines_s1, 'tv_trans2.tim',    'tv_trans2.html',      'Thermal Vacuum Transition #2'
;web_seq_do_timelines_s1, 'tv_trans3.tim',    'tv_trans3.html',      'Thermal Vacuum Transition #3'
;web_seq_do_timelines_s1, 'tv_barker.tim',    'tv_barker.html',      'Thermal Vacuum Barker'
;web_seq_do_timelines_s1, 'tv_3days1.tim',    'tv_3days1.html',      'Thermal Vacuum 3 Day Contam #1'
;web_seq_do_timelines_s1, 'tv_3days2.tim',    'tv_3days2.html',      'Thermal Vacuum 3 Day Contam #2'
;web_seq_do_timelines_s1, 'tv_3days3.tim',    'tv_3days3.html',      'Thermal Vacuum 3 Day Contam #3'

;web_seq_do_timelines_s1, 'tv_test1.tim',     'tv_test1.html',       'Pre-Thermal Vacuum Test #1'

ff = file_list('/tsw/obs_dev/timeline', '*.atim', file=file)
for i=0,n_elements(file)-1 do web_seq_do_timelines_s1, file(i), file(i)+'.html', file(i)

web_seq_tlin_index
end
;------------------------------------------------------