
(define-module (bloatrunner)
  #:use-module (guix packages)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix download)
  #:use-module (guix gexp)
  #:use-module (guix git-download)
  #:use-module (guix git)
  #:use-module (guix build-system gnu)
  #:use-module (guix build guile-build-system)
  #:use-module (guix utils)
  #:use-module (gnu packages)
  #:use-module (gnu packages autotools)
  #:use-module (gnu packages guile)
  #:use-module (gnu packages game-development)
  #:use-module (gnu packages pkg-config)
  #:use-module (gnu packages python-xyz)
  )


(define vcs-file?
  ;; Return true if the given file is under version control.
  (or (git-predicate (dirname (dirname (current-source-directory))))
      (const #t)))                                ;not in a Git checkout

(define-public bloatrunner
  (package
   (name "bloatrunner")
   (version "0.3.4-git")
   ;; (source (git-checkout (url (dirname (current-filename)))))
   (source (local-file "../.." "guile-checkout"
                       #:recursive? #t
                       #:select? vcs-file?))
   (build-system gnu-build-system)
   (arguments
    '(#:modules ((guix build gnu-build-system)
                 (guix build utils)
                 (ice-9 popen)
                 (ice-9 rdelim))
      #:make-flags '("GUILE_AUTO_COMPILE=0")
      
      #:phases
      (modify-phases %standard-phases
                     (add-after 'install-bin 'wrap-program
                                (lambda* (#:key inputs outputs #:allow-other-keys)
                                  ;; (use-modules (guix build guile-build-system))
                                  ;; Wrap the 'gitile' command to refer to the right modules.
                                  (let* ((out    (assoc-ref outputs "out"))
                                         (guile  (assoc-ref inputs "guile"))
                                         (guile-opengl (assoc-ref inputs "guile-opengl"))
                                         (guile-sdl2 (assoc-ref inputs "guile-sdl2"))
                                         (chickadee (assoc-ref inputs "guile-chickadee"))
                                         (deps   (list out guile-opengl guile-sdl2 chickadee guile))
                                         (effective (read-line
                                                     (open-pipe* OPEN_READ
                                                                 (string-append guile "/bin/guile")
                                                                 "-c" "(display (effective-version))")))
                                         (mods   (string-drop-right  ;drop trailing colon
                                                  (string-join deps
                                                               (string-append "/share/guile/site/"
                                                                              effective ":")
                                                               'suffix)
                                                  1))
                                         (objs   (string-drop-right
                                                  (string-join deps
                                                               (string-append "/lib/guile/" effective
                                                                              "/site-ccache:")
                                                               'suffix)
                                                  1)))
                                    (wrap-program (string-append out "/bin/bloatrunner")
                                                  `("GUILE_LOAD_PATH" ":" prefix (,mods))
                                                  `("GUILE_LOAD_COMPILED_PATH" ":" prefix (,objs))
                                                  `("ASSET_DIR" ":" prefix (,(string-append out "/share/bloatrunner/assets")))
                                                  )))))
      ))
   (native-inputs
    (list autoconf automake pkg-config python-bumpversion))
   (inputs
    (list guile-3.0))
   (propagated-inputs
    (list guile-chickadee))
   (synopsis "Loderunner inspired game")
   (description "Small game written for the 2024 lisp game jam.")
   (home-page "https://www.snamellit.com/projects/bloatrunner.html")
   (license license:gpl3)))


bloatrunner
