;;; SPDX-FileCopyrightText: 2023, 2024 Hilton Chain <hako@ultrarare.space>
;;;
;;; SPDX-License-Identifier: GPL-3.0-or-later

(define-module (testament common)
  #:use-module ((guix diagnostics) #:select (leave))
  #:use-module ((guix gexp) #:select (local-file))
  #:use-module ((guix i18n) #:select (G_))
  #:use-module ((guix packages) #:select (package-name))
  #:use-module ((rnrs io ports) #:select (get-string-all))
  #:export (testament-find-file
            testament-file-content
            testament-file-object

            delete-package-from-list))

(define testament-path
  (getenv "testament_path"))

(define (testament-find-file name)
  "Find a file in the \"files\" directory (fallback to \"blobs\") of Testament
repository.  Return a string of path to the file, or #f if file not found."
  (let ((gen-file (string-append testament-path "/gen/files/plain/" name))
        (src-file (string-append testament-path "/src/files/plain/" name))
        (blob-name (string-append testament-path "/src/modules/blobs/" name)))
    (cond ((file-exists? gen-file) gen-file)
          ((file-exists? src-file) src-file)
          ((file-exists? blob-name) blob-name)
          (else (leave (G_ "file '~a' not found.~%") name)))))

(define (testament-file-content name)
  "Return a string, the content of file NAME to be found by
'testament-find-file'."
  (call-with-input-file (testament-find-file name)
    (lambda (port)
      (get-string-all port))))

(define (testament-file-object name)
  "Similar to 'testament-file-content' but return a file-like object."
  (local-file (testament-find-file name)))

(define (delete-package-from-list name lst)
  "Return a copy of package list LST, removing packages named NAME."
  (filter (lambda (pkg)
            (not (string=? name (package-name pkg))))
          lst))
