slides_setup.rkt
(module common racket

(require mzlib/etc)
(require racket/system)

(display "\nbystroTeX setup\n")
(display "===============\n")
(display "You need to have installed on your computer\n")
(display "the program called ``javac''\n")
(display "If you do have it, press ENTER to continue\n")

(display (read-line))

(display "We have a sample slide presentation, which you can use\n")
(display "as a template for preparing your own talk.\n")
(display "Please enter the ABSOLUTE path of the folder for these sample files\n")
(display "(You can use the symbol ~ for home directory)\n")
(display "Then press ENTER:\n")

(define sampledir (path->string (expand-user-path (string->path (read-line)))))
(unless (directory-exists? (string->path sampledir))
  (make-directory (string->path sampledir)))
(system (string-append "cp -a " (path->string (this-expression-source-directory)) "/example-slides/* " sampledir "/"))

(display "\nNow we have to install some Java files\n")
(display "Enter the ABSOLUTE path of the directory where you want to keep these Java files:\n")
(define javadir (path->string (expand-user-path (string->path (read-line)))))
(unless (directory-exists? (string->path javadir))
  (make-directory (string->path javadir)))
(display "\n\n")

(define javapath (string->path javadir))

(display "Go to http://forge.scilab.org/index.php/p/jlatexmath/downloads/ \ndownload the latest jlatexmath-x.x.x.jar\n")
(display (string-append "and save it to the folder " javadir "\n"))
(display "Press ENTER when done\n")
(display (read-line))

(define jarpath-jlatexmath 
  (let ([jlatexps (find-files 
                   (lambda (x) 
                     (regexp-match #rx".*/jlatexmath-.*\\.jar$" (path->string x)))
                   (string->path javadir))])
    (unless (pair? jlatexps) (error 
                              (string-append "*** ERROR: Could not find the jlatexmath jar file in"
                                             javadir
                                             "\nperhaps something went wrong with the download?\n" 
                                             "Please try again\n")))
    (when   (pair? (cdr jlatexps)) (error 
                                    (string-append "*** ERROR: More than one jar file found in"
                                                   javadir
                                                   "\nplease cleanup\n")))
    (car jlatexps)))

;; (define jarpath-snakeyaml
;;   (let ([snakeyamlps (find-files
;;                       (lambda (x)
;;                         (regexp-match #rx".*/snakeyaml-.*\\.jar$" (path->string x)))
;;                       (string->path javadir))])
;;     (unless (pair? snakeyamlps) (error
;;                                  (string-append "*** ERROR: Could not find the snakeyaml jar file in"
;;                                                 javadir
;;                                                 "\nperhaps something went wrong with the download?\n"
;;                                                 "Please try again\n")))
;;     (when   (pair? (cdr snakeyamlps)) (error
;;                                        (string-append "*** ERROR: More than one jar file found in"
;;                                                       javadir
;;                                                       "\nplease cleanup\n")))
;;     (car snakeyamlps)))

(display (path->string (this-expression-source-directory)))

(system (string-append "cp '" (path->string (this-expression-source-directory)) "ShowMath.java' '" javadir "'"))
(system (string-append "cp '" (path->string (this-expression-source-directory)) "LaTeXServer.java' '" javadir "'"))
(system (string-append "cp '" (path->string (this-expression-source-directory)) "server-control.sh' '" javadir "'"))
(system (string-append "javac -classpath '" (path->string jarpath-jlatexmath) "' '" javadir "/" "ShowMath.java'"))
(system (string-append "javac -classpath '" 
                       (path->string jarpath-jlatexmath) 
                       "' '" javadir "/" "LaTeXServer.java'"))

(define abs-javapath javapath)
(define abs-jarpath-jlatexmath jarpath-jlatexmath)
;(define abs-jarpath-snakeyaml jarpath-snakeyaml)

(define out (open-output-file (build-path javapath "amkhlv-java-formula.sh") #:exists 'replace))
(display "#!/usr/bin/env bash\n\n" out)
(display "# uncomment the line below if want to use slow version:\n" out)
(display (string-append 
          "# java -classpath '" 
          (path->string abs-javapath) ":" (path->string abs-jarpath-jlatexmath) 
          "' ShowMath \"$1\" \"$2\" \"$3\" \n\n") out)
(display "# comment the lines below if want to use slow version:\n" out)
(display (string-append
          "export AMKHLV_JAVA_PATH='" (path->string abs-javapath) "'\n") out)
(display (string-append 
          "export   JLATEXMATH_JAR='" (path->string abs-jarpath-jlatexmath) "'\n") out)
;; (display (string-append
;;           "export    SNAKEYAML_JAR='" (path->string abs-jarpath-snakeyaml) "'\n") out)
(display (string-append 
          (path->string abs-javapath) "/server-control.sh \"$@\" \n\n") out)
(close-output-port out)

(system (string-append "chmod +x " (path->string (build-path javapath "amkhlv-java-formula.sh"))))

(display "\n\nFinally, one more thing to do\n")
(display (string-append 
          "The directory " (path->string javapath) " contains the file: \n amkhlv-java-formula.sh \n"
          "You should move it to some place on your $PATH, and make sure that it is executable\n"
          "Once this is done, the slide-system should be ready to use.\n\n"
          ))
)