; $Id: dave_online_help.pro,v 1.3 2005/08/19 13:46:31 rdimeo Exp $
; +
; NAME:
;  DAVE_ONLINE_HELP
;
; PURPOSE:
;  This function launches the new online-help available in
;  IDL 6.2.  The programmer must specify the directory in
;  which the ADP files are located and the name of the base
;  ADP file that will be merged with all other exogenous
;  ADP files.
;
; RETURN VALUE:
;  1 indicating success or 0 indicating failure.
;
; REQUIRED KEYWORDS:
;  PATH:          String specifying the location of the HTML and
;                 ADP files.
;  BASE_ADP_FILE: String specifying the name of the base ADP file
;                 (not fully qualified path).  The base ADP file must
;                 contain a <profile> </profile> tag pair wrapped in
;                 a <assistantconfig> </assistantconfig> pair.
;
; OPTIONAL KEYWORDS:
;  PARENT:        Widget parent (Default:0L)
;
; REQUIRED PROGRAMS:
;  NONE
;
;
; EXAMPLE BASE_ADP_FILE syntax:
;
;  <!-- Assistant Document Profile file for SEUTIL.PRO-->
;  <!DOCTYPE DCF> <assistantconfig version="3.3.0">
;     <profile>
;        <property name="name">SEUTIL Version 1.0</property>
;        <property name="title">SEUTIL Help System</property>
;        <property name="startpage">home.html</property>
;        <property name="assistantdocs">C:\RSI\IDL62\help\online_help</property>
;     </profile>
;  </assistantconfig>
;
; INDIVIDUAL ADP FILES syntax:
;
;   <DCF ref="home.html" title="Help for the 1-D Schrodinger Equation Application">
;    <section ref="seutil_application_description.html" title="Application Description">
;        <keyword ref="seutil_application_description.html">Hamiltonian</keyword>
;        <keyword ref="seutil_application_description.html">Schrodinger</keyword>
;    </section>
;    <section ref="seutil_application_interface.html" title="Application Interface">
;        <keyword ref="seutil_application_interface.html">Hamiltonian</keyword>
;    </section>
;   </DCF>
;
; REQUIREMENTS:
;  IDL 6.2 and higher
;
; CALLING SEQUENCE:
;  XRES
;
; COMMON BLOCKS:
;  None
;
; AUTHOR:
;  Robert M. Dimeo, Ph.D.
;  NIST Center for Neutron Research
;  100 Bureau Drive
;  Gaithersburg, MD 20899
;  Phone: (301) 975-8135
;  E-mail: robert.dimeo@nist.gov
;  http://www.ncnr.nist.gov/staff/dimeo
;
; MODIFICATION HISTORY:
;  Written by RMD 08/11/05
;
; LICENSE:
;  The software in this file is written by an employee of
;  National Institute of Standards and Technology
;  as part of the DAVE software project.
;
;  The DAVE software package is not subject to copyright
;  protection and is in the public domain. It should be
;  considered as an experimental neutron scattering data
;  reduction, visualization, and analysis system. As such,
;  the authors assume no responsibility whatsoever for its
;  use, and make no guarantees, expressed or implied,
;  about its quality, reliability, or any other
;  characteristic. The use of certain trade names or commercial
;  products does not imply any endorsement of a particular
;  product, nor does it imply that the named product is
;  necessarily the best product for the stated purpose.
;  We would appreciate acknowledgment if the DAVE software
;  is used or if the code in this file is included in another
;  product.
;
; -
; ************************************************************ ;
function dave_online_help, path = path,                        $
                           base_adp_file = base_adp_file,      $
                           parent = parent
compile_opt idl2,hidden
if n_elements(parent) eq 0 then parent = 0L
if !version.release lt 6.2 then begin
   strout = 'Must use IDL 6.2 or higher to use this function'
   void = dialog_message(strout,dialog_parent = parent,/error)
   return,0B
endif
if n_elements(path) eq 0 then begin
   strout = 'Keyword PATH must be specified'
   void = dialog_message(strout,/error,dialog_parent = parent)
   return,0B
endif
if n_elements(base_adp_file) eq 0 then begin
   strout = 'Keyword BASE_ADP_FILE must be specified'
   void = dialog_message(strout,/error,dialog_parent = parent)
   return,0B
endif

temp_filename = file_basename(base_adp_file,'.adp')
new_file_name = temp_filename+'_total.adp'
book_file = path+new_file_name
base_file = path+base_adp_file

all_adp_files = file_search(path+'*.adp')
adp_names = file_basename(all_adp_files)
nfiles = n_elements(adp_names)
match = where(adp_names eq new_file_name,count_match)
if count_match eq 1 then file_delete,book_file
all_adp_files = reverse(file_search(path+'*.adp'))
adp_names = file_basename(all_adp_files)
nfiles = n_elements(adp_names)
for i = 0,nfiles-1 do begin
   if all_adp_files[i] ne base_file then begin
      nlines = file_lines(all_adp_files[i])
      new_addition = strarr(nlines)
      openr,lun,all_adp_files[i],/get_lun
      readf,lun,new_addition
      free_lun,lun,/force
      if n_elements(add_string) eq 0 then  $
         add_string = new_addition         $
      else                                $
         add_string = [add_string,new_addition]
   endif
endfor
if n_elements(add_string) eq 0 then begin
   strout = 'No ADP files found'
   void = dialog_message(strout,dialog_parent = parent,/error)
   return,0B
endif
add_lines = n_elements(add_string)

search_item = '</profile>' ; The string after which we'll add the new XML stuff from ADD_FILE
nlines = file_lines(base_file)
base_text = strarr(nlines)
openr,lun,base_file,/get_lun
readf,lun,base_text
free_lun,lun,/force

trimmed = strtrim(base_text,2)
index = where(trimmed eq search_item,count)
if count eq 0 then return,0B
base_end = base_text[(index+1):(nlines-1)]

new_lines = nlines+add_lines
new_string = strarr(new_lines)
new_string[0:index] = base_text[0:index]
new_string[(index+1):index+add_lines] = add_string
new_string[(index+add_lines+1):*] = base_end
; Write the new string out to a file
openw,lun,book_file
printf,lun,new_string
free_lun,lun,/force

online_help,book = book_file

return,1B
end
; *********************************************************** ;