/* make_web_index.rexx -- Make "web:index.html" with links to all directories.
 *
 * This program is copyrighted freeware. All rights reserved.
 * Copyright (C) 1999 by Thomas Aglassinger <agi@sbox.tu-graz.ac.at>
 *
 * See http://www.giga.or.at/~agi/wgetrexx/ for more information.
 */
version = '$VER: make_web_index.rexx 1.2 (23.10.99)'

Call AddLib('rexxsupport.library', 0, -30, 0)

Signal On NoValue

/* Name of target file to be created */
index_name = 'web:index.html'

list_name = 't:web.list'
sorted_list_name = 't:web.sort'

linefeed = '0a'x
verbose = 1

/* Handle CLI arguments */
Parse Upper Arg argument
if argument = 'QUIET' then do
   verbose = 0
end
else if argument = 'DEBUG' then do
   verbose = 2
end
else if argument ~= '' then do
   Parse Var version '$VER: ' version
   Say version || ' -- Make "' || index_name || '".'
   Say 'Usage: rx make_web_index.rexx [Quiet|Debug]'
   Exit
end

Address Command 'list web: dirs lformat=%p%n:::%c >' || list_name
Address Command 'sort from=' || list_name || ' to=' || sorted_list_name

if ~Open('list', sorted_list_name, 'read') then do
   Say 'Cannot read "' || sorted_list_name || '"'
   Exit 10
end
else if ~Open('index', index_name, 'write') then do
   Close('list')
   Say 'Cannot write "' || index_name || '"'
   Exit 10
end

WriteLn('index', '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">')
WriteLn('index', '<html><head>')
WriteLn('index', '<title>Web Index</title>')
WriteLn('index', '</head><body>')
WriteLn('index', '<h1>Web Index</h1>')

do while ~eof('list')
   line = ReadLn('list')
   Parse Var line 'web:' server ':::' comment
   if server ~= '' then do
      call say_verbose(server)
      directory = 'web:' || server || '/'
      welcome = find_welcome(directory)
      if welcome = '' then do
         call say_debug('   Cannot find suitable welcome document. Scanning whole directory')
         welcome = find_some_html(directory)
      end
      if welcome ~= '' then do
         Parse Var welcome 'web:' welcome
         entry = '<a href="' || welcome || '">' || server || '</a>'
         if comment ~= '' then do
            entry = entry || ' - ' || comment
         end
         call say_debug('   Found "' || welcome || '"')
      end
      else do
         entry = server
         Say '   Cannot find any document. Specify one in "welcome.web".'
      end
      entry = entry || '<br>'
      WriteLn('index', entry)
   end
end

WriteLn('index', '<hr>')
WriteLn('index', '<small>Last update: ' || Date() || '</small>')
WriteLn('index', '</body></html>')

Say
Say 'done.'

Close('index')
Close('list')

Exit 0

/* Scan subdirectories for useable welcome document. */
find_welcome: procedure expose linefeed verbose
   Parse Arg directory
   welcome = ''
   welcome_web_name = directory || 'welcome.web'
   if Open('pointer', welcome_web_name) then do
      welcome = directory || ReadLn('pointer')
      Call Close('pointer')
      if ~exists(welcome) then do
         Say '   Cannot find document specified in "welcome.web": "' || welcome || '"'
         Say '   Ignoring document and trying to find a different one'
         welcome = ''
      end
   end

   if welcome = '' then do
      welcome = find_some_welcome(directory)
      if welcome = '' then do
         directories = ShowDir(directory, 'dir', linefeed) || linefeed
         do until (welcome ~= '') | (next = '')
            Parse Var directories next (linefeed) directories
            if next ~= '' then do
               scanning = directory || next || '/'
               welcome = find_welcome(scanning)
               if welcome = '' then do
                  welcome = find_some_welcome(scanning)
               end
            end
         end
      end
   end

   Return welcome

/* File useable as welcome document in `directory' or "" if none available. */
find_some_welcome: procedure expose linefeed verbose
   Parse Arg directory
   welcome = ''
   if welcome = '' then welcome = welcome_exists(directory || 'welcome.html')
   if welcome = '' then welcome = welcome_exists(directory || 'default.html')
   if welcome = '' then welcome = welcome_exists(directory || 'index.html')
   if welcome = '' then welcome = welcome_exists(directory || 'welcome.htm')
   if welcome = '' then welcome = welcome_exists(directory || 'default.htm')
   if welcome = '' then welcome = welcome_exists(directory || 'index.htm')
   Return welcome

/* Check if `file' exists and return `file' or "" if not. */
welcome_exists: procedure expose verbose
   Parse Arg file

   call say_debug('   try "' || file || '"')
   if exists(file) then
      Return file
   else
      Return ''

/* First html file within path or "" if none available */
find_some_html: procedure expose linefeed verbose
   Parse Arg directory

   html = ''

   files = ShowDir(directory, 'file', linefeed) || linefeed
   do until (html ~= '') | (files = '')
      Parse Var files next (linefeed) files
      if is_html_name(next) then do
         html = directory || next
      end
   end

   if html = '' then do
      directories = ShowDir(directory, 'dir', linefeed) || linefeed
      do until (html ~= '') | (next = '')
         Parse Var directories next (linefeed) directories
         if next ~= '' then do
            scanning = directory || next || '/'
            call say_debug('   scan "' || scanning || '"')
            html = find_some_html(scanning)
         end
      end
   end

   Return html

/* Is file an html file? */
is_html_name: procedure
   Parse Arg file
   Return (Right(file,5) = '.html') | (Right(file,4) = '.htm') | (Right(file,6) = '.shtml') | (Right(file,4) = '.sht')

/* Display message in verbsoe mode */
say_verbose: procedure expose verbose
   if verbose > 0 then do
      Parse Arg message
      Say message
   end
   Return

say_debug: procedure expose verbose
   if verbose > 1 then do
      Parse Arg message
      Say message
   end
   Return

/* Handler for uninitialized variables */
NoValue:
    Signal Off NoValue
    Say
    Say SIGL SourceLine(SIGL)
    Say 'make_web_index.rexx: error in line ' || SIGL || ': uninitialized value.'
    Exit 20

