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 name of folder for these sample files\n")
(display "relative to the current directory.\n")
(display "For example, if the current directory is /home/ububutu\n")
(display "and you want sample files in /home/ubuntu/bystroslides\n")
(display "then just enter the word ``bystroslides'' end press ENTER:\n")

(define sampledir (read-line))
(system (string-append "mkdir " 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 path of the directory where you want to keep Java classes:\n")
(define javadir (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 
  (let ([jps (find-files 
              (lambda (x) 
                (regexp-match #rx".*jlatexmath-.*\\.jar" (path->string x)))
              (string->path javadir))])
    (unless (pair? jps) (error 
                         (string-append "*** ERROR: Could not find downloaded jar file in"
                                        javadir
                                        "\nperhaps something went wrong with the download?\n" 
                                        "Please try again\n")))
    (when   (pair? (cdr jps)) (error 
                               (string-append "*** ERROR: More than one jar file found in"
                                              javadir
                                              "\nplease cleanup\n")))
    (car jps)))

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

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

(define abs-javapath (build-path (current-directory) javapath))
(define abs-jarpath (build-path (current-directory) jarpath))

(define java-formula-sh 
  (list "#!/bin/bash"
        (string-append "java -classpath '" (path->string abs-javapath) ":" (path->string abs-jarpath) "' ShowMath \"$1\" \"$2\" \"$3\" ")))
(system (string-append "echo " (car java-formula-sh) " > " javadir "/amkhlv-java-formula.sh"))
(system (string-append "echo " (car (cdr java-formula-sh)) " >> " javadir "/amkhlv-java-formula.sh"))

(define out (open-output-file (build-path javapath "amkhlv-java-formula.sh") #:exists 'replace))
(display "#!/bin/bash\n\n" out)
(display (string-append 
          "java -classpath '" 
          (path->string abs-javapath) ":" (path->string abs-jarpath) 
          "' ShowMath \"$1\" \"$2\" \"$3\" \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"
          ))
)