/*
OS/2 script doing a kind of 'make' on a TeX file. First the TeX
source if TeXed until the .aux-file stays constant. Then, if
the -p option is provided, the postscript file is generated.
No action is taken, when the file to create is more recent than
the corresponding source from which it is to create.
NOTE: The argument must be given without extension!

Syntax: texit [-p] <Texfilename-without-extension>

The -p option tells texit, that the resulting DVI file should be
processed by dvips afterwards.

First get out the basename of the file and the path where it is located
*/

call RxFuncAdd 'SysFileDelete', 'RexxUtil', 'SysFileDelete'
call RxFuncAdd 'SysFileTree', 'RexxUtil', 'SysFileTree'
TMP='c:\tmp'
postscript=0
current_dir=directory()
parse arg arguments
parse var arguments options rest
if options='-p' then
do
  postscript=1
  parse var arguments options arguments
end

do while \ (arguments='')
  parse var arguments filename1 arguments
  filename=stream(filename1'.TeX','c','query exists')
  if (filename='') then
     filename=stream(filename1,'c','query exists')
  if \ (filename='') then
  do
     bn=left(filespec("name",filename),length(filespec("name",filename))-4)
     path=filespec("drive",filename)filespec("path",filename)
     path=left(path,length(path)-1)
     call directory path
  end
  else
     bn='qwoeiruz'     /* avoid the file being found */
  /* Check, if TeXing is necessary. */
  if exists(bn'.TeX') then
  do
     if exists(bn'.dvi') & get_date(bn'.dvi')>get_date(bn'.TeX') then
        say 'texit: TeX started despite DVI file newer than TeX file...'

     do until \ index_changed
        index_changed=0
        if exists(bn'.idx') then
           'copy 'bn'.idx 'TMP' >nul'
        do until \ diff(bn'.aux', TMP'\'bn'.aux')
           /* First copy .aux and .idx file to allow finding differences later on */
           if exists(bn'.aux') then
              'copy 'bn'.aux 'TMP'\'bn'.aux >nul'
           'call gblatex 'bn'.TeX'
        end

        /* Now clean up the copied .aux-file */
        if exists(TMP'\'bn'.aux') then
           call SysFileDelete TMP'\'bn'.aux'

        /* At this point in each case a preliminary stable dvi-file has been
           built. Now look, if an index file (bn.idx) has been generated.
           The condition for checking, if the .idx-file has been generated
           is its date. If it's more recent than the bn.TeX file, then it is
           considered generated. */

        if diff(bn'.idx', TMP'\'bn'.idx') | (exists(bn'.idx') & \ exists(bn'.ind')) then
        do
        /* If the .idx-files differ, make the .ind-file using makeindex and
           rerun latex in order to insert this new index into the .dvi-file
        */
           index_changed = 1
           'makeindx 'bn
        end
     end  /* of do until \ indexchanged */

     /* Clean up the copied .idx-file */
     if exists(TMP'\'bn'.idx') then
        call SysFileDelete TMP'\'bn'.idx'

     /* Now look at postscript option: */
     if postscript then
        'call dvips 'bn
  end   /* if .TeX-File exists */
  else
     say 'texit: TeX-File not found!'
end
call directory current_dir
exit

/* *********************************************************************** */
diff: procedure;
/* *********************************************************************** */
  parse arg f1, f2
  /* If the names are equal, or both files do not exists,
     then the files are identical. They are considered different, if only
     one of the files does not exist. */
  if f1=f2 then
     return 0
  else if exists(f1) & \ exists(f2) then
     return 1
  else if exists(f2) & \ exists(f1) then
     return 1
  else if \ exists(f1) & \ exists(f2) then
     return 0
  else if \ (stream(f1,'c','query size')=stream(f2,'c','query size')) then
     return 1
  else
  do
     linein(f1,,0)
     linein(f2,,0)
     do while lines(f1) & lines(f2)
        l1=linein(f1)
        l2=linein(f2)
        if \ (l1=l2) then
        do
           call stream f1, 'c', 'close'
           call stream f2, 'c', 'close'
           return 1
        end
     end
     call stream f1, 'c', 'close'
     call stream f2, 'c', 'close'
     return 0
  end

/* *********************************************************************** */
exists: procedure;
/* *********************************************************************** */
  parse arg filename
  st=stream(filename,'c','query exists')
  if st='' then
     return 0
  else
     return 1

/* *********************************************************************** */
get_date: procedure;
/* *********************************************************************** */
  /* Returns the date of the file passed as argument in the format
     YY/MM/DD/HH/MM */
  parse arg filename
  call SysFileTree filename, ft, tf
  file_date=left(ft.1,14)
  return file_date