! sqrsh (sqr shell)
! Copyright (C) 1997 Ray Ontko & Company
!
! This program is free software; you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation; either version 2 of the License, or
! (at your option) any later version.
!
! This program is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with this program; if not, write to the Free Software
! Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
!
!++
!
! gensqrsh
!
! reads a skeleton sqrsh file (pre-loop and post-loop)
! and also reads a list of commands, each command must
! have its own local procedure with the same name as
! the command, and must have a file called command.sqh
! which will be copied into the gensqrsh program
! at the end.  It could also generate the sqrsh.hlp
! file which could contain a single line for each
! program.
!
!--
!
!
! Author
!
! Ray Ontko
! Ray Ontko & Company
! Richmond, In 47375
!
!
! Modification History
!
! Name             Date       Comment
! ---------------- ---------- ----------------------------------------
! Ray Ontko        1997/06/01 Added this header
!

!
! To Do
!
! needs to be restructured to add support for fixup_files
!
! we need to decide whether we want people to be able to
! enhance what we have by including their own, or whether
! we want to distribute code, or what.
!
! the file format might be:
! command [file_name] [# of params] [short help message or usage]

!%% - extracted BEGIN-SETUP section begins
!
! ! gensqrsh setup begins
! ! gensqrsh setup ends
!
!%% - extracted BEGIN-SETUP section ends

!--------------------------------------------------------------- gensqrsh
begin-procedure gensqrsh local
! read file sqrsh.sqr
! read file sqrsh.dat
! write setup section for each file
! read file sqrsh.dat
! write when for each file
! read file sqrsh.dat
! write #include for each file
if isnull( $_p1 )
   input $sqr_fn 'Template file [sqrsh.sqr]'
   if isnull( $sqr_fn )
      move 'sqrsh.sqr' to $sqr_fn
   end-if
else
   move $_p1 to $sqr_fn
end-if

if isnull( $_p2 )
   input $dat_fn 'Configuration file [sqrsh.dat]'
   if isnull( $dat_fn )
      move 'sqrsh.dat' to $dat_fn
   end-if
else
   move $_p2 to $dat_fn
end-if

if isnull( $_p3 )
   input $gen_fn 'Generated file [sqrsh.gen]'
   if isnull( $gen_fn )
      move 'sqrsh.gen' to $gen_fn
   end-if
else
   move $_p3 to $gen_fn
end-if

open $sqr_fn as 1 for-reading record={SQRSH_LINE_MAX}:vary
open $gen_fn as 2 for-writing record={SQRSH_LINE_MAX}:vary
read 1 into $line:{SQRSH_LINE_MAX}
move 1 to #copying
while not #_end-file
   if #copying
      write 2 from $line
      if $line = '!!%% - generated BEGIN-SETUP section begins'
         move 0 to #copying
         open $dat_fn as 3 for-reading record={SQRSH_LINE_MAX}:vary
         read 3 into $fn:{SQRSH_LINE_MAX}
         while not #_end-file
            if substr($fn,1,1) <> '!!'
               move $fn to $sqr_file
               concat '.sqh' with $sqr_file
               open $sqr_file as 4 for-reading record={SQRSH_LINE_MAX}:vary status=#setup_status
               if #setup_status = 0
                  move 0 to #copying_setup
                  read 4 into $setup:{SQRSH_LINE_MAX}
                  while not #_end-file
                     if #copying_setup
                        if $setup = '!!%% - extracted BEGIN-SETUP section ends'
                           move 0 to #copying_setup
                        else
                           if substr($setup,1,1) = '!!'
                              let $setup = substr($setup,2,length($setup)-1)
                              write 2 from $setup
                           end-if
                        end-if
                     else
                        if $setup = '!!%% - extracted BEGIN-SETUP section begins'
                           move 1 to #copying_setup
                        end-if
                     end-if
                     read 4 into $setup:{SQRSH_LINE_MAX}
                  end-while
                  close 4
               end-if
            end-if
            read 3 into $fn:{SQRSH_LINE_MAX}
         end-while
         close 3
           ! ! open the file $fn.sqr and scan for !%% - BEGIN-SETUP sections
      end-if
      if $line = '!!%% - generated WHEN section begins'
         move 0 to #copying
         open $dat_fn as 3 for-reading record={SQRSH_LINE_MAX}:vary
         read 3 into $fn:{SQRSH_LINE_MAX}
         while not #_end-file
            if substr($fn,1,1) <> '!!'
               uppercase $fn
               write 2 from '            when = ''' $fn ''''
               lowercase $fn
               write 2 from '               do ' $fn
               write 2 from '               break'
            end-if
            read 3 into $fn:{SQRSH_LINE_MAX}
         end-while
         close 3
      end-if
      if $line = '!!%% - generated INCLUDE section begins'
         move 0 to #copying
         open $dat_fn as 3 for-reading record={SQRSH_LINE_MAX}:vary
         read 3 into $fn:{SQRSH_LINE_MAX}
         while not #_end-file
            if substr($fn,1,1) <> '!!'
               write 2 from '#include ''' $fn '.sqh'''
            end-if
            read 3 into $fn:{SQRSH_LINE_MAX}
         end-while
         close 3
      end-if
   else
      if substr($line,1,3) = '!!%%'
         move 1 to #copying
         write 2 from $line
      end-if
   end-if
   read 1 into $line:{SQRSH_LINE_MAX}
end-while
close 1
close 2
!
! do an autodoc on each input file
!
open $dat_fn as 3 for-reading record={SQRSH_LINE_MAX}:vary
read 3 into $fn:{SQRSH_LINE_MAX}
while not #_end-file
   if substr($fn,1,1) <> '!!'
      move $fn to $sqr_file
      concat '.sqh' with $sqr_file
      move $fn to $hlp_file
      concat '.hlp' with $hlp_file
      do autodoc_file( $sqr_file , $hlp_file )
   end-if
   read 3 into $fn:{SQRSH_LINE_MAX}
end-while
close 3
end-procedure ! gensqrsh
