(.module:
  [lux #*
   [data
    ["." text
     format]]]
  [//
   ["_" term]
   [//
    ["$" document]]])

(def: warranty-communications
  (List Text)
  (list "expressed"
        "implied"
        "statutory"))

(def: work-disclamers
  (List Text)
  (list "free of defects"
        "merchantable"
        "fit for a particular purpose"
        "non-infringing"))

(def: fixes
  (List Text)
  (list "servicing"
        "repair"
        "correction"))

(def: #export warranty
  (let [on-basics (let [applicability-escape "Unless required by applicable law or agreed to in writing"
                        work-provisioning (format _.licensor
                                                  " provides " _.work
                                                  ", and each " _.contributor
                                                  " provides its " ($.plural _.contribution))]
                    (format applicability-escape ", "
                            work-provisioning
                            " under " _.license
                            " on an " ($.quote "as is")
                            " basis, without warranty or condition of any kind, either " ($.or warranty-communications)
                            " including, without limitation, any warranties or conditions that " _.work
                            " is " ($.or work-disclamers)))
        on-distribution (format _.recipient
                                " is solely responsible for determining the appropriateness of using or redistributing " _.work)
        on-risk (format "The entire risk as to the quality and performance of " _.work
                        " is with " _.recipient)
        on-fixes (format "Should " _.work
                         " prove defective in any respect, " _.recipient
                         ", not any " _.contributor
                         ", assumes the cost of any necessary " ($.or fixes))
        on-importance (format "This disclaimer of warranty constitutes an essential part of "_.license)
        on-authorization (format "No use of "_.work
                                 " is authorized under " _.license
                                 " except under this disclaimer")]
    ($.paragraph (list on-basics
                       on-distribution
                       on-risk
                       on-fixes
                       on-importance
                       on-authorization))))

(def: damage-types
  (List Text)
  (list "direct"
        "indirect"
        "special"
        "incidental"
        "consequential"))

(def: damage-consequences
  (List Text)
  (list "lost profits"
        "loss of goodwill"
        "work stoppage"
        "computer failure or malfunction"
        "any and all other commercial damages or losses"))

(def: #export limitation
  (let [on-limit (let [exclusion "Under no circumstances and under no legal theory"
                       examples "whether tort (including negligence), contract, or otherwise"
                       applicable-law-exception "unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing"
                       renunciation (format "shall any " _.contributor
                                            " be liable to " _.recipient)
                       damage-enumeration (format "for any " ($.or damage-types) " damages of any character"
                                                  " including without limitation damages for " ($.or damage-consequences))
                       conscience "even if such party shall have been informed of the possibility of such damages"]
                   (format exclusion ", " examples ", " applicable-law-exception ", " renunciation " " damage-enumeration ", " conscience))
        on-death-exception "This limitation of liability shall not apply to liability for death or personal injury resulting from such party’s negligence to the extent applicable law prohibits such limitation"
        on-jurisdictions (format "Some jurisdictions do not allow the exclusion or limitation of incidental or consequential damages, so this exclusion and limitation may not apply to " _.recipient)]
    ($.paragraph (list on-limit
                       on-death-exception
                       on-jurisdictions))))

(def: #export litigation
  (let [on-jurisdiction (format "Any litigation relating to " _.license " may be brought only in the courts of a jurisdiction where the defendant maintains its principal place of business")
        on-laws "Such litigation shall be governed by laws of that jurisdiction, without reference to its conflict-of-law provisions"
        on-claims "Nothing in this section shall prevent a party’s ability to bring cross-claims or counter-claims"]
    ($.paragraph (list on-jurisdiction
                       on-laws
                       on-claims))))

(def: liability-obligations
  (List Text)
  (list "support"
        "warranty"
        "indemnity"
        "other liability obligations"
        (format "rights consistent with " _.license)))

(def: #export can-accept
  (let [on-acceptance-of-liability (let [condition (format "While redistributing " _.work " or " ($.plural _.extension) " thereof")
                                         right (format _.recipient " may choose to offer, and charge a fee for, acceptance of " ($.and/or ..liability-obligations))]
                                     (format condition ", " right))
        on-responsibility (let [but "However, in accepting such obligations"
                                cannot-represent-a-contributor (format _.recipient " may not act on behalf of any other " _.contributor)
                                can-only-represent-oneself (format "only on behalf and on sole responsibility of " _.recipient)
                                each-contributor (: (-> Text Text)
                                                    (function (_ responsibility)
                                                      (format responsibility " each " _.contributor)))
                                responsibilities (: (List Text)
                                                    (list (each-contributor "indemnify")
                                                          (each-contributor "defend")
                                                          (format (each-contributor "hold") " harmless")))
                                full-responsibility-condition (format "and only if " _.recipient
                                                                      " agrees to " ($.and responsibilities)
                                                                      " for any liability incurred by, or claims asserted against, such " _.contributor
                                                                      " by reason of acceptance of any such warranty or additional liability by " _.recipient)]
                            (format but ", " cannot-represent-a-contributor ", " can-only-represent-oneself ", " full-responsibility-condition))]
    ($.paragraph (list on-acceptance-of-liability
                       on-responsibility))))

(def: #export disclaim-high-risk
  (let [on-work (let [intentions (: (List Text)
                                    (list "designed"
                                          "manufactured"
                                          "intended for use or distribution"))
                      hazardous-environments (: (List Text)
                                                (list "nuclear facilities"
                                                      "aircraft navigation"
                                                      "communications systems"
                                                      "air traffic control"
                                                      "direct life support machines"
                                                      "weapons systems"))
                      consequences (: (List Text)
                                      (list "death"
                                            "personal injury"
                                            "severe physical damage"
                                            "environmental damage"))
                      disclaim (format _.work " is not fault tolerant"
                                       ", and is not " ($.or intentions)
                                       " as on-line control equipment in hazardous environments requiring fail-safe performance")
                      examples (format "such as in the operation of " ($.or hazardous-environments))
                      further (format "in which the failure of " _.work " could lead directly to " ($.or consequences))]
                  (format disclaim ", " examples ", " further))
        on-contributors (let [claim "any express or implied warranty of fitness for high risk activities"]
                          (format _.licensor
                                  " and every " _.contributor
                                  " specifically disclaim " claim))]
    ($.paragraph (list on-work
                       on-contributors))))