(import "lib/mosc")
(import "lib/build")

(define (main . args)
  (define base-dir "/usr/local")
  (define mod-dir (string-append base-dir "/lib/mosvm"))
  (define stub-dir (string-append mod-dir "-stubs"))
  (define bin-dir (string-append base-dir "/bin"))
  (define good #f)
  
  (define (do-config-prompt title current guess-fn description)
    (define val (or current (guess-fn)))
    (send (string-append "-- " 
                         title (if val (string-append ": " (format val)) "")
                         " --" *line-sep*))
    (send (string-append description *line-sep* ">> "))
    (define ans (strip (wait)))
    (if (empty-string? ans) val ans))
  
  (define-macro (config var title guess . descr-lines)
    `(set! ,var 
       (do-config-prompt ,title ,var (lambda () ,guess)
                         ,(apply string-join *line-sep* descr-lines))))
  
  (define (do-confirm lead . items)
    (send (apply string-append 
                 lead *line-sep*
                 (map (lambda (item) 
                        (string-append "    " (car item) ": " (cadr item) *line-sep*))
                      items)))
  
    (send "Yes / No? >> ")
  
    (define ans (strip (wait)))
    (or (string-begins-with? ans "y")
        (string-begins-with? ans "Y")))
  
  (define-macro (confirm lead . rest)
      `(if (do-confirm ,lead 
                       ,@(map (lambda (item) (cons 'list item))
                              rest))
         (return)))
  
  (define (mkdir . items)
    (run-command (string-append "mkdir -pm 0755 " (apply string-join " " items))))
  
  (define (copy . items)
    (run-command (string-append "cp -rp " (apply string-join " " items))))
  
  (define (chmod mask . paths)
    (run-command (string-append "chmod " mask " " (apply string-join " " paths))))
  
  (define (configure)
    (forever
      (config base-dir "Base Directory" "/usr/local"
        "Most UNIX users place applications that are not managed by their"
        "environment's package manager in subdirectories of /usr/local.  MOSVM"
        "will use this preference to guess sane defaults for the rest of the"
        "install process.")
  
      (config mod-dir "Library Directory" (string-append base-dir "/lib/mosvm")
        "MOSVM includes a considerable number of add on modules, and must be able"
        "to find them to compile applications.  The default location is derived"
        "from your Base Directory, and is fairly standard for development"
        "environments.")
  
      (config stub-dir "Stub Directory" (string-append mod-dir "-stubs")
        "MOSVM also includes a number of binary \"stubs\" which contain the bootstrap"
        "program that must begin any compiled MOSVM application.  The default"
        "location keeps these stubs separate from the module directory since they"
        "are not MOSVM object packages.")
  
      (config bin-dir "Binary Directory" (string-append base-dir "/bin")
        "MOSVM includes several utilities, in addition to the \"mosvm\" interactive"
        "environment.  These utilities will, by default, be placed in the bin"
        "subdirectory of the Base Directory.")
  
      (confirm
        "You have selected the following directories for installation:"
  
        ("Library Directory" mod-dir)
        ("   Stub Directory" stub-dir)
        (" Binary Directory" bin-dir))))
  
  ;(configure)
  
  (write-data-file "site/config.ms"
     (string-append ";;; Generated by bin/install.ms" *line-sep*
                    "(set-site-config! 'bin-path \"" bin-dir "\")" *line-sep*
                    "(set-site-config! 'mod-path '(\".\" \"" mod-dir "\"))" *line-sep*
                    "(set-site-config! 'stub-path \"" stub-dir "\")" *line-sep*))
  
  (mosc "site/config.ms")
  
  (define mod-bin-dir (string-append mod-dir "/bin"))
  
  (mkdir bin-dir mod-dir stub-dir mod-bin-dir)
  
  (copy "lib" "site" "core" "mosref" mod-dir)
  (copy "bin/*.ms" "bin/*.mo" mod-bin-dir)
  (copy "stubs/*" stub-dir)
  
  ; Re-link, so we have the new site config..
  (define mosvm-bin (string-append bin-dir "/mosvm"))
  (define mosref-bin (string-append bin-dir "/mosref"))
  
  (write-data-file mosvm-bin (build-exe *platform* "bin/mosvm")) 
  (write-data-file mosref-bin (build-exe *platform* "bin/mosref")) 
  
  (chmod "-R 0755" mod-dir stub-dir mod-bin-dir)
  
  (chmod "0755" bin-dir mosvm-bin mosref-bin))
