;;; Copyright (C) 2023, 2024, 2025 David Thompson <dave@spritely.institute>
;;; Copyright (C) 2023, 2024, 2025 Igalia, S.L.
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;;    http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.

(use-modules (guix)
             (guix build-system gnu)
             (guix git)
             (guix git-download)
             ((guix licenses) #:prefix license:)
             (guix packages)
             (gnu packages autotools)
             (gnu packages bash)
             (gnu packages guile)
             (gnu packages guile-xyz)
             (gnu packages node)
             (gnu packages pkg-config)
             (gnu packages texinfo)
             (gnu packages tls))

;; Uncomment and update when we need a fresher Guile than what Guix
;; provides.
;;
;; (define guile-next-next
;;   (let ((commit "c8a169d38825d5a21da5392b355ca5fc9f33fa55")
;;         (revision "1"))
;;     (package
;;      (inherit guile-next)
;;      (version (git-version "3.0.10" revision commit))
;;      (source
;;       (origin
;;        (method git-fetch)
;;        (uri (git-reference
;;              (url "https://codeberg.org/guile/guile.git")
;;              (commit commit)))
;;        (file-name (git-file-name "guile" version))
;;        (sha256
;;         (base32 "01gqf6c9rnr5l8qralfwq23xmfxbnim1kqppgrd2l42pak3rm9c2")))))))

(package
  (name "guile-hoot")
  (version "0.9.0-git")
  (source (git-checkout (url (dirname (current-filename)))))
  (build-system gnu-build-system)
  (arguments
   (list
    #:make-flags #~(list "GUILE_AUTO_COMPILE=0")
    #:tests? #f
    #:modules `(((guix build guile-build-system)
                 #:select (target-guile-effective-version))
                ,@%default-gnu-modules)
    #:imported-modules `((guix build guile-build-system)
                         ,@%default-gnu-imported-modules)
    #:phases
    #~(modify-phases %standard-phases
        (add-after 'install 'wrap-program
          (lambda _
            (let* ((version (target-guile-effective-version))
                   (scm (string-append "/share/guile/site/" version))
                   (go (string-append "/lib/guile/" version
                                      "/site-ccache"))
                   (deps (list #$output
                               #$guile-fibers
                               #$guile-gnutls
                               #$guile-websocket)))
              (wrap-program (string-append #$output "/bin/hoot")
                `("GUILE_LOAD_PATH" prefix
                  ,(map (lambda (dep) (string-append dep scm)) deps))
                `("GUILE_LOAD_COMPILED_PATH" prefix
                  ,(map (lambda (dep) (string-append dep go)) deps)))))))))
  (native-inputs (list autoconf automake guile-syntax-highlight pkg-config texinfo))
  (inputs (list bash-minimal guile-next node))
  (propagated-inputs (list guile-fibers guile-gnutls guile-websocket))
  (native-search-paths
   (list (search-path-specification
           (variable "HOOT_LOAD_PATH")
           (files (list "share/guile-hoot/site")))))
  (synopsis "WebAssembly compiler for Guile Scheme")
  (description "Guile-hoot is an ahead-of-time, whole-program WebAssembly compiler for
GNU Guile.")
  (home-page "https://spritely.institute/hoot")
  (license (list license:asl2.0 license:lgpl3+)))
