#lang racket/base

(provide check-valid-module-source
         [struct-out exn:invalid-module-source])

(require syntax/kerncase

(struct exn:invalid-module-source exn:fail ())

(define (abort-abort #:reason (reason "Invalid module source"))
  (fprintf (current-report-port) "Aborting compilation.\n")
  (raise (exn:invalid-module-source reason

(define ns (make-base-namespace))

(define (looks-like-old-moby-or-js-vm? module-source-path)
  (or (call-with-input-file* module-source-path
                             (lambda (ip) (regexp-match #px"^\\s*#lang\\s+planet\\s+dyoo/moby" ip)))
      (call-with-input-file* module-source-path
                             (lambda (ip) (regexp-match #px"^\\s*#lang\\s+planet\\s+dyoo/js-vm" ip)))))

(define (check-valid-module-source module-source-path)
  ;; Check that the file exists.
  (unless (file-exists? module-source-path)
    (fprintf (current-report-port) "ERROR: Can't read a Racket module from ~e.  The file does not appear to exist.\n"

  ;; Is the file one that we know how to symbolically resolve?
  (cond [(rewrite-path module-source-path)
         (fprintf (current-report-port)
                  "ERROR: The file ~e appears to be outside the root package directory ~e.  You may need to use --root-dir.\n"

  ;; Does it look like something out of moby or js-vm?  Abort early, because if we don't do
  ;; this up front, Racket will try to install the deprecated module, and that's bad.
  (when (looks-like-old-moby-or-js-vm? module-source-path)
    (fprintf (current-report-port) "ERROR: The program in ~e appears to be written using the deprecated project js-vm or Moby.\n\nPlease change the lang line to:\n\n    #lang planet dyoo/whalesong\n\ninstead.\n"

  ;; Check that it looks like a module.
  (define stx
    (with-handlers ([exn:fail?
                     (lambda (exn)
                       ;; We can't even get the bytecode for the file.
                       ;; Fail immediately.
                       (fprintf (current-report-port) "ERROR: Can't read a Racket module from ~e.  The file may be ill-formed or be written in a language that Whalesong doesn't recognize.\n"
                       (fprintf (current-report-port) "\nFor reference, the error message produced when trying to read ~e is:\n\n" module-source-path)
                       (fprintf (current-report-port) "~a\n" (exn-message exn))
      (parameterize ([read-accept-reader #t]
                     [read-accept-lang #t])
        (call-with-input-file* module-source-path
                               (lambda (ip)
                                 (port-count-lines! ip)
                                 (read-syntax module-source-path ip))))))

  (define relative-language-stx
    (kernel-syntax-case stx #t
      [(module name language body ...)
       (fprintf (current-report-port) "ERROR: Can't read a Racket module from ~e.  The file exists, but does not appear to be a Racket module.\n"

  ;; Check that the module is written in a language that we allow.
  (define resolved-language-path
    (resolve-module-path (syntax->datum relative-language-stx)
   [(eq? resolved-language-path '#%kernel)
   [(path? resolved-language-path)
    (define normalized-resolved-language-path
      (normalize-path resolved-language-path))

     [(within-root-path? normalized-resolved-language-path)

     [(within-whalesong-path? normalized-resolved-language-path)

      ;; Something bad is about to happen, as the module is written
      ;; in a language that we, most likely, can't compile.
      ;; Let's see if we can provide a good error message here
      (fprintf (current-report-port) "ERROR: The file ~e is a Racket module, but is written in the language ~a [~e], which Whalesong does not know how to compile.\n"
              (syntax->datum relative-language-stx)

  ;; Once we know that the module is in a language we allow, we
  ;; check that the file compiles.
  (with-handlers ([exn:fail?
                   (lambda (exn)
                     (fprintf (current-report-port) "ERROR: the racket module ~e raises a compile-time error during compilation." module-source-path)
                     (fprintf (current-report-port) "\n\nFor reference, the error message produced during compilation is the following:\n\n")
                     (fprintf (current-report-port) "~a\n" (exn-message exn))
                     (newline (current-report-port))
    (parameterize ([current-namespace ns]
                    (path-only module-source-path)]
                    (path-only module-source-path)])
      (compile stx))))