c	H2l_auto.F
c	Help 2 latex converter
c	Convert a VMS help file (*.hlp) to simple latex (*.tex) 
c	Reads in from a file rather than interactively.

c      testing main

        implicit none
c..	functions
	integer		trimlen, trimsemicolin
c	logical		ask_ifd
	CHARACTER*80	firstword

c	functions
	logical		makehelp2latex	

c...	local variables
        integer 	leveloff, unit, unitpath
	integer 	unitlatex, unitdvips
	integer		unitmake
c	integer		unitcom
        CHARACTER*80  	helpPATH, docpath, NODE, latexfile, basefile, os
c	CHARACTER*80	lscom, pipecom, 
c	CHARACTER*80	dvipscom
c	CHARACTER*80	execom
c	CHARACTER*132	listingcom
        CHARACTER*132  	aline
	logical		morefiles, verbose
	character*1	CTRLI
	character*10	extn
	integer		istart

      CTRLI=CHAR(9)   ! TAB

	morefiles = .true.
	verbose = .false.	!! Determines how noisy the reporting is.

c	Use leveloff = 0 for article style, with highest level \section.
c	Use leveloff = 1 for report style with highest level \chapter	
c	leveloff = 0
	leveloff = 1


	write (*,*) 'Program help2latex_auto'
	write (*,*) '  Converts VMS help files *.hlp to *.tex.'
	write (*,*) '  Creates latex.com, to be run under VMS, for *.dvi '
	write (*,*) '  Creates dvips.com, to be run under VMS, for *.ps '
	write (*,*) '  Creates Makefile_tex., to be run under Unix,', 
     &		' for *.dvi and *.ps and *.html'
	write (*,*) ' '
	write (*,*) ' File h2l.path must contain:'
	write (*,*) '   Line 1: directory path containing *.hlp files'
	write (*,*) '   Line 2: target directory path for *.tex files'
	write (*,*) '   Line 3: Operating system (vms, g77, decunix)' 
c	write (*,*) '   Line 3: dvips option (so goes to file)' 
c        write (*,*) '     E.g. "-o" for Linux'
c        write (*,*) '     E.g. " " for VMS'

c	write (*,*) ' Line 3: directory command to get list of ', 
c     &		'files, one per line'
cc        write (*,*) '   E.g. "dir/col=1/noheadings/notrailing',
cc     &		'/output=h2l.lis" for VMS'
c        write (*,*) '   E.g. "dir/col=1/noheadings/notrailing" for VMS'
c	write (*,*) '   E.g., "ls -1" for linux'
c	write (*,*) ' Line 4: redirection to file command '
c        write (*,*) '   E.g. "/output=" for VMS'
c	write (*,*) '   E.g., ">" for linux'
cc        write (*,*) '   E.g. "" for VMS'
cc	write (*,*) '   E.g., ">h2l.lis" for linux'
c	write (*,*) ' Line 5: dvips option (so goes to file)' 
c        write (*,*) '   E.g. "-o" for Linux'
c	write (*,*) ' Line 6: shell command to run file ', 
c     &		'latex.com and dvips.com'
c        write (*,*) '   E.g. "source" for Linux'
c        write (*,*) '   E.g. "@" for VMS'
	write (*,*) ' '
	write (*,*) ' Output file h2l.dat should contain a listing of ', 
     &		'all *.hlp files to be converted,'
	write (*,*) ' including the path'
        write (*,*) '   Under VMS, create h2l.dat via:'
        write (*,*) ' "dir/col=1/notrailing', 
     &		'/output=h2l.dat [tanya.for.help]*.hlp" '
c        write (*,*) ' "dir/col=1/noheadings/notrailing', 
c     &		'/output=h2l.dat [tanya.for.help]*.hlp" '
        write (*,*) '   Under unix, create h2l.dat via:'
	write (*,*) '	  "ls -1 ~/for/help > h2l.dat" '
c	write (*,*) '	"ls -1 ~/for/help/*.hlp > h2l.dat" '
       write (*,*) ' '



c	goto 105
c 100	write (*,*) 'Use leveloff = 0 for article style, ', 
c     &		'with highest level \section.'
c	write (*,*) 'Use leveloff = 1 for report style ', 
c     &		'with highest level \chapter'
c	write (*,*) 'Enter leveloff: '
c	read (*,*) leveloff
c	if (.not.((leveloff .eq.0) .or. (leveloff .eq.1))) goto 100
c 105	continue


c        call ask1d('Enter help file path>>', '80a', path)
c        call ask1d('Enter help file name, no extention>>', '80a', node)

        call new_unit(unit)
        if (unit .eq. -1) then
	  goto 666
        endif
	write (*,*) 'Opening old file h2l.dat ', unit
	open(unit, file='h2l.dat', status='old', err=666)

        call new_unit(unitpath)
        if (unitpath .eq. -1) then
	  goto 666
        endif
	write (*,*) 'Opening old file h2l.path ', unitpath
	open(unitpath, file='h2l.path', status='old', err=666)

c        call new_unit(unitcom)
c        if (unit .eq. -1) then
c	  goto 666
c        endif
c	write (*,*) 'Opening old file h2l_lis.com ', unitcom
c	open(unit, file='h2l_lis.com', status='unknown', err=666)

        call new_unit(unitmake)
        if (unitmake .eq. -1) then
	  goto 666
        endif
        write (*,*) 'Opening new file Makefile_tex ', unitmake
	open(unitmake, file='Makefile_tex.', status='unknown', err=666)

        call new_unit(unitdvips)
        if (unitdvips .eq. -1) then
	  goto 666
        endif
        write (*,*) 'Opening new file dvips.com ', unitdvips
	open(unitdvips, file='dvips.com', status='unknown', err=666)

        call new_unit(unitlatex)
        if (unitlatex .eq. -1) then
	  goto 999
        endif
        write (*,*) 'Opening new file latex.com ', unitlatex
	open(unitlatex, file='latex.com', status='unknown', err=666)

        write (*,*) ' '
	read(unitpath,'(a)') helppath
	write (*,*) 'helppath = ', helppath
	read(unitpath,'(a)') docpath
	write (*,*) 'docpath = ', docpath
	read(unitpath,'(a)') os
	write (*,*) 'os = ', os

c	read(unitpath,'(a)') lscom
c	write (*,*) 'lscom = ', lscom
c	read(unitpath,'(a)') pipecom
c	write (*,*) 'pipecom = ', pipecom
c	read(unitpath,'(a)') dvipscom
c	write (*,*) 'dvipscom = ', dvipscom
c	read(unitpath,'(a)') execom
c	write (*,*) 'execom = ', execom
        write (*,*) ' '


	istart = 1
	if ((os(1:trimlen(os)) .eq. 'vms') .or. 
     &      (os(1:trimlen(os)) .eq. 'VMS')) then
	  istart = 2
	endif

	if ((os(1:trimlen(os)) .eq. 'g77') .or. 
     &      (os(1:trimlen(os)) .eq. 'G77')) then
	  istart = 2
	endif

        if ((os(1:trimlen(os)) .eq. 'decunix') .or.
     &      (os(1:trimlen(os)) .eq. 'DECUNIX')) then
          istart = 1
        endif


c        write (*,*) 'Creating temporary file h2l_lis.com ',
c     &          '(for directory listings) using SYSTEM call.'
c	listingcom = lscom(1:trimlen(lscom))//'$'//
c     &		docpath(1:trimlen(docpath))//'*.hlp'//
c     &		pipecom(1:trimlen(pipecom))//'h2l.lis'
c	write (*,*) 'Directory command constructed is:'
c	write (*,*) listingcom
c	write 
c        write (*,*) ' '
c	write (unitcom, '(a)') listingcom(2:132)
c	call system('@h2l_lis.com ') 

c        write (*,*) 'Creating temporary file h2l.lis ',
c     &          '(for directory listings) using SYSTEM call.'
c	listingcom = lscom(1:trimlen(lscom))//'$'//
c     &		docpath(1:trimlen(docpath))//'*.hlp'//
c     &		pipecom(1:trimlen(pipecom))
cc     &		pipecom(1:trimlen(pipecom))//'h2l.lis'
c	write (*,*) 'Directory command constructed is:'
c	write (*,*) listingcom
c        write (*,*) ' '
c	call system(' '//listingcom(1:trimlen(listingcom)))

c        call new_unit(unit)
c        if (unit .eq. -1) then
c	  goto 666
c        endif
c	write (*,*) 'Opening old temporary file h2l.lis ', 
c     &		'(for directory listings)'
c	open(unit, file='h2l.lis', status='old', err=666)

	write (aline, *) 'all : process'
	write (unitmake, '(a)') aline(istart:132)
	write (unitmake, *) ' '
c	write (aline, *) 'DVIPSOPS = ', dvipscom(1:trimlen(dvipscom))
c	write (unitmake, '(a)') aline(istart:132)
c	write (aline, *) 'DVIPS = ', dvipscom(1:trimlen(dvipscom))
c	write (unitmake, '(a)') aline(istart:132)
	write (unitmake, *) ' '
	write (aline, *) 'process :'
	write (unitmake, '(a)') aline(istart:132)

c	write (unitlatex, *) 'all : latex'
c	write (unitlatex, *) ' '
c	write (unitlatex, *) '	latex :'
c
c
c	write (unitlatex, *) 'all : dvips'
c	write (unitlatex, *) ' '
c	write (unitlatex, *) 'dvips :'

	do while (morefiles) 
 	  read(unit,'(a)', err=650, end=600) aline
	  node = firstword(aline)
c	  write (*,*) 'file = ', node(1:trimlen(node))

	  if (trimlen(node) .gt. 4) then

c		File name already contains the path.
c	    basefile = node(1:(trimsemicolin(node)-4))
c	    latexfile = node(1:(trimsemicolin(node)-4))//'.tex'

	    basefile = docpath(1:trimlen(docpath))//
     &		node(1:(trimsemicolin(node)-4))
	    latexfile = docpath(1:trimlen(docpath))//
     &		node(1:(trimsemicolin(node)-4))//'.tex'
	    extn = node((trimsemicolin(node)-2):trimsemicolin(node))
	    if ((extn(1:3) .ne. 'hlp') .and.
     &          (extn(1:3) .ne. 'HLP')) then
	      write (*,*) 'SKIPPING file (wrong extension "', 
     &		extn(1:3), '") ', 
     &		node(1:trimsemicolin(node))
	      goto 500
	    endif
c           call ask1d('Enter latex file name >>', '80a', latexfile)
	
cWrong		Note: extension extn is already in the file name.
cWrong		And the path is in the filename (NODE) too!
c	    if (makehelp2latex( ' ', NODE, ' ', 
c     &		latexfile, leveloff, verbose) ) then

	    if (makehelp2latex( helpPATH, NODE, ' ', 
     &		latexfile, leveloff, verbose) ) then
	      write (aline, *) '$ latex ', basefile(1:trimlen(basefile))
	      write (unitlatex, '(a)') aline(istart:80) 
	      write (unitlatex, '(a)') aline(istart:80) 

	      write (aline, *) '$ dvips ', 
     &		basefile(1:trimlen(basefile))
	      write (unitdvips, '(a)') aline(istart:80) 
	      
	      write (aline, *) '$ ps2pdf ', 
     &		basefile(1:trimlen(basefile))
	      write (unitdvips, '(a)') aline(istart:80) 
c	      write (unitdvips, *) ' dvips ', 
c     &		dvipscom(1:trimlen(dvipscom)), ' ', 
c     &		basefile(1:trimlen(basefile))


	      write (aline, *) CTRLI, '(cd ', 
     &		docpath(1:trimlen(docpath))//'; latex ',
     &		node(1:(trimsemicolin(node)-4))//')'
c     &		//'; cd ..)'
	      write(unitmake, '(a)') aline(istart:132)
	      write(unitmake, '(a)') aline(istart:132)

c	      write (aline, *) CTRLI, 'latex ', 
c     &		basefile(1:trimlen(basefile))
cc	      write (*,*) 'makefile: ', aline(1:trimlen(aline))
c	      write(unitmake, '(a)') aline(istart:132)
c	      write(unitmake, '(a)') aline(istart:132)

c	      write (*,*) 'Executing: ', aline(1:trimlen(aline))
c	      call system(aline)

	      write (aline, *) CTRLI, '(cd ', 
     &		docpath(1:trimlen(docpath))//'; dvips -o ',
     &		node(1:(trimsemicolin(node)-4))//'.ps '//
     &		node(1:(trimsemicolin(node)-4))//')'
c     &		//'; cd ..)'
	      write(unitmake, '(a)') aline(istart:132)

	      write (aline, *) CTRLI, '(cd ', 
     &		docpath(1:trimlen(docpath))//'; ps2pdf ',
     &		node(1:(trimsemicolin(node)-4))//'.ps '//
     &		node(1:(trimsemicolin(node)-4))//'.pdf )'
c     &		//'; cd ..)'
	      write(unitmake, '(a)') aline(istart:132)

c	      write (aline, *) CTRLI, 'dvips -o ',
c     &		basefile(1:trimlen(basefile))//'.ps ',
c     &		basefile(1:trimlen(basefile))
cc	      write (aline, *) CTRLI, 'dvips $(DVIPSOPS) ', 
cc     &		basefile(1:trimlen(basefile))
ccc              write (*,*) 'makefile: ', aline(1:trimlen(aline))
c	      write(unitmake, '(a)') aline(2:132)
cc	      write (*,*) 'Executing: ', aline(1:trimlen(aline))
cc	      call system(aline)

	      write (aline, *) CTRLI, '(cd ', 
     &		docpath(1:trimlen(docpath))//'; '//
     &          'latex2html -split 0 -show_section_numbers ',
     &		node(1:(trimsemicolin(node)-4))//'.tex)'
c     &			//'; cd ..)'
	      write(unitmake, '(a)') aline(istart:132)

c	      write (aline, *) CTRLI, 'latex2html -split 0 ', 
c     &		'-show_section_numbers ', 
c     &		basefile(1:trimlen(basefile))//'.tex'
cc              write (*,*) 'makefile: ', aline(1:trimlen(aline))
c	      write(unitmake, '(a)') aline(istart:132)

	    else
		Write (*,*) 'makehelp2latex, returned error for file'
                Write (*,*) NODE
	    endif

 500	    continue
	  endif
	  goto 680

 600	  continue
	  write (*,*) 'help2latex: Last file converted.'
	  morefiles = .false.
	  goto 680

 650	  continue
	  write (*,*) 'help2latex: Error reading in next file name' 

 680	  continue 
	  write (*,*) ' '

	enddo

	close (unit)
	close (unitpath)
c	write (unitlatex, *) ' '
	close (unitlatex)
c	write (unitdvips, *) ' '
	close (unitdvips)

	write (unitmake, *) ' '
	write (aline, *) 'clean :'
	write (unitmake, '(a)') aline(istart:80)
	write (aline, *) CTRLI, '(cd ', 
     &		docpath(1:trimlen(docpath))//'; '//
     &          'rm *.aux *.toc *.log)'
c	write (aline, *) CTRLI, 'rm *.aux *.toc *.log'
	write (unitmake, '(a)') aline(istart:80)
	close (unitmake)

c	aline = execom(1:trimlen(execom))//'latex.com'
c	write (*,*) 'Executing shell command ', aline
c	call system(aline)

c	aline = execom(1:trimlen(execom))//'dvips.com'
c	write (*,*) 'Executing shell command ', aline
c	call system(aline)

	goto 999
 666	  continue
	  write (*,*) 'help2latex: Error opening file ' 

 999	continue
	end

c========================================================
      logical function makeHELP2latex ( PATH, NODE, extn, 
     &		latexfile, leveloff, verbose) 
      IMPLICIT NONE
c...	passed variables
      CHARACTER  PATH*(*), NODE*(*), extn*(*), latexfile*(*)
c      EXTERNAL LBR$OUTPUT_HELP, LIB$GET_INPUT, LIB$PUT_OUTPUT
c      INTEGER*4 LBR$OUTPUT_HELP, LIB$GET_INPUT, LIB$PUT_OUTPUT
	logical		verbose

c...	functions
      INTEGER TRIMSTART, TRIMSTOP

c...	local variables
c      INTEGER ERR
      CHARACTER*132 HELPFILE
      integer hunit, lunit, leveloff


c      write (*,*) 'HELP-STUPID: ', path
c      write (*,*) 'HELP-STUPID: ', node

	makeHELP2latex  = .false.
c      err = 1
      IF ((PATH .EQ. ' ') .AND. (NODE .EQ. '    ')) THEN
	HELPFILE = 'users'//extn
c	HELPFILE = 'users.hlp'
c	HELPFILE = 'MSRHELP:users.hlp'

      ELSE IF ((PATH .NE. ' ') .AND. (NODE .EQ. '    ')) THEN
	HELPFILE = PATH(trimstart(PATH):trimstop(PATH))//'users'//extn
c	HELPFILE = PATH(trimstart(PATH):trimstop(PATH))//'users.hlp'

      ELSE IF ((PATH .EQ. ' ') .AND. (NODE .NE. '    ')) THEN
        HELPFILE = 
     &		node(trimstart(node):trimstop(node)) // extn
c        HELPFILE = 
c     &		node(trimstart(node):trimstop(node)) // '.hlp'
cc        HELPFILE = 'MSRHELP:' // 
cc     &		node(trimstart(node):trimstop(node)) // '.hlp'

      ELSE IF ((PATH .NE. ' ') .AND. (NODE .NE. '    ')) THEN
        HELPFILE = PATH(trimstart(PATH):trimstop(PATH))//
     &          node(trimstart(node):trimstop(node)) // extn
c        HELPFILE = PATH(trimstart(PATH):trimstop(PATH))//
c     &          node(trimstart(node):trimstop(node)) // '.hlp'
      ENDIF
      
c      write (*,*) 'HELP-STUPID: ', HELPFILE(1:TRIMSTOP(HELPFILE))
c      write (*,*) 'HELP-STUPID: help not implimented'

      call new_unit(hunit)
      if (hunit .eq. -1) then
	goto 999
      endif
      write (*,*) 'makeHELP2latex: Opening ', 		!! hunit, ' ',
     &		helpfile(1:TRIMSTOP(HELPFILE))
      open(hunit, file=HELPFILE, status='old', err=666)

      call new_unit(lunit)
      if (lunit .eq. -1) then
	goto 999
      endif
      write (*,*) 'makeHELP2latex: Opening ', 		!! lunit, ' ',
     &		latexfile(1:TRIMSTOP(latexFILE))
      open(lunit, file=latexFILE, status='unknown', err=668)

      makeHELP2latex  = .true.

      goto 700
 666  write (*,*) 'Can not open help file to read ', 
     &		HELPFILE(1:TRIMSTOP(HELPFILE))
      goto 999

      goto 700
 668  write (*,*) 'Can not open output latex file ', 
     &		latexFILE(1:TRIMSTOP(latexFILE))

      goto 999

 700  continue
      call startlatex(lunit, latexfile, helpfile, leveloff)
      call help2latex(hunit, lunit, 1, leveloff, verbose)
      call endlatex(lunit)

 999  continue
      close(hunit)
      close(lunit)

      return
      END

c=======================================================================
      subroutine help2latex(hunit, lunit, level, leveloff, verbose)
      implicit none
c...	passed variables
      integer 	hunit, lunit, level, leveloff
      logical	verbose

c...	common
      character*20 topictree(10)
      integer	topicdepth
      common /tree/ topictree, topicdepth
	
c...	functions
      character*20 nextword 

c...	local variables
      logical looking
      character aline*(132)
      character*10 digits
      parameter (digits = '1234567890')
      integer idigit
      character*20 lineword
      integer len_lineword
      integer i
      integer iline
      logical haveline

      rewind (hunit)

      looking  = .true.
      iline = 0
 132  format(a132)
      topicdepth = 0
      haveline = .false.
      do while (looking)
	i = 1
        if (.not. haveline) then
c	  otherwise, line is already read in by report_help.
	  read(hunit, 132,end=600,err=555) aline
          iline = iline + 1
	endif

c	write (*,*) 'help2latex:', haveline, iline, ' ...', aline(1:40)

	idigit = index(digits,aline(1:1))
	if (idigit .eq. 0) then		!! Not a topic line, so skip it 
	  goto 556
	endif

        i = 2
	if (i .gt. len(aline)) then
	  goto 556
	endif

c 	      /* Grab topic off the line */
        lineword = nextword(aline, i, len_lineword)

	topicdepth = idigit
	if (topicdepth .gt. 10) then
	  write (*,*) 'help2latex: topic depth too large ', topicdepth 
	  return
	else
	  topictree(topicdepth) = lineword(1:len_lineword)
	endif

c        write (*,*) 'help2latex: ', iline, ' help topic is ', idigit, 
c     &		'..', lineword(1:len_lineword), '..'
	call report_section(lunit, leveloff, idigit, verbose) 
ccc     &		lineword(1:len_lineword))

	call report_help(hunit, lunit, idigit, iline, aline, haveline)

        if (iline .eq. 0) then
	  goto 999
	endif
	
	goto 556
 555    looking = .false.	
 556    continue
      enddo
 560  continue

      goto 999
 600  write (*,*) 'dohelp: End of help file.'
      iline = 0
 999  return

      end


c=======================================================================
      subroutine report_section(lunit, leveloff, level, verbose)
ccc	, topicword) 

      implicit none
c...	passed variables
      integer leveloff, level
	integer lunit
ccc      character topicword*(*) 
	logical	verbose

c...	common
      character*20 topictree(10)
      integer	topicdepth
      common /tree/ topictree, topicdepth
	
c...	functions
c	integer trimstop

c...	local variables
      integer len_branch, len_label, len_llabel
      character*200 thisbranch, sectionlabel, longlabel

      call makebranch(thisbranch, len_branch,1,10, ':', .false.)
      if (verbose) then
        write (*,*)  thisbranch(1:len_branch)
c        write (*,*) 'report_section: ', thisbranch(1:len_branch)
      endif
	
      call makebranch(longlabel, len_llabel,1,level,':', .true.)

      if (level-leveloff .eq. 0) then
c	write (lunit,*) '\chapter{', topicword, '}'
        call makebranch(sectionlabel, len_label,level,level,' ', .true.)
	write (lunit,*) '\chapter[',sectionlabel(1:len_label)
     &		,']{', longlabel(1:len_llabel), '}' 
c     &         ,'}'

      else if (level-leveloff .eq. 1) then
c	write (lunit,*) '\section{', topicword, '}'
        call makebranch(sectionlabel, len_label,level,level,' ', .true.)
	write (lunit,*) '\section[',sectionlabel(1:len_label)
     &		,']{', longlabel(1:len_llabel), '}' 
c     &		,'}'

      else if (level-leveloff .eq. 2) then
c	write (lunit,*) '\subsection{', topicword, '}'
        call makebranch(sectionlabel, len_label,level,level,' ', .true.)
	write (lunit,*) '\subsection[',sectionlabel(1:len_label)
     &		,']{', longlabel(1:len_llabel), '}' 
c     &		,'}'

      else if (level-leveloff .eq. 3) then
c	write (lunit,*) '\subsubsection{', topicword, '}'
        call makebranch(sectionlabel, len_label,level,level,' ', .true.)
	write (lunit,*) '\subsubsection[',sectionlabel(1:len_label)
     &		,']{', longlabel(1:len_llabel), '}' 
c     &		,'}'

cc	!! Now, I think, there are no deeper subsections in Latex.

      else if (level-leveloff .eq. 4) then
        call makebranch(sectionlabel, len_label,level,level,' ', .true.)
	write (lunit,*) '\paragraph[',sectionlabel(1:len_label)
     &		,']{', longlabel(1:len_llabel), '}' 
c     &		,'}'
c        call makebranch(sectionlabel, len_label,level-1,10, ' ', .true.)
c	write (lunit,*) '\subsubsection{',sectionlabel(1:len_label)
c     &		,'}'

      else if (level-leveloff .eq. 5) then
        call makebranch(sectionlabel, len_label,level,level,' ', .true.)
	write (lunit,*) '\subparagraph[',sectionlabel(1:len_label)
     &		,']{', longlabel(1:len_llabel), '}' 
c     &		,'}'
c        call makebranch(sectionlabel, len_label,level-2,10, ' ', .true.)
c	write (lunit,*) '\subsubsection{',sectionlabel(1:len_label)
c     &		,'}'

      else if (level-leveloff .eq. 6) then
        call makebranch(sectionlabel, len_label,level-1,10, ' ', .true.)
	write (lunit,*) '\subsubsection[',sectionlabel(1:len_label)
     &		,']{', longlabel(1:len_llabel), '}' 
c     &		,'}'

      else if (level-leveloff .eq. 7) then
        call makebranch(sectionlabel, len_label,level-2,10, ' ', .true.)
	write (lunit,*) '\subsubsection[',sectionlabel(1:len_label)
     &		,']{', longlabel(1:len_llabel), '}' 
c     &		,'}'

      else if (level-leveloff .eq. 8) then
        call makebranch(sectionlabel, len_label,level-3,10, ' ', .true.)
	write (lunit,*) '\subsubsection[',sectionlabel(1:len_label)
     &		,']{', longlabel(1:len_llabel), '}' 
c     &		,'}'

      else if (level-leveloff .eq. 9) then
        call makebranch(sectionlabel, len_label,level-4,10, ' ', .true.)
	write (lunit,*) '\subsubsection[',sectionlabel(1:len_label)
     &		,']{', longlabel(1:len_llabel), '}' 
c     &		,'}'

      else if (level-leveloff .eq. 10) then
        call makebranch(sectionlabel, len_label,level-5,10, ' ', .true.)
	write (lunit,*) '\subsubsection[',sectionlabel(1:len_label)
     &		,']{', longlabel(1:len_llabel), '}' 
c     &		,'}'

      endif

ccc	This won't be proper latex. And don't need links anyway.
ccc      write (lunit,*) '\label{', thisbranch(1:len_branch) , '}'
      write (lunit, *) '%', thisbranch(1:len_branch)

c	This path is now in the section label
c      write (lunit, *) '{\sl'
c      write (lunit, *) '\begin{verbatim}'
c      write (lunit, *) thisbranch(1:len_branch)
c      write (lunit, *) '\end{verbatim}'
c      write (lunit, *) '\sl}'

c      write (lunit,*) ' '

      end


c=======================================================================
      subroutine makebranch(thisbranch, len_branch, startlevel, 
     &		 maxlevel, separator, uselatex)
      implicit none
      character thisbranch*(*)
      integer len_branch, startlevel, maxlevel, itopic
      character*1 separator
      logical uselatex

c...	functions
	integer trimstop

c...	common
      character*20 topictree(10)
      integer	topicdepth
      common /tree/ topictree, topicdepth

c...	local
      integer ll, j, i, trouble
      character*12 lalatex !! 123456789012
      parameter (lalatex =    '&$#%_{}^~\<>')
 
      j = 1
      do itopic = startlevel, min(topicdepth, maxlevel)
	if ((itopic .ne. startlevel) .and. 
     &		(j+1 .le. len(thisbranch))) then
	  thisbranch(j:j) = separator
	  j = j +1
	endif
	ll = trimstop(topictree(itopic))
	if (j+ ll +1 .le. len(thisbranch)) then
	  if (useLatex) then
	   do i = 1, ll
	     trouble = index(lalatex,topictree(itopic)(i:i))
ccc	     jold = j
	     if (trouble .eq. 0) then
	        thisbranch(j:j) = topictree(itopic)(i:i)
          	j = j +1
	     else if (trouble .eq. 1) then
	        thisbranch(j:j+1) = '\&'
          	j = j +2
	     else if (trouble .eq. 2) then
	        thisbranch(j:j+1) = '\$'
          	j = j +2
	     else if (trouble .eq. 3) then
	        thisbranch(j:j+1) = '\#'
          	j = j +2
	     else if (trouble .eq. 4) then
	        thisbranch(j:j+1) = '\%'
          	j = j +2
	     else if (trouble .eq. 5) then
	        thisbranch(j:j+1) = '\_'
          	j = j +2
	     else if (trouble .eq. 6) then
	        thisbranch(j:j+1) = '\{'
          	j = j +2
	     else if (trouble .eq. 7) then
	        thisbranch(j:j+1) = '\}'
          	j = j +2
	     else if (trouble .eq. 8) then
	        thisbranch(j:j+7) = '$\wedge$'
          	j = j +8
	     else if (trouble .eq. 9) then
	        thisbranch(j:j+5) = '\tidle'
          	j = j +6
	     else if (trouble .eq. 10) then
	        thisbranch(j:j+10) = '$\setminus$'
          	j = j +11
	     else if (trouble .eq. 11) then
	        thisbranch(j:j+2) = '$<$'
          	j = j +3
	     else if (trouble .eq. 12) then
	        thisbranch(j:j+2) = '$>$'
          	j = j +3
	     endif
c	     write (*,*) 'makebranch:', jold, j, thisbranch(jold:j)
	   enddo

	  else
	    thisbranch(j:(j+ll)) = topictree(itopic)(1:ll)
	    j = j + ll 
	  endif
	endif
      enddo

      len_branch = j -1
     
      return
      end

c=======================================================================
      subroutine report_help(hunit,lunit,level, iline, aline, haveline) 
      implicit none
c...	passed variables
      integer hunit, level
	integer lunit
      integer iline
      character aline*(132)
      logical haveline

c...	common

c...	functions

c...	local variables
      logical looking
      character*10 digits
      parameter (digits = '1234567890')
      integer idigit
	
      looking  = .true.
 132  format(a132)
c 79   format(x, a79)
 80   format(a80)
c 81   format('%',a79)

      write (lunit, *) '\begin{verbatim}'
      do while (looking)
	haveline = .false.
	read(hunit, 132,end=600,err=555) aline
	haveline = .true.
        iline = iline + 1
	idigit = index(digits,aline(1:1))
	if (idigit .eq. 0) then		!! Not a topic line, so print it
	  if (aline(1:1) .ne. '!') then
	    write (lunit,80) aline(1:80)
c	  else 
cc	Doesn't work when in verbatim mode!
c	    write (lunit,81) aline(2:80)
	  endif
	  goto 556
	else if (idigit .lt. level) then !! Start of supertopic, 
cDebug          write (*,*) 'report_help: Stopping. supertopic ', idigit, 
cDebug     &		iline,  '..', aline(1:20), '..'
	  goto 555			 !! so break out of loop
	else if (idigit .gt. level) then	!! Start of subtopic, so skip
cDebug          write (*,*) 'report_help: Stopping. subtopic ', idigit, 
cDebug     &		iline, '..', aline(1:20), '..'
	  goto 555
        else
cDebug          write (*,*) 'report_help: Stopping. same level ', 
cDebug     &		level, idigit, iline,  '..', aline(1:20), '..'
	  goto 555
	endif
	
	goto 556
 555    looking = .false.	
 556    continue
      enddo
 560  continue

      goto 999
 600  write (*,*) 'report_help: End of help file.'
      iline = 0

 999  continue
      write (lunit, *) '\end{verbatim}'
      write (lunit, *) ' '
      write (lunit, *) ' '

      return

      end

c=======================================================
      character*20 function nextword(aline, apos, len_nextword)
c...	passed variables
      character aline*(*)
      integer apos, len_nextword

c...	local variables
      integer pos, i, next

      pos = apos
      do while ((aline(pos:pos) .eq. ' ') .and. (pos .lt. len(aline)) ) 
        pos = pos + 1	!! skip blanks
      enddo
      next = pos
      do while ((next.lt.len(aline)) .and. 
     &		.not.(aline(next:next).eq.' ')) 
	next = next + 1
      enddo

c	Case where there are no trailing blanks.
      if ((next .eq. len(aline) ) .and. 
     &		.not.(aline(next:next).eq.' ')) then
	next = next + 1
      endif

      if (next .eq. pos) then 
	len_nextword = 0
      else
        len_nextword = next-pos
        nextword(1:len_nextword) = aline(pos:next-1)
      endif
      apos = next

      do i = next, 20
        nextword(i:i) = ' '
      enddo

c	write (*,*) 'nextword:', len_nextword, ' ..', 
c     &		nextword(1:len_nextword), '..'

c      if (nextword(1:1) .eq. '@') then
c	write (*,*) 'nextword: AT ', len_nextword, ' ..', 
c     &		nextword(1:len_nextword), '..'
c      endif

      return
      end

c============================================
      subroutine new_UNIT(hunit)
      implicit none
      integer hunit
      logical LUOPEN

      DO 100 hunit = 30, 99
        INQUIRE(UNIT=hunit,OPENED=LUOPEN,ERR=100)
        IF(.NOT. LUOPEN) goto 110
100   CONTINUE
      WRITE (*,*) '** Unable to find an un-opened unit for file **'
      hunit = -1
      return
110   continue

      return
      end

c==========================================================
	subroutine endlatex(lunit)
	implicit none
	integer lunit

        write (lunit, *) ' '
ccc        write (lunit, *) '\backmatter'
	write (lunit, *) '\end{document}'
	end


c==========================================================
	subroutine startlatex(lunit, latexfile, helpfile, leveloff)
	implicit none
	integer lunit, leveloff
        character latexfile*(*), helpfile*(*)
c...	functions
	integer trimstop

c	Use leveloff = 0 for article style, with highest level \section.
c	Use leveloff = 1 for report style with highest level \chapter	

c	  write (lunit, *) '\documentclass[10pt,a4paper]{book}'

       if (leveloff .eq. 0) then
	  write (lunit, *) '\documentclass[10pt,a4paper]{article}'
       else if (leveloff .eq. 1) then
	  write (lunit, *) '\documentclass[10pt,a4paper]{report}'
       endif
       write (lunit, *) '\usepackage[dvips]{hyperref}'
c       write (lunit, *) '\usepackage[dvips,'
c	write (lunit, *) '% colorlinks=true,'
c	write (lunit, *) '% linkcolor=blue,'
c	write (lunit, *) '% citecolor=pinegreen,'
c	write (lunit, *) '% pdftitle={My title},'
c	write (lunit, *) '% pdfauthor={Dr Tanya M Riseman},'
c	write (lunit, *) '% pdfsubject={NIST center for Neutron Research},' 
c	write (lunit, *) '% bookmarks=true'
c	write (lunit, *) '       ]{hyperref}'


	write (lunit, *) '\begin{document}'
	write (lunit, *) '\pagestyle{headings}'
c	Set section number to 5 to go all the way to subparagraph
	write (lunit, *) '\setcounter{secnumdepth}{5}'
	write (lunit, *) '\setcounter{tocdepth}{5}'
	write (lunit, *) '\title{'
	write (lunit, *) latexfile(1:trimstop(latexfile))
        write (lunit, *) '\newline created from help file \newline'
	write (lunit, *) helpfile(1:trimstop(helpfile)), '}'
	write (lunit, *) ' '
	write (lunit, *) '\date{\today}'
	write (lunit, *) '\maketitle'
	write (lunit, *) '\tableofcontents'
	write (lunit, *) ' '
	write (lunit, *) ' '

	end

c==============================================================================
c23456789_123456789_123456789_123456789_123456789_123456789_123456789_12
        integer function trimlen(thestring)
c       Return the length of the string, ignoring trailing spaces ' '
c       and trailing zeros (from uninitialized strings).

        implicit none
        character*(*) thestring

        integer         lens
        logical         looking

        lens = len(thestring)
        trimlen = lens
        looking = .true.
        do while (looking .and. (trimlen .ge. 1))
          if ((thestring(trimlen:trimlen) .ne. ' ') .and.
     &          (ichar(thestring(trimlen:trimlen)) .ne. 0) ) then
            looking = .false.
          else
            trimlen = trimlen - 1
          endif
        enddo

        return
        end

c======================================================================
        integer function trimstart(thestring)
c Return the position of the first non-blank character of the string,
c where blanks are trailing spaces ' ' and zeros (from uninitialized strings).

        implicit none
        character*(*) thestring

        integer         lens, n

        lens = len(thestring)
        n = 1
        do while ((n .lt. lens) .and.  ((thestring(n:n) .eq. ' ') .or.
     &          (ichar(thestring(n:n)) .eq. 0) ))
          n = n + 1
        enddo

        trimstart = n

        return
        end

c======================================================================
        integer function trimstop(thestring)
c       Return the length of the string, ignoring trailing spaces ' '
c       and trailing zeros (from uninitialized strings).

        implicit none
        character*(*) thestring

        integer         n

        n = len(thestring)
        do while ((n .ge. 1) .and.  ((thestring(n:n) .eq. ' ') .or.
     &          (ichar(thestring(n:n)) .eq. 0) ))
          n = n - 1
        enddo

        trimstop = n

        return
        end

c======================================================================
        integer function trimsemicolin(thestring)
c       Return the length of the string, ignoring trailing spaces ' '
c       and trailing zeros (from uninitialized strings).
c	and trailing ";<version number>".

        implicit none
        character*(*) thestring

        integer         n, n2

        n = len(thestring)
        do while ((n .ge. 1) .and.  ((thestring(n:n) .eq. ' ') .or.
     &          (ichar(thestring(n:n)) .eq. 0) ))
          n = n - 1
        enddo

	n2 = 1
        do while ((n2 .lt. n) .and.  (thestring(n2:n2) .ne. ' ') .and.
     &          (ichar(thestring(n2:n2)) .ne. 0)  .and.
     &		(thestring(n2:n2) .ne. ';'))
          n2 = n2 + 1
        enddo

	if (thestring(n2:n2) .eq. ';') then
          n2 = n2 - 1
	endif

        trimsemicolin = n2

        return
        end


c======================================================================
        character*80 function firstword(thestring)
c       Returns the first word in thestring. 

        implicit none
        character*(*) thestring

c	functions
	integer trimstart, trimstop

c	local variables
        integer         n1, n2, nmax

	n1 = trimstart(thestring)

        n2 = n1
	nmax = trimstop(thestring)
	do while ((thestring(n2:n2) .ne. ' ') .and. (n2 .lt. nmax))
	  n2 = n2 + 1
	enddo

	firstword = thestring(n1:n2)
	
        return
        end

