(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 \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"
                                        "\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"
                                              "\nplease cleanup\n")))
    (car jps)))

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

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

(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 "/"))
(system (string-append "echo " (car (cdr java-formula-sh)) " >> " javadir "/"))

(define out (open-output-file (build-path javapath "") #: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 ""))))

(display "\n\nFinally, one more thing to do\n")
(display (string-append 
          "The directory " (path->string javapath) " contains the file: \n \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"