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

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

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

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

(define out (open-output-file (build-path javapath "") #:exists 'replace))
(display "#!/usr/bin/env bash\n\n" out)
(display "\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 
          (path->string abs-javapath) "/\n\n") out)
(close-output-port out)

(system (string-append "chmod +x " (path->string (build-path javapath ""))))
(system (string-append "chmod +x " (path->string (build-path javapath ""))))
(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"