;batconfg.scm;-*-scheme-*-
;Configures SLaTeX batfile/shellscript (Scheme version)
;(c) Dorai Sitaram, Rice U., 1991, 1994

(display "Beginning configuring command script -- wait...")
(newline)

(define dialect 'forward)
(define *op-sys* 'forward)
(define scheme-pathname 'forward)
(define slatex-pathname 'forward)
(define texinputs 'forward)
(define texinputs-list 'forward)
(define accepts-echo 'forward)
(define accepts-cmdline-file 'forward)
(define accepts-initfile 'forward)
(define system-procedure #f)

(call-with-input-file "config.dat"
  (lambda (ip)
    (set! dialect (read ip))
    (set! *op-sys* (read ip))
    (set! scheme-pathname (read ip))
    (set! slatex-pathname (read ip))
    (set! texinputs (read ip))
    (set! texinputs-list (read ip))
    (set! accepts-echo (read ip))
    (set! accepts-cmdline-file (read ip))
    (set! accepts-initfile (read ip))
    (cond ((or (eof-object? dialect)
               (eof-object? *op-sys*)
               (eof-object? scheme-pathname)
               (eof-object? slatex-pathname)
               (eof-object? texinputs)
               (eof-object? texinputs-list)
               (eof-object? accepts-echo)
               (eof-object? accepts-cmdline-file)
               (eof-object? accepts-initfile))
           (error "config.dat has too few answers"))
          ((eof-object? (read ip)) #t)
          (else (error "config.dat has too many answers")))))

(case dialect
  ((bigloo chez cscheme guile mzscheme scm stk)
   (set! system-procedure 'system)))

(define bat-file 'forward)

(case *op-sys*
  ((os2 os2bat)
   (set! bat-file "slatex.cmd"))
  ((windows dos)
   (set! bat-file "slatex.bat"))
  ((unix)
   (set! bat-file "slatex")))

;(if (memq *op-sys* '(unix windows dos os2fat os2)) ;why here?

(if (memq dialect '(bigloo chez cscheme guile mzscheme pcsge scm))
    (if (file-exists? bat-file)
        (delete-file bat-file)))

(define modified-newline newline)

(cond ((and (eq? dialect 'mzscheme)
            (memq *op-sys* '(windows dos)))
       (set! modified-newline
             (let ((cr (integer->char 13))
                   (lf (integer->char 10)))
               (lambda (o)
                 (display cr o)
                 (display lf o))))))

(define princn
  (lambda (x o)
    (display x o)
    (modified-newline o)))

(call-with-output-file bat-file
  (lambda (o)
    (case *op-sys*
      ((unix)
       (cond (accepts-echo
              (princn "echo '" o)
              (display "(load " o)
              (write slatex-pathname o)
              (princn ")" o)
              (display "(set! slatex::*texinputs* " o)
              (write texinputs o)
              (princn ")" o)
              (display "(set! slatex::*texinputs-list* `" o)
              (write texinputs-list o)
              (princn ")" o)
              (display "(slatex::process-main-tex-file \"'$1'\")' | " o)
              (princn scheme-pathname o))
             (accepts-cmdline-file
              (display "echo '(load " o)
              (write slatex-pathname o)
              (princn ")' > Zslatex.jnk" o)
              (display "echo  '(set! slatex::*texinputs* " o)
              (write texinputs o)
              (princn ")' >> Zslatex.jnk" o)
              (display "echo '(set! slatex::*texinputs-list* `" o)
              (write texinputs-list o)
              (princn ")' >> Zslatex.jnk" o)
              (princn "echo '(slatex::process-main-tex-file \"'$1'\")' >> Zslatex.jnk" o)
              (princn "echo '(slatex::exit-scheme)' >> Zslatex.jnk" o)
              (display scheme-pathname o)
              (display " " o)
              (display accepts-cmdline-file o)
              (princn " Zslatex.jnk" o)
              (princn "rm -f Zslatex.jnk" o))
             (accepts-initfile
              (display "echo '(load " o)
              (write slatex-pathname o)
              (display ")' > " o)
              (princn accepts-initfile o)
              (display "echo '(set! slatex::*texinputs* " o)
              (write texinputs o)
              (display ")' >> " o)
              (princn accepts-initfile o)
              (display "echo '(set! slatex::*texinputs-list* (quote " o)
              (write texinputs-list o)
              (display ")' >> " o)
              (princn accepts-initfile o)
              (display "echo '(slatex::process-main-tex-file \"'$1'\")' >> " o)
              (princn accepts-initfile o)
              (princn scheme-pathname o)
              (display "rm -f " o)
              (princn accepts-initfile o)))
       (princn "if test -f pltexchk.jnk" o)
       (princn "then tex $1; rm pltexchk.jnk" o)
       (princn "else latex $1" o)
       (princn "fi" o))
      ((windows dos os2fat os2)
       (princn "@echo off" o)
       (cond (accepts-echo
              (display "echo (load " o)
              (write slatex-pathname o)
              (princn ") > Zslatex.jnk" o)
              (display "echo (set! slatex::*texinputs* " o)
              (write texinputs o)
              (princn ") >> Zslatex.jnk" o)
              (display "echo (set! slatex::*texinputs-list* '" o)
              (write texinputs-list o)
              (princn ") >> Zslatex.jnk" o)
              (princn "echo (slatex::process-main-tex-file \"%1\") >> Zslatex.jnk" o)
              (princn "echo (slatex::exit-scheme) >> Zslatex.jnk" o)
              (display "echo (load \"Zslatex.jnk\") | " o)
              (princn scheme-pathname o)
              (princn "del Zslatex.jnk" o))
             (accepts-cmdline-file
              (display "echo (load " o)
              (write slatex-pathname o)
              (princn ") > Zslatex.jnk" o)
              (display "echo (set! slatex::*texinputs* " o)
              (write texinputs o)
              (princn ") >> Zslatex.jnk" o)
              (display "echo (set! slatex::*texinputs-list* '" o)
              (write texinputs-list o)
              (princn ") >> Zslatex.jnk" o)
              (princn "echo (slatex::process-main-tex-file \"%1\") >> Zslatex.jnk" o)
              (princn "echo (slatex::exit-scheme) >> Zslatex.jnk" o)
              (display scheme-pathname o)
              (display " " o)
              (display accepts-cmdline-file o)
              (display " Zslatex.jnk" o)
              (princn "del Zslatex.jnk" o))
             (accepts-initfile
              (display "echo (load " o)
              (write slatex-pathname o)
              (display ") > " o)
              (princn accepts-initfile o)
              (display "echo (set! slatex::*texinputs* " o)
              (write texinputs o)
              (display ") >> " o)
              (princn accepts-initfile o)
              (display "echo (set! slatex::*texinputs-list* '" o)
              (write texinputs-list o)
              (display ") >> " o)
              (princn accepts-initfile o)
              (display "echo (slatex::process-main-tex-file \"%1\") >> " o)
              (princn accepts-initfile o)
              (display "echo (slatex::exit-scheme) >> " o)
              (princn accepts-initfile o)
              (princn scheme-pathname o)
              (display "del " o)
              (princn accepts-initfile o)))
       (princn "if exist pltexchk.jnk goto one" o)
       (princn "goto two" o)
       (princn ":one" o)
       (princn "call tex %1" o)
       (princn "del pltexchk.jnk" o)
       (princn "goto end" o)
       (princn ":two" o)
       (princn "call latex %1" o)
       (princn ":end" o)))))

(display "Finished configuring batfile/shellscript")
(newline)
(if (eq? *op-sys* 'unix)
    (case system-procedure
      ((system) (system "chmod +x slatex"))
      (else
       (display "Type (chmod +x slatex) on Unix command line")
       (newline))))
