;batconfg.lsp
;Configures SLaTeX batfile/shellscript (CL version)
;(c) Dorai Sitaram, Rice U., 1991, 1994

#+gcl
(or (find-package :slatex) (make-package :slatex))

#-gcl
(defpackage slatex (:use cl))

(set-dispatch-macro-character #\# #\t
  #'(lambda (p ig ig2)
      (declare (ignore ig ig2))
      t))

(set-dispatch-macro-character #\# #\f
  #'(lambda (p ig ig2)
      (declare (ignore ig ig2))
      nil))

(format t "~&Beginning configuring command script -- wait...")

(defvar *slatex-directory* (directory-namestring *load-pathname*))

(defvar *op-sys*)
(defvar cl-pathname)
(defvar slatex-pathname)
(defvar texinputs)
(defvar texinputs-list)
(defvar accepts-echo)
(defvar accepts-cmdline-file)
(defvar accepts-initfile)
(defvar system-procedure nil)

#+clisp
(setf system-procedure 'run-shell-command)

(with-open-file (inp (concatenate 'string *slatex-directory*
                                  "config.dat")
                     :direction :input)
  (read inp) ;we already know dialect
  (setf *op-sys* (read inp)
	cl-pathname (read inp)
	slatex-pathname (read inp)
	texinputs (read inp)
	texinputs-list (read inp)
	accepts-echo (read inp)
	accepts-cmdline-file (read inp)
	accepts-initfile (read inp))  )

(defvar bat-file)
(setf bat-file
  (concatenate 'string *slatex-directory*
	       (case *op-sys*
		 ((os2 os2fat) "slatex.cmd")
		 ((windows dos) "slatex.bat")
		 (unix "slatex"))))

(unless (eq *op-sys* 'mac-os)
  (if (probe-file bat-file) (delete-file bat-file)))

(defun princn (x o)
    (princ x o)
    (terpri o))

(defun n (o)
  (terpri o))

(with-open-file 
    (o bat-file :direction :output)
  (case *op-sys*
        ((unix)
         (cond (accepts-echo
                (princn "echo '" o)
                (princ "(load " o)
                (prin1 slatex-pathname o)
                (princn ")" o)
                (princ "(setq slatex::*texinputs* " o)
                (prin1 texinputs o)
                (princn ")" o)
                (princ "(setq slatex::*texinputs-list* `" o)
                (prin1 texinputs-list o)
                (princn ")" o)
                (princ "(slatex::process-main-tex-file \"'$1'\")' | " o)
                (princn cl-pathname o))
               (accepts-cmdline-file
                (princ "echo '(load " o)
                (prin1 slatex-pathname o)
                (princn ")' > Zslatex.jnk" o)
                (princ "echo  '(setq slatex::*texinputs* " o)
                (prin1 texinputs o)
                (princn ")' >> Zslatex.jnk" o)
                (princ "echo '(setq slatex::*texinputs-list* `" o)
                (prin1 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)
                (princ cl-pathname o)
                (princ " " o)
                (princ accepts-cmdline-file o)
                (princn " Zslatex.jnk" o)
                (princn "rm -f Zslatex.jnk" o))
               (accepts-initfile
                (princ "echo '(load " o)
                (prin1 slatex-pathname o)
                (princ ")' > " o)
                (princn accepts-initfile o)
                (princ "echo '(setq slatex::*texinputs* " o)
                (prin1 texinputs o)
                (princ ")' >> " o)
                (princn accepts-initfile o)
                (princ "echo '(setq slatex::*texinputs-list* (quote " o)
                (prin1 texinputs-list o)
                (princ ")' >> " o)
                (princn accepts-initfile o)
                (princ "echo '(slatex::process-main-tex-file \"'$1'\")' >> " o)
                (princn accepts-initfile o)
                (princn cl-pathname o)
                (princ "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
                (princ "echo (load " o)
                (prin1 slatex-pathname o)
                (princn ") > Zslatex.jnk" o)
                (princ "echo (setq slatex::*texinputs* " o)
                (prin1 texinputs o)
                (princn ") >> Zslatex.jnk" o)
                (princ "echo (setq slatex::*texinputs-list* '" o)
                (prin1 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)
                (princ "echo (load \"Zslatex.jnk\") | " o)
                (princn cl-pathname o)
                (princn "del Zslatex.jnk" o))
               (accepts-cmdline-file
                (princ "echo (load " o)
                (prin1 slatex-pathname o)
                (princn ") > Zslatex.jnk" o)
                (princ "echo (setq slatex::*texinputs* " o)
                (prin1 texinputs o)
                (princn ") >> Zslatex.jnk" o)
                (princ "echo (setq slatex::*texinputs-list* '" o)
                (prin1 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)
                (princ cl-pathname o)
                (princ " " o)
                (princ accepts-cmdline-file o)
                (princn " Zslatex.jnk" o)
                (princn "del Zslatex.jnk" o))
               (accepts-initfile
                (princ "echo (load " o)
                (prin1 slatex-pathname o)
                (princ ") > " o)
                (princn accepts-initfile o)
                (princ "echo (setq slatex::*texinputs* " o)
                (prin1 texinputs o)
                (princ ") >> " o)
                (princn accepts-initfile o)
                (princ "echo (setq slatex::*texinputs-list* '" o)
                (prin1 texinputs-list o)
                (princ ") >> " o)
                (princn accepts-initfile o)
                (princ "echo (slatex::process-main-tex-file \"%1\") >> " o)
                (princn accepts-initfile o)
                (princ "echo (slatex::exit-scheme) >> " o)
                (princn accepts-initfile o)
                (princn cl-pathname o)
                (princ "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))))

(format t "~&Finished configuring command script.~%")

(when (eq *op-sys* 'unix)
  #+(or allegro clisp)
  (run-shell-command "chmod +x slatex")
  #+gcl
  (system "chmod +x slatex")
  #-(or gcl clisp)
  (format t "~&Type (chmod +x slatex) on Unix command line~%"))
