(module util mzscheme
  (require (planet "" ("dherman" "io.plt" 1)))
  (require (planet "" ("dherman" "io.plt" 1)))
  (require (planet "" ("dherman" "test.plt" 1)))
  (require (planet "" ("ryanc" "scripting.plt" 1)))
  (require (prefix list: (lib "" "srfi" "1")))
  (require (lib ""))
  (require (lib ""))
  (require (lib ""))
  (require (lib ""))

  (define (rm-rf path)
    (when (or (file-exists? path) (directory-exists? path))
      (delete-directory/files path)))

  (define (dir-tree=? dt1 dt2)
      [(and (pair? dt1) (pair? dt2))
       (dir-tree-pair=? dt1 dt2)]
      [(and (or (path? dt1) (string? dt1))
            (or (path? dt2) (string? dt2)))
       (dir-tree-path=? dt1 dt2)]
      [(pair? dt1)
       (dir-tree-heterogeneous=? dt1 dt2)]
      [(pair? dt2)
       (dir-tree-heterogeneous=? dt2 dt1)]))

  ;; TODO: is this right?
  (define (dir-tree-pair=? dt1 dt2)
    (list-permutation? dt1 dt2 equal?))

  ;; TODO: is this right?
  (define (dir-tree-path=? dt1 dt2)
    (list-permutation? (directory-list/all dt1)
                       (directory-list/all dt2)

  (define (dir-tree-heterogeneous=? mockup path)
    (match mockup
      [('dir dir-name rest ...)
       (and (directory-exists? path)
            (string=? dir-name (path->string (basename path)))
            (parameterize ([current-directory path])
              (let ([expected (quicksort rest dir-tree-name<?)]
                    [actual (quicksort (directory-list) file-name<?)])
                (and (= (length expected) (length actual))
                     (andmap dir-tree-heterogeneous=? expected actual)))))]
      [('file i)
       (and (file-exists? path)
            (string=? (path->string (basename path))
                      (format "file~a.txt" i)))]
      [(elts ...)
       (let ([expected (quicksort elts dir-tree-name<?)]
             [actual (quicksort (directory-list) file-name<?)])
         (and (= (length expected) (length actual))
              (andmap dir-tree-heterogeneous=? expected actual)))]))

  (define (file=? file1 file2)
    (equal? (read-all/file file1)
            (read-all/file file2)))

  (define (directory=? dir1 dir2)
    (let loop ([dir1 dir1] [dir2 dir2])
      (let ([entries1 (quicksort (directory-list dir1) file-name<?)]
            [entries2 (quicksort (directory-list dir2) file-name<?)])
        (and (= (length entries1) (length entries2))
             (andmap (lambda (entry1 entry2)
                       (let ([path1 (build-path dir1 entry1)]
                             [path2 (build-path dir2 entry2)])
                         (and (bytes=? (path->bytes entry1) (path->bytes entry2))
                              (or (and (directory-exists? path1)
                                       (directory-exists? path2)
                                       (loop path1 path2))
                                  (and (file-exists? path1)
                                       (file-exists? path2)
                                       (file=? path1 path2))))))

  (define (dir-tree-name<? dt1 dt2)
    (string<? (dir-tree-name dt1) (dir-tree-name dt2)))

  (define (file-name<? p1 p2)
    (string<? (path->string (basename p1)) (path->string (basename p2))))

  (define (dir-tree-name src)
    (match src
      [('dir dir-name rest ...) dir-name]
      [('file i) (format "file~a.txt" i)]
      [(elts ...) "."]))

  (define (build-dir-tree src)
    (match src
      [('dir dir-name rest ...)
       (rm-rf dir-name)
       (make-directory dir-name)
       (parameterize ([current-directory dir-name])
         (for-each build-dir-tree rest))]
      [('file i)
       (with-output-to-file (format "file~a.txt" i)
         (lambda ()
           (printf "(test~a a b c)~n" i))
      [(elts ...)
       (for-each build-dir-tree elts)]))

  (define all-files
    (opt-lambda ([dir (current-directory)])
      (list:append-map (lambda (entry)
                         (let ([path (build-path dir entry)])
                           (if (directory-exists? path)
                               (all-files path)
                               (list path))))
                       (directory-list dir))))

  (define gettysburg-address
    '("Four score and seven years ago our fathers brought forth"
      "on this continent, a new nation, conceived in Liberty,"
      "and dedicated to the proposition that all men are created"
      "Now we are engaged in a great civil war, testing whether"
      "that nation, or any nation so conceived and so dedicated,"
      "can long endure. We are met on a great battle-field of"
      "that war. We have come to dedicate a portion of that field,"
      "as a final resting place for those who here gave their"
      "lives that that nation might live. It is altogether"
      "fitting and proper that we should do this."
      "But, in a larger sense, we can not dedicate -- we can not"
      "consecrate -- we can not hallow -- this ground. The brave"
      "men, living and dead, who struggled here, have consecrated"
      "it, far above our poor power to add or detract. The world"
      "will little note, nor long remember what we say here, but"
      "it can never forget what they did here. It is for us the"
      "living, rather, to be dedicated here to the unfinished"
      "work which they who fought here have thus far so nobly"
      "advanced. It is rather for us to be here dedicated to the"
      "great task remaining before us -- that from these honored"
      "dead we take increased devotion to that cause for which"
      "they gave the last full measure of devotion -- that we"
      "here highly resolve that these dead shall not have died"
      "in vain -- that this nation, under God, shall have a new"
      "birth of freedom -- and that government of the people, by"
      "the people, for the people, shall not perish from the"

  (define-syntax with-gettysburg-address
    (syntax-rules ()
      [(_ file e1 e2 ...)
       (with-temporary-file file ("abe~a.txt" #f (current-directory))
         (with-output-to-file file
           (lambda ()
             (write-lines gettysburg-address))
         e1 e2 ...)]))

  ;; gettysburg-address? : (union string bytes path input-port (listof string)) -> boolean
  (define (gettysburg-address? thing)
      [(string? thing) (gettysburg-address? (string->path thing))]
      [(bytes? thing) (gettysburg-address? (bytes->path thing))]
      [(path? thing) (gettysburg-address? (with-input-from-file thing read-lines))]
      [(input-port? thing) (gettysburg-address? (read-lines thing))]
      [(list? thing) (equal? thing gettysburg-address)]
      [else #f]))

  (define ex:arch
    '(dir "example"
          (file 1)
          (file 2)
          (dir "empty")
          (dir "subdir"
               (file 3)
               (file 4)
               (file 5))))

  (define ex:single-file/no-directories
    '(file 1))

  (define ex:multiple-files/no-directories
    '((file 1) (file 2) (file 3) (file 4) (file 5)))

  (define ex:multiple-files/subdirectories
    '(dir "test-zip"
          (file 1)
          (file 2)
          (file 3)
          (dir "subdirectory"
               (file 4)
               (file 5))))

  (define ex:multiple-files/subdirectories/empty-directories
    '(dir "test-zip"
          (file 1)
          (file 2)
          (dir "subdir1"
               (file 3)
               (file 4))
          (dir "empty1")
          (dir "empty2")
          (dir "subdir2"
               (file 5)
               (file 6))))

  (provide (all-defined)))