code:
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018 Konrad Hinsen <konrad.hinsen@fastmail.net>
;;; Copyright © 2018, 2021 Kei Kebreau <kkebreau@posteo.net>
;;; Copyright © 2018 Efraim Flashner <efraim@flashner.co.il>
;;; Copyright © 2018 Tobias Geerinckx-Rice <me@tobias.gr>
;;; Copyright © 2020 Björn Höfling <bjoern.hoefling@bjoernhoefling.de>
;;; Copyright © 2020 Vincent Legoll <vincent.legoll@gmail.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu packages chemistry)
  #:use-module (guix packages)
  #:use-module (guix utils)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix download)
  #:use-module (guix git-download)
  #:use-module (gnu packages)
  #:use-module (gnu packages algebra)
  #:use-module (gnu packages autotools)
  #:use-module (gnu packages backup)
  #:use-module (gnu packages boost)
  #:use-module (gnu packages check)
  #:use-module (gnu packages compression)
  #:use-module (gnu packages documentation)
  #:use-module (gnu packages gl)
  #:use-module (gnu packages graphviz)
  #:use-module (gnu packages gv)
  #:use-module (gnu packages maths)
  #:use-module (gnu packages mpi)
  #:use-module (gnu packages perl)
  #:use-module (gnu packages pkg-config)
  #:use-module (gnu packages python)
  #:use-module (gnu packages python-xyz)
  #:use-module (gnu packages qt)
  #:use-module (gnu packages serialization)
  #:use-module (gnu packages sphinx)
  #:use-module (gnu packages xml)
  #:use-module (guix build-system cmake)
  #:use-module (guix build-system gnu)
  #:use-module (guix build-system python))

(define-public avogadrolibs
  (package
    (name "avogadrolibs")
    (version "1.93.0")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url "https://github.com/OpenChemistry/avogadrolibs")
             (commit version)))
       (sha256
        (base32 "1xivga626n5acnmwmym8svl0pdri8hkp59czf04ri2zflnviyh39"))
       (file-name (git-file-name name version))))
    (build-system cmake-build-system)
    (native-inputs
     `(("eigen" ,eigen)
       ("mmtf-cpp" ,mmtf-cpp)
       ("msgpack" ,msgpack)
       ("googletest" ,googletest)
       ("pkg-config" ,pkg-config)
       ("pybind11" ,pybind11)))
    (inputs
     `(("glew" ,glew)
       ("libarchive" ,libarchive)
       ("libmsym" ,libmsym)
       ("molequeue" ,molequeue)
       ("python" ,python)
       ("spglib" ,spglib)
       ("qtbase" ,qtbase-5)))
    (arguments
     '(#:configure-flags (list "-DENABLE_TESTING=ON"
                               (string-append "-DSPGLIB_INCLUDE_DIR="
                                              (assoc-ref %build-inputs "spglib")
                                              "/include"))))
    (home-page "https://www.openchemistry.org/projects/avogadro2/")
    (synopsis "Libraries for chemistry, bioinformatics, and related areas")
    (description
     "Avogadro libraries provide 3D rendering, visualization, analysis and data
processing useful in computational chemistry, molecular modeling,
bioinformatics, materials science, and related areas.")
    (license license:bsd-3)))

(define-public avogadro2
  (package
    (name "avogadro2")
    (version "1.93.0")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url "https://github.com/OpenChemistry/avogadroapp")
             (commit version)))
       (sha256
        (base32
         "1z3pjlwja778a1dmvx9aqz2hlw5q9g3kqxhm9slz08452600jsv7"))
       (file-name (git-file-name name version))))
    (build-system cmake-build-system)
    (native-inputs
     `(("eigen" ,eigen)
       ("pkg-config" ,pkg-config)))
    (inputs
     `(("avogadrolibs" ,avogadrolibs)
       ("hdf5" ,hdf5)
       ("molequeue" ,molequeue)
       ("qtbase" ,qtbase-5)))
    ;; TODO: Enable tests with "-DENABLE_TESTING" configure flag.
    (arguments
     '(#:tests? #f))
    (home-page "https://www.openchemistry.org/projects/avogadro2/")
    (synopsis "Advanced molecule editor")
    (description
     "Avogadro 2 is an advanced molecule editor and visualizer designed for use
in computational chemistry, molecular modeling, bioinformatics, materials
science, and related areas.  It offers flexible high quality rendering and a
powerful plugin architecture.")
    (license license:bsd-3)))

(define-public domainfinder
  (package
    (name "domainfinder")
    (version "2.0.5")
    (source
     (origin
       (method url-fetch)
       (uri (string-append "https://bitbucket.org/khinsen/"
                           "domainfinder/downloads/DomainFinder-"
                           version ".tar.gz"))
       (sha256
        (base32
         "1z26lsyf7xwnzwjvimmbla7ckipx6p734w7y0jk2a2fzci8fkdcr"))))
    (build-system python-build-system)
    (inputs
     `(("python-mmtk" ,python2-mmtk)))
    (arguments
     `(#:python ,python-2
       ;; No test suite
       #:tests? #f))
    (home-page "http://dirac.cnrs-orleans.fr/DomainFinder.html")
    (synopsis "Analysis of dynamical domains in proteins")
    (description "DomainFinder is an interactive program for the determination
and characterization of dynamical domains in proteins.  It can infer dynamical
domains by comparing two protein structures, or from normal mode analysis on a
single structure.  The software is currently not actively maintained and works
only with Python 2 and NumPy < 1.9.")
    (license license:cecill-c)))

(define-public inchi
  (package
    (name "inchi")
    ;; Update the inchi-doc native input when updating inchi.
    (version "1.06")
    (source (origin
              (method url-fetch)
              (uri (string-append "https://www.inchi-trust.org/download/"
                                  (string-join (string-split version #\.) "")
                                  "/INCHI-1-SRC.zip"))
              (sha256
               (base32
                "1zbygqn0443p0gxwr4kx3m1bkqaj8x9hrpch3s41py7jq08f6x28"))
              (file-name (string-append name "-" version ".zip"))))
    (build-system gnu-build-system)
    (arguments
     '(#:tests? #f ; no check target
       #:phases
       (modify-phases %standard-phases
         (delete 'configure) ; no configure script
         (add-before 'build 'chdir-to-build-directory
           (lambda _ (chdir "INCHI_EXE/inchi-1/gcc") #t))
         (add-after 'build 'build-library
           (lambda _
             (chdir "../../../INCHI_API/libinchi/gcc")
             (invoke "make")))
         (replace 'install
           (lambda* (#:key inputs outputs #:allow-other-keys)
             (let* ((out (assoc-ref outputs "out"))
                    (bin (string-append out "/bin"))
                    (doc (string-append out "/share/doc/inchi"))
                    (include-dir (string-append out "/include/inchi"))
                    (lib (string-append out "/lib/inchi"))
                    (inchi-doc (assoc-ref inputs "inchi-doc"))
                    (unzip (string-append (assoc-ref inputs "unzip")
                                          "/bin/unzip")))
               (chdir "../../..")
               ;; Install binary.
               (with-directory-excursion "INCHI_EXE/bin/Linux"
                 (rename-file "inchi-1" "inchi")
                 (install-file "inchi" bin))
               ;; Install libraries.
               (with-directory-excursion "INCHI_API/bin/Linux"
                 (for-each (lambda (file)
                             (install-file file lib))
                           (find-files "." "libinchi\\.so\\.1\\.*")))
               ;; Install header files.
               (with-directory-excursion "INCHI_BASE/src"
                 (for-each (lambda (file)
                             (install-file file include-dir))
                           (find-files "." "\\.h$")))
               ;; Install documentation.
               (mkdir-p doc)
               (invoke unzip "-j" "-d" doc inchi-doc)
               #t))))))
    (native-inputs
     `(("unzip" ,unzip)
       ("inchi-doc"
        ,(origin
           (method url-fetch)
           (uri (string-append "http://www.inchi-trust.org/download/"
                                  (string-join (string-split version #\.) "")
                                  "/INCHI-1-DOC.zip"))
           (sha256
            (base32
             "1kyda09i9p89xfq90ninwi7w13k1w3ljpl4gqdhpfhi5g8fgxx7f"))
           (file-name (string-append name "-" version ".zip"))))))
    (home-page "https://www.inchi-trust.org")
    (synopsis "Utility for manipulating machine-readable chemical structures")
    (description
     "The @dfn{InChI} (IUPAC International Chemical Identifier) algorithm turns
chemical structures into machine-readable strings of information.  InChIs are
unique to the compound they describe and can encode absolute stereochemistry
making chemicals and chemistry machine-readable and discoverable.  A simple
analogy is that InChI is the bar-code for chemistry and chemical structures.")
    (license (license:non-copyleft
              "file://LICENCE"
              "See LICENCE in the distribution."))))

(define-public libmsym
  (package
    (name "libmsym")
    (version "0.2.3")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url "https://github.com/mcodev31/libmsym")
             (commit (string-append "v" version))))
       (sha256
        (base32
         "0a9j28irdsr461qpzlc9z1yjyb9kp64fh5zw7ylspc9zn3189qwk"))
       (file-name (git-file-name name version))))
    (build-system cmake-build-system)
    (arguments
     '(#:configure-flags '("-DBUILD_SHARED_LIBS=ON")
       #:tests? #f))                    ; no check target
    (home-page "https://github.com/mcodev31/libmsym")
    (synopsis "C library dealing with point group symmetry in molecules")
    (description "libmsym is a C library dealing with point group symmetry in
molecules.")
    (license license:expat)))

(define-public mmtf-cpp
  (package
    (name "mmtf-cpp")
    (version "1.0.0")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url "https://github.com/rcsb/mmtf-cpp")
             (commit (string-append "v" version))))
       (file-name (git-file-name name version))
       (sha256
        (base32
         "17ylramda69plf5w0v5hxbl4ggkdi5s15z55cv0pljl12yvyva8l"))))
    (build-system cmake-build-system)
    ;; Tests require the soon-to-be-deprecated version 1 of the catch-framework.
    (arguments
     '(#:tests? #f))
    (home-page "https://mmtf.rcsb.org/")
    (synopsis "C++ API for the Macromolecular Transmission Format")
    (description "This package is a library for the
@acronym{MMTF,macromolecular transmission format}, a binary encoding of
biological structures.")
    (license license:expat)))

(define-public molequeue
  (package
    (name "molequeue")
    (version "0.9.0")
    (source
     (origin
       (method url-fetch)
       (uri (string-append "https://github.com/OpenChemistry/molequeue/"
                           "releases/download/" version "/molequeue-"
                           version ".tar.bz2"))
       (sha256
        (base32
         "1w1fgxzqrb5yxvpmnc3c9ymnvixy0z1nfafkd9whg9zw8nbgl998"))))
    (build-system cmake-build-system)
    (inputs
     `(("qtbase" ,qtbase-5)))
    (arguments
     '(#:configure-flags '("-DENABLE_TESTING=ON")
       #:phases
       (modify-phases %standard-phases
         (add-after 'unpack 'patch-tests
           (lambda _
             ;; TODO: Fix/enable the failing message and clientserver tests.
             ;; In the message test, the floating-point value "5.36893473232" on
             ;; line 165 of molequeue/app/testing/messagetest.cpp should
             ;; (apparently) be truncated, but it is not.
             (substitute* "molequeue/app/testing/messagetest.cpp"
               (("5\\.36893473232") "5.36893"))
             ;; It is unclear why the clientserver test fails, so it is
             ;; completely disabled.
             (substitute* "molequeue/app/testing/CMakeLists.txt"
               ((".*clientserver.*") ""))
             #t))
         (add-before 'check 'set-display
           (lambda _
             ;; Make Qt render "offscreen" for the sake of tests.
             (setenv "QT_QPA_PLATFORM" "offscreen")
             #t)))))
    (home-page "https://www.openchemistry.org/projects/molequeue/")
    (synopsis "Application for coordinating computational jobs")
    (description "MoleQueue is a system-tray resident desktop application for
abstracting, managing, and coordinating the execution of tasks both locally and
 on remote computational resources.  Users can set up local and remote queues
that describe where the task will be executed.  Each queue can have programs,
with templates to facilitate the execution of the program.  Input files can be
staged, and output files collected using a standard interface.")
    (license license:bsd-3)))

(define with-numpy-1.8
  (package-input-rewriting `((,python2-numpy . ,python2-numpy-1.8))))

(define-public nmoldyn
  (package
    (name "nmoldyn")
    (version "3.0.11")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url "https://github.com/khinsen/nMOLDYN3")
             (commit (string-append "v" version))))
       (file-name (git-file-name name version))
       (sha256
        (base32
         "016h4bqg419p6s7bcx55q5iik91gqmk26hbnfgj2j6zl0j36w51r"))))
    (build-system python-build-system)
    (inputs
     `(("python-matplotlib" ,(with-numpy-1.8 python2-matplotlib))
       ("python-scientific" ,python2-scientific)
       ("netcdf" ,netcdf)
       ("gv" ,gv)))
    (propagated-inputs
     `(("python-mmtk" ,python2-mmtk)))
    (arguments
     `(#:python ,python-2
       #:tests? #f  ; No test suite
       #:phases
       (modify-phases %standard-phases
         (add-before 'build 'create-linux2-directory
           (lambda _
             (mkdir-p "nMOLDYN/linux2")))
         (add-before 'build 'change-PDF-viewer
           (lambda* (#:key inputs #:allow-other-keys)
             (substitute* "nMOLDYN/Preferences.py"
               ;; Set the paths for external executables, substituting
               ;; gv for acroread.
               ;; There is also vmd_path, but VMD is not free software
               ;; and Guix contains currently no free molecular viewer that
               ;; could be substituted.
               (("PREFERENCES\\['acroread_path'\\] = ''")
                (format #f "PREFERENCES['acroread_path'] = '~a'"
                        (which "gv")))
               (("PREFERENCES\\['ncdump_path'\\] = ''")
                (format #f "PREFERENCES['ncdump_path'] = '~a'"
                        (which "ncdump")))
               (("PREFERENCES\\['ncgen_path'\\] = ''")
                (format #f "PREFERENCES['ncgen_path'] = '~a'"
                        (which "ncgen3")))
               (("PREFERENCES\\['task_manager_path'\\] = ''")
                (format #f "PREFERENCES['task_manager_path'] = '~a'"
                        (which "task_manager")))
               ;; Show documentation as PDF
               (("PREFERENCES\\['documentation_style'\\] = 'html'")
                "PREFERENCES['documentation_style'] = 'pdf'") ))))))
    (home-page "http://dirac.cnrs-orleans.fr/nMOLDYN.html")
    (synopsis "Analysis software for Molecular Dynamics trajectories")
    (description "nMOLDYN is an interactive analysis program for Molecular Dynamics
simulations.  It is especially designed for the computation and decomposition of
neutron scattering spectra, but also computes other quantities.  The software
is currently not actively maintained and works only with Python 2 and
NumPy < 1.9.")
    (license license:cecill)))

(define-public tng
  (package
    (name "tng")
    (version "1.8.2")
    (source (origin
              (method git-fetch)
              (uri (git-reference
                    (url "https://github.com/gromacs/tng")
                    (commit (string-append "v" version))))
              (file-name (git-file-name name version))
              (sha256
               (base32
                "1apf2n8nb34z09xarj7k4jgriq283l769sakjmj5aalpbilvai4q"))))
    (build-system cmake-build-system)
    (inputs
     `(("zlib" ,zlib)))
    (arguments
     `(#:phases
       (modify-phases %standard-phases
         (add-after 'unpack 'remove-bundled-zlib
           (lambda _
             (delete-file-recursively "external")
             #t))
         (replace 'check
           (lambda _
             (invoke "../build/bin/tests/tng_testing")
             #t)))))
    (home-page "https://github.com/gromacs/tng")
    (synopsis "Trajectory Next Generation binary format manipulation library")
    (description "TRAJNG (Trajectory next generation) is a program library for
handling molecular dynamics (MD) trajectories.  It can store coordinates, and
optionally velocities and the H-matrix.  Coordinates and velocities are
stored with user-specified precision.")
    (license license:bsd-3)))

(define-public gromacs
  (package
    (name "gromacs")
    (version "2020.2")
    (source (origin
              (method url-fetch)
              (uri (string-append "http://ftp.gromacs.org/pub/gromacs/gromacs-"
                                  version ".tar.gz"))
              (sha256
               (base32
                "1wyjgcdl30wy4hy6jvi9lkq53bqs9fgfq6fri52dhnb3c76y8rbl"))
              ;; Our version of tinyxml2 is far newer than the bundled one and
              ;; require fixing `testutils' code. See patch header for more info
              (patches (search-patches "gromacs-tinyxml2.patch"))))
    (build-system cmake-build-system)
    (arguments
     `(#:configure-flags
       (list "-DGMX_DEVELOPER_BUILD=on" ; Needed to run tests
             ;; Unbundling
             "-DGMX_USE_LMFIT=EXTERNAL"
             "-DGMX_BUILD_OWN_FFTW=off"
             "-DGMX_EXTERNAL_BLAS=on"
             "-DGMX_EXTERNAL_LAPACK=on"
             "-DGMX_EXTERNAL_TNG=on"
             "-DGMX_EXTERNAL_ZLIB=on"
             "-DGMX_EXTERNAL_TINYXML2=on"
             (string-append "-DTinyXML2_DIR="
                            (assoc-ref %build-inputs "tinyxml2"))
             ;; Workaround for cmake/FindSphinx.cmake version parsing that does
             ;; not understand the guix-wrapped `sphinx-build --version' answer
             (string-append "-DSPHINX_EXECUTABLE_VERSION="
                            ,(package-version python-sphinx)))
       #:phases
       (modify-phases %standard-phases
         (add-after 'unpack 'fixes
           (lambda* (#:key inputs #:allow-other-keys)
             ;; Still bundled: part of gromacs, source behind registration
             ;; but free software anyways
             ;;(delete-file-recursively "src/external/vmd_molfile")
             ;; Still bundled: threads-based OpenMPI-compatible fallback
             ;; designed to be bundled like that
             ;;(delete-file-recursively "src/external/thread_mpi")
             ;; Unbundling
             (delete-file-recursively "src/external/lmfit")
             (delete-file-recursively "src/external/clFFT")
             (delete-file-recursively "src/external/fftpack")
             (delete-file-recursively "src/external/build-fftw")
             (delete-file-recursively "src/external/tng_io")
             (delete-file-recursively "src/external/tinyxml2")
             (delete-file-recursively "src/external/googletest")
             (copy-recursively (assoc-ref inputs "googletest-source")
                               "src/external/googletest")
             ;; This test warns about the build host hardware, disable
             (substitute* "src/gromacs/hardware/tests/hardwaretopology.cpp"
               (("TEST\\(HardwareTopologyTest, HwlocExecute\\)")
                "void __guix_disabled()"))
             #t)))))
    (native-inputs
     `(("doxygen" ,doxygen)
       ("googletest-source" ,(package-source googletest))
       ("graphviz" ,graphviz)
       ("pkg-config" ,pkg-config)
       ("python" ,python)
       ("python-pygments" ,python-pygments)
       ("python-sphinx" ,python-sphinx)))
    (inputs
     `(("fftwf" ,fftwf)
       ("hwloc" ,hwloc-2 "lib")
       ("lmfit" ,lmfit)
       ("openblas" ,openblas)
       ("perl" ,perl)
       ("tinyxml2" ,tinyxml2)
       ("tng" ,tng)))
    (home-page "http://www.gromacs.org/")
    (synopsis "Molecular dynamics software package")
    (description "GROMACS is a versatile package to perform molecular dynamics,
i.e. simulate the Newtonian equations of motion for systems with hundreds to
millions of particles.  It is primarily designed for biochemical molecules like
proteins, lipids and nucleic acids that have a lot of complicated bonded
interactions, but since GROMACS is extremely fast at calculating the nonbonded
interactions (that usually dominate simulations) many groups are also using it
for research on non-biological systems, e.g. polymers.  GROMACS supports all the
usual algorithms you expect from a modern molecular dynamics implementation.")
    (license license:lgpl2.1+)))

(define-public openbabel
  (package
    (name "openbabel")
    (version "3.1.1")
    (source (origin
              (method url-fetch)
              (uri (string-append "https://github.com/openbabel/openbabel/"
                                  "releases/download/openbabel-"
                                  (string-replace-substring version "." "-")
                                  "/openbabel-" version "-source.tar.bz2"))
              (sha256
               (base32
                "0s0f4zib8vshfaywsr5bjjz55jwsg6yiz2qw4i5jm8wysn0q7v56"))))
    (build-system cmake-build-system)
    (arguments
     `(;; FIXME: Disable tests on i686 to work around
       ;; https://github.com/openbabel/openbabel/issues/2041.
       #:tests? ,(or (%current-target-system)
                     (not (string=? "i686-linux" (%current-system))))
       #:configure-flags
       (list "-DOPENBABEL_USE_SYSTEM_INCHI=ON"
             (string-append "-DINCHI_LIBRARY="
                            (assoc-ref %build-inputs "inchi")
                            "/lib/inchi/libinchi.so.1")
             (string-append "-DINCHI_INCLUDE_DIR="
                            (assoc-ref %build-inputs "inchi") "/include/inchi"))
       #:test-target "test"))
    (native-inputs
     `(("pkg-config" ,pkg-config)))
    (inputs
     `(("eigen" ,eigen)
       ("inchi" ,inchi)
       ("libxml2" ,libxml2)
       ("zlib" ,zlib)))
    (home-page "http://openbabel.org/wiki/Main_Page")
    (synopsis "Chemistry data manipulation toolbox")
    (description
     "Open Babel is a chemical toolbox designed to speak the many languages of
chemical data.  It's a collaborative project allowing anyone to search, convert,
analyze, or store data from molecular modeling, chemistry, solid-state
materials, biochemistry, or related areas.")
    (license license:gpl2)))

(define-public spglib
  (package
    (name "spglib")
    (version "1.16.0")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url "https://github.com/spglib/spglib")
             (commit (string-append "v" version))))
       (sha256
        (base32 "1kzc956m1pnazhz52vspqridlw72wd8x5l3dsilpdxl491aa2nws"))
       (file-name (git-file-name name version))))
    (build-system cmake-build-system)
    (arguments
     '(#:test-target "check"
       #:phases
       (modify-phases %standard-phases
         (add-after 'unpack 'patch-header-install-dir
           (lambda _
             ;; As of the writing of this package, CMake and GNU build systems
             ;; install the header to two different location.  This patch makes
             ;; the CMake build system's choice of header directory compatible
             ;; with the GNU build system's choice and with what avogadrolibs
             ;; expects.
             ;; See https://github.com/spglib/spglib/issues/75 and the relevant
             ;; part of https://github.com/OpenChemistry/avogadroapp/issues/97.
             (substitute* "CMakeLists.txt"
               (("\\$\\{CMAKE_INSTALL_INCLUDEDIR\\}" include-dir)
                (string-append include-dir "/spglib")))
             #t)))))
    (home-page "https://spglib.github.io/spglib/index.html")
    (synopsis "Library for crystal symmetry search")
    (description "Spglib is a library for finding and handling crystal
symmetries written in C.  Spglib can be used to:

@enumerate
@item Find symmetry operations
@item Identify space-group type
@item Wyckoff position assignment
@item Refine crystal structure
@item Find a primitive cell
@item Search irreducible k-points
@end enumerate")
    (license license:bsd-3)))

repo_name: maximed/guix

license: mit


code:
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2016 Federico Beffa <beffa@fbengineering.ch>
;;; Copyright © 2016 Efraim Flashner <efraim@flashner.co.il>
;;; Copyright © 2017, 2019 Tobias Geerinckx-Rice <me@tobias.gr>
;;; Copyright © 2019 Brett Gilio <brettg@gnu.org>
;;; Copyright © 2020 Brendan Tildesley <mail@brendan.scot>
;;; Copyright © 2021 Philip McGrath <philip@philipmcgrath.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu packages chez)
  #:use-module (gnu packages)
  #:use-module ((guix licenses)
                #:select (gpl2+ gpl3+ lgpl2.0+ lgpl2.1+ asl2.0 bsd-3 expat
                          public-domain))
  #:use-module (guix packages)
  #:use-module (guix download)
  #:use-module (guix git-download)
  #:use-module (guix utils)
  #:use-module (guix gexp)
  #:use-module (guix build-system gnu)
  #:use-module (gnu packages compression)
  #:use-module (gnu packages ncurses)
  #:use-module (gnu packages ghostscript)
  #:use-module (gnu packages linux)
  #:use-module (gnu packages netpbm)
  #:use-module (gnu packages tex)
  #:use-module (gnu packages compression)
  #:use-module (gnu packages image)
  #:use-module (gnu packages xorg)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-1))

(define nanopass
  (let ((version "1.9.2"))
    (origin
      (method git-fetch)
      (uri (git-reference
            (url "https://github.com/nanopass/nanopass-framework-scheme")
            (commit (string-append "v" version))))
      (sha256 (base32 "16vjsik9rrzbabbhbxbaha51ppi3f9n8rk59pc6zdyffs0vziy4i"))
      (file-name (git-file-name "nanopass" version)))))

(define stex
  ;; This commit includes a fix, which we would otherwise want to use as
  ;; patch.  Let's revert to tagged releases as soon as one becomes available.
  (let* ((commit "54051494434a197772bf6ca5b4e6cf6be55f39a5")
         (version "1.2.2")
         (version (git-version version "1" commit)))
    (origin
      (method git-fetch)
      (uri (git-reference
            (url "https://github.com/dybvig/stex")
            (commit commit)))
      (sha256 (base32 "01jnvw8qw33gnpzwrakwhsr05h6b609lm180jnspcrb7lds2p23d"))
      (file-name (git-file-name "stex" version)))))

(define-public chez-scheme
  (package
    (name "chez-scheme")
    (version "9.5.4")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url "https://github.com/cisco/ChezScheme")
             (commit (string-append "v" version))))
       (sha256
        (base32 "0prgn2z9l888j93ydxaf04ph424g0fi3a8w7f8m0b2r7fr1v7388"))
       (file-name (git-file-name name version))
       (patches
        (search-patches
         ;; backported from upstream: remove on next release
         "chez-scheme-build-util-paths-backport.patch"))
       (snippet
        ;; remove bundled libraries
        (with-imported-modules '((guix build utils))
          #~(begin
              (use-modules (guix build utils))
              (for-each (lambda (dir)
                          (when (directory-exists? dir)
                            (delete-file-recursively dir)))
                        '("stex"
                          "nanopass"
                          "lz4"
                          "zlib")))))))
    (build-system gnu-build-system)
    (inputs
     `(("libuuid" ,util-linux "lib")
       ("zlib" ,zlib)
       ("zlib:static" ,zlib "static")
       ("lz4" ,lz4)
       ("lz4:static" ,lz4 "static")
       ;; for expeditor:
       ("ncurses" ,ncurses)
       ;; for X11 clipboard support in expeditor:
       ;; https://github.com/cisco/ChezScheme/issues/9#issuecomment-222057232
       ("libx11" ,libx11)))
    (native-inputs
     `(("nanopass" ,nanopass) ; source only
       ;; for docs
       ("stex" ,stex)
       ("xorg-rgb" ,xorg-rgb)
       ("texlive" ,(texlive-union (list texlive-latex-oberdiek
                                        texlive-generic-epsf)))
       ("ghostscript" ,ghostscript)
       ("netpbm" ,netpbm)))
    (native-search-paths
     (list (search-path-specification
            (variable "CHEZSCHEMELIBDIRS")
            (files (list (string-append "lib/csv" version "-site"))))))
    (outputs '("out" "doc"))
    (arguments
     `(#:modules
       ((guix build gnu-build-system)
        (guix build utils)
        (ice-9 ftw)
        (ice-9 match))
       #:test-target "test"
       #:configure-flags
       '("--threads") ;; TODO when we fix armhf, it doesn't support --threads
       #:phases
       (modify-phases %standard-phases
         ;; put these where configure expects them to be
         (add-after 'unpack 'unpack-nanopass+stex
           (lambda* (#:key native-inputs inputs #:allow-other-keys)
             (for-each (lambda (dep)
                         (define src
                           (assoc-ref (or native-inputs inputs) dep))
                         (copy-recursively src dep
                                           #:keep-mtime? #t))
                       '("nanopass" "stex"))
               #t))
         ;; NOTE: the custom Chez 'configure' script doesn't allow
         ;; unrecognized flags, such as those automatically added
         ;; by `gnu-build-system`.
         (replace 'configure
           (lambda* (#:key inputs outputs
                           (configure-flags '())
                           #:allow-other-keys)
             (let* ((zlib-static (assoc-ref inputs "zlib:static"))
                    (lz4-static (assoc-ref inputs "lz4:static"))
                    (out (assoc-ref outputs "out"))
                    ;; add flags which are always required:
                    (flags (cons*
                            (string-append "--installprefix=" out)
                            (string-append "ZLIB=" zlib-static "/lib/libz.a")
                            (string-append "LZ4=" lz4-static "/lib/liblz4.a")
                            ;; Guix will do compress man pages,
                            ;; and letting Chez try causes an error
                            "--nogzip-man-pages"
                            configure-flags)))
               (format #t "configure flags: ~s~%" flags)
               ;; Some makefiles (for tests) don't seem to propagate CC
               ;; properly, so we take it out of their hands:
               (setenv "CC" ,(cc-for-target))
               (apply invoke
                      "./configure"
                      flags)
               #t)))
         ;; The binary file name is called "scheme" as is the one from MIT/GNU
         ;; Scheme.  We add a symlink to use in case both are installed.
         (add-after 'install 'install-symlink
           (lambda* (#:key outputs #:allow-other-keys)
             (let* ((out (assoc-ref outputs "out"))
                    (bin (string-append out "/bin"))
                    (lib (string-append out "/lib"))
                    (name "chez-scheme"))
               (symlink (string-append bin "/scheme")
                        (string-append bin "/" name))
               (map (lambda (file)
                      (symlink file (string-append (dirname file)
                                                   "/" name ".boot")))
                    (find-files lib "scheme.boot"))
               #t)))
         ;; Building explicitly lets us avoid using substitute*
         ;; to re-write makefiles.
         (add-after 'install-symlink 'prepare-stex
           (lambda* (#:key native-inputs inputs outputs #:allow-other-keys)
             (let* ((stex+version
                     (strip-store-file-name
                      (assoc-ref (or native-inputs inputs) "stex")))
                    ;; Eventually we want to install stex as a real
                    ;; package so it's reusable. For now:
                    (stex-output "/tmp")
                    (doc-dir (string-append stex-output
                                            "/share/doc/"
                                            stex+version)))
               (with-directory-excursion "stex"
                 (invoke "make"
                         "install"
                         (string-append "LIB="
                                        stex-output
                                        "/lib/"
                                        stex+version)
                         (string-append "Scheme="
                                        (assoc-ref outputs "out")
                                        "/bin/scheme"))
                 (for-each (lambda (pth)
                             (install-file pth doc-dir))
                           '("ReadMe" ; includes the license
                             "doc/stex.html"
                             "doc/stex.css"
                             "doc/stex.pdf"))
                 #t))))
         ;; Building the documentation requires stex and a running scheme.
         ;; FIXME: this is probably wrong for cross-compilation
         (add-after 'prepare-stex 'install-doc
           (lambda* (#:key native-inputs inputs outputs #:allow-other-keys)
             (let* ((chez+version (strip-store-file-name
                                   (assoc-ref outputs "out")))
                    (stex+version
                     (strip-store-file-name
                      (assoc-ref (or native-inputs inputs) "stex")))
                    (scheme (string-append (assoc-ref outputs "out")
                                           "/bin/scheme"))
                    ;; see note on stex-output in phase build-stex, above:
                    (stexlib (string-append "/tmp"
                                            "/lib/"
                                            stex+version))
                    (doc-dir (string-append (assoc-ref outputs "doc")
                                            "/share/doc/"
                                            chez+version)))
               (define* (stex-make #:optional (suffix ""))
                 (invoke "make"
                         "install"
                         (string-append "Scheme=" scheme)
                         (string-append "STEXLIB=" stexlib)
                         (string-append "installdir=" doc-dir suffix)))
               (with-directory-excursion "csug"
                 (stex-make "/csug"))
               (with-directory-excursion "release_notes"
                 (stex-make "/release_notes"))
               (with-directory-excursion doc-dir
                 (symlink "release_notes/release_notes.pdf"
                          "release_notes.pdf")
                 (symlink "csug/csug9_5.pdf"
                          "csug.pdf"))
               #t))))))
    ;; Chez Scheme does not have a  MIPS backend.
    ;; FIXME: Debian backports patches to get armhf working.
    ;; We should too. It is the Chez machine type arm32le
    ;; (no threaded version upstream yet, though there is in
    ;; Racket's fork), more specifically (per the release notes) ARMv6.
    (supported-systems (fold delete %supported-systems
                             '("mips64el-linux" "armhf-linux")))
    (home-page "https://cisco.github.io/ChezScheme/")
    (synopsis "R6RS Scheme compiler and run-time")
    (description
     "Chez Scheme is a compiler and run-time system for the language of the
Revised^6 Report on Scheme (R6RS), with numerous extensions.  The compiler
generates native code for each target processor, with support for x86, x86_64,
and 32-bit PowerPC architectures.")
    (license asl2.0)))

(define-public chez-srfi
  (package
    (name "chez-srfi")
    (version "1.0")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url "https://github.com/fedeinthemix/chez-srfi")
             (commit (string-append "v" version))))
       (sha256
        (base32 "1vgn984mj2q4w6r2q66h7qklp2hrh85wwh4k9yisga5fi0ps7myf"))
       (file-name (git-file-name name version))))
    (build-system gnu-build-system)
    (native-inputs
     `(("chez-scheme" ,chez-scheme)))
    (arguments
     `(#:make-flags (let ((out (assoc-ref %outputs "out")))
                      (list (string-append "PREFIX=" out)
                            "CHEZ=chez-scheme --libdirs ./"
                            (string-append "chezversion=" ,(package-version chez-scheme))))
       #:test-target "test"
       #:phases (modify-phases %standard-phases
                  (delete 'configure))))
    (home-page "https://github.com/fedeinthemix/chez-srfi")
    (synopsis "SRFI libraries for Chez Scheme")
    (description
     "This package provides a collection of SRFI libraries for Chez Scheme.")
    (license expat)))

(define-public chez-web
  (let ((commit "5fd177fe53f31f466bf88720d03c95a3711a8bea")
        (revision "1"))
    (package
      (name "chez-web")
      ;; Release 2.0 is different and doesn't work.
      (version (git-version "2.0" revision commit))
      (source
       (origin
         (method git-fetch)
         (uri (git-reference
               (url "https://github.com/arcfide/ChezWEB")
               (commit commit)))
         (file-name (git-file-name name version))
         (sha256
          (base32 "1dq25qygyncbfq4kwwqqgyyakfqjwhp5q23vrf3bff1p66nyfl3b"))))
      (build-system gnu-build-system)
      (native-inputs
       `(("chez-scheme" ,chez-scheme)
         ("ghostscript" ,ghostscript)
         ("texlive" ,(texlive-union (list texlive-latex-oberdiek
                                          texlive-generic-epsf
                                          texlive-metapost
                                          texlive-fonts-charter
                                          texlive-generic-pdftex
                                          texlive-context-base
                                          texlive-fonts-cm
                                          texlive-tex-plain)))))
      (arguments
       `(#:make-flags (list (string-append "PREFIX=" %output)
                            (string-append "DOCDIR=" %output "/share/doc/"
                                           ,name "-" ,version)
                            (string-append "LIBDIR=" %output "/lib/chezweb")
                            (string-append "TEXDIR=" %output "/share/texmf-local"))
                      #:tests? #f        ; no tests
                      #:phases
                      (modify-phases %standard-phases
                        (add-before 'build 'set-HOME
                          (lambda _
                            ;; FIXME: texlive-union does not find the built
                            ;; metafonts, so it tries to generate them in HOME.
                            (setenv "HOME" "/tmp")
                            #t))
                        ;; This package has a custom "bootstrap" script that
                        ;; is meant to be run from the Makefile.
                        (delete 'bootstrap)
                        (replace 'configure
                          (lambda* _
                            (copy-file "config.mk.template" "config.mk")
                            (substitute* "tangleit"
                              (("\\./cheztangle\\.ss" all)
                               (string-append "chez-scheme --program " all)))
                            (substitute* "weaveit"
                              (("mpost chezweb\\.mp")
                               "mpost --tex=tex chezweb.mp")
                              (("\\./chezweave" all)
                               (string-append "chez-scheme --program " all)))
                            (substitute* "installit"
                              (("-g \\$GROUP -o \\$OWNER") ""))
                            #t)))))
      (home-page "https://github.com/arcfide/ChezWEB")
      (synopsis "Hygienic Literate Programming for Chez Scheme")
      (description "ChezWEB is a system for doing Knuthian style WEB
programming in Scheme.")
      (license expat))))

(define-public chez-sockets
  (let ((commit "bce96881c06bd69a6757a6bff139744153924140")
        (revision "1"))
    (package
      (name "chez-sockets")
      (version (git-version "0.0.0" revision commit))
      (source
       (origin
         (method git-fetch)
         (uri (git-reference
               (url "https://github.com/arcfide/chez-sockets")
               (commit commit)))
         (file-name (git-file-name name version))
         (sha256
          (base32 "1n5fbwwz51fdzvjackgmnsgh363g9inyxv7kmzi0469cwavwcx5m"))))
      (build-system gnu-build-system)
      (native-inputs
       `(("chez-scheme" ,chez-scheme)
         ("chez-web" ,chez-web)
         ("texlive" ,(texlive-union (list texlive-generic-pdftex)))))
      (arguments
       `(#:tests? #f              ; no tests
         #:phases
         (modify-phases %standard-phases
           (replace 'configure
             (lambda* (#:key outputs inputs #:allow-other-keys)
               (let* ((out (assoc-ref outputs "out"))
                      (chez-web (assoc-ref inputs "chez-web"))
                      (chez (assoc-ref inputs "chez-scheme"))
                      (chez-h (dirname (car (find-files chez "scheme\\.h")))))
                 (substitute* "Makefile"
                   (("(SCHEMEH=).*$" all var)
                    (string-append var chez-h)))
                 #t)))
           (add-before 'build 'tangle
             (lambda* (#:key inputs #:allow-other-keys)
               (setenv "TEXINPUTS"
                       (string-append
                        (getcwd) ":"
                        (assoc-ref inputs "chez-web") "/share/texmf-local/tex/generic:"
                        ":"))
               ;; just using "make" tries to build the .c files before
               ;; they are created.
               (and (invoke "make" "sockets")
                    (invoke "make"))))
           (replace 'build
             (lambda* (#:key outputs inputs #:allow-other-keys)
               (let* ((out (assoc-ref outputs "out"))
                      (chez-site (string-append out "/lib/csv"
                                                ,(package-version chez-scheme)
                                                "-site/arcfide")))
                 ;; make sure Chez Scheme can find the shared libraries.
                 (substitute* "sockets.ss"
                   (("(load-shared-object) \"(socket-ffi-values\\.[sd][oy].*)\""
                     all cmd so)
                    (string-append cmd " \"" chez-site "/" so "\""))
                   (("sockets-stub\\.[sd][oy].*" all)
                    (string-append chez-site "/" all)))
                 ;; to compile chez-sockets, the .so files must be
                 ;; installed (because of the absolute path we
                 ;; inserted above).
                 (for-each (lambda (f d) (install-file f d))
                           '("socket-ffi-values.so" "sockets-stub.so")
                           (list chez-site chez-site))
                 (zero? (system "echo '(compile-file \"sockets.sls\")' | scheme -q")))))
           (replace 'install
             (lambda* (#:key outputs inputs #:allow-other-keys)
               (let* ((out (assoc-ref outputs "out"))
                      (lib (string-append out "/lib/chez-sockets"))
                      (doc (string-append out "/share/doc/" ,name "-" ,version))
                      (chez-site (string-append out "/lib/csv"
                                                ,(package-version chez-scheme)
                                                "-site/arcfide")))
                 (for-each (lambda (f d) (install-file f d))
                           '("sockets.pdf" "sockets.so")
                           (list doc chez-site))
                 #t))))))
      (home-page "https://github.com/arcfide/chez-sockets")
      (synopsis "Extensible sockets library for Chez Scheme")
      (description "Chez-sockets is an extensible sockets library for
Chez Scheme.")
      (license expat))))

;; Help function for Chez Scheme to add the current path to
;; CHEZSCHEMELIBDIRS.
(define chez-configure
  '(lambda _
     (let ((chez-env (getenv "CHEZSCHEMELIBDIRS")))
       (setenv "CHEZSCHEMELIBDIRS"
               (if chez-env
                   (string-append ".:" chez-env)
                   "."))
       #t)))

;; Help function to define make flags for some Chez Scheme custom make
;; files.
(define (chez-make-flags name version)
  `(let ((out (assoc-ref %outputs "out")))
     (list
      ;; Set 'chezversion' so that libraries are installed in
      ;; 'lib/csvX.Y.Z-site' like Chez's 'native-search-paths' expects.
      (string-append "chezversion=" ,(package-version chez-scheme))
      (string-append "PREFIX=" out)
      (string-append "DOCDIR=" out "/share/doc/"
                     ,name "-" ,version))))

(define-public chez-matchable
  (package
    (name "chez-matchable")
    (version "20160306")
    (home-page "https://github.com/fedeinthemix/chez-matchable")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url home-page)
             (commit (string-append "v" version))))
       (sha256
        (base32 "02qn7x348p23z1x5lwhkyj7i8z6mgwpzpnwr8dyina0yzsdkr71s"))
       (file-name (git-file-name name version))))
    (build-system gnu-build-system)
    (inputs
     `(("chez-srfi" ,chez-srfi))) ; for tests
    (native-inputs
     `(("chez-scheme" ,chez-scheme)))
    (arguments
     `(#:make-flags ,(chez-make-flags name version)
       #:test-target "test"
       #:phases (modify-phases %standard-phases
                  (replace 'configure ,chez-configure))))
    (synopsis "Portable hygienic pattern matcher for Scheme")
    (description "This package provides a superset of the popular Scheme
@code{match} package by Andrew Wright, written in fully portable
@code{syntax-rules} and thus preserving hygiene.")
    (license public-domain)))

(define-public chez-irregex
  (package
    (name "chez-irregex")
    (version "0.9.4")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url "https://github.com/fedeinthemix/chez-irregex")
             (commit (string-append "v" version))))
       (sha256
        (base32 "0jh6piylw545j81llay9wfivgpv6lcnwd81gm4w17lkasslir50q"))
       (file-name (git-file-name name version))))
    (build-system gnu-build-system)
    (inputs
     `(("chez-matchable" ,chez-matchable))) ; for tests
    (propagated-inputs
     `(("chez-srfi" ,chez-srfi))) ; for irregex-utils
    (native-inputs
     `(("chez-scheme" ,chez-scheme)))
    (arguments
     `(#:make-flags ,(chez-make-flags name version)
       #:test-target "test"
       #:phases (modify-phases %standard-phases
                  (replace 'configure ,chez-configure))))
    (home-page "https://github.com/fedeinthemix/chez-irregex")
    (synopsis "Portable regular expression library for Scheme")
    (description "This package provides a portable and efficient
R[4567]RS implementation of regular expressions, supporting both POSIX
syntax with various (irregular) PCRE extensions, as well as SCSH's SRE
syntax, with various aliases for commonly used patterns.")
    (license bsd-3)))

(define-public chez-fmt
  (package
    (name "chez-fmt")
    (version "0.8.11")
    (source
     (origin
       (method url-fetch)
       (uri (string-append
             "http://synthcode.com/scheme/fmt/fmt-" version ".tar.gz"))
       (sha256
        (base32 "1zxqlw1jyg85yzclylh8bp2b3fwcy3l3xal68jw837n5illvsjcl"))
       (file-name (string-append name "-" version ".tar.gz"))))
    (build-system gnu-build-system)
    (propagated-inputs
     `(("chez-srfi" ,chez-srfi))) ; for irregex-utils
    (native-inputs
     `(("chez-scheme" ,chez-scheme)))
    (arguments
     `(#:make-flags ,(chez-make-flags name version)
       #:test-target "chez-check"
       #:phases
       (modify-phases %standard-phases
         (replace 'configure ,chez-configure)
         (replace 'build
           (lambda* (#:key (make-flags '()) #:allow-other-keys)
             (apply invoke "make" "chez-build" make-flags)))
         (replace 'install
           (lambda* (#:key (make-flags '()) #:allow-other-keys)
             (apply invoke "make" "chez-install" make-flags))))))
    (home-page "http://synthcode.com/scheme/fmt")
    (synopsis "Combinator formatting library for Chez Scheme")
    (description "This package provides a library of procedures for
formatting Scheme objects to text in various ways, and for easily
concatenating, composing and extending these formatters efficiently
without resorting to capturing and manipulating intermediate
strings.")
    (license bsd-3)))

(define-public chez-mit
  (package
    (name "chez-mit")
    (version "0.1")
    (home-page "https://github.com/fedeinthemix/chez-mit")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url home-page)
             (commit (string-append "v" version))))
       (sha256
        (base32 "0c7i3b6i90xk96nmxn1pc9272a4yal4v40dm1a4ybdi87x53zkk0"))
       (file-name (git-file-name name version))))
    (build-system gnu-build-system)
    (inputs
     `(("chez-srfi" ,chez-srfi)))       ; for tests
    (native-inputs
     `(("chez-scheme" ,chez-scheme)))
    (arguments
     `(#:make-flags ,(chez-make-flags name version)
       #:test-target "test"
       #:phases (modify-phases %standard-phases
                  (replace 'configure ,chez-configure))))
    (synopsis "MIT/GNU Scheme compatibility library for Chez Scheme")
    (description "This package provides a set of MIT/GNU Scheme compatibility
libraries for Chez Scheme.  The main goal was to provide the functionality
required to port the program @code{Scmutils} to Chez Scheme.")
    (license gpl3+)))

(define-public chez-scmutils
  (package
    (name "chez-scmutils")
    (version "0.1")
    (home-page "https://github.com/fedeinthemix/chez-scmutils")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url home-page)
             (commit (string-append "v" version))))
       (sha256
        (base32 "0lb05wlf8qpgg8y0gdsyaxg1nbfx1qbaqdjvygrp64ndn8fnhq7l"))
       (file-name (git-file-name name version))))
    (build-system gnu-build-system)
    (inputs
     `(("chez-srfi" ,chez-srfi)))       ; for tests
    (native-inputs
     `(("chez-scheme" ,chez-scheme)))
    (propagated-inputs
     `(("chez-mit" ,chez-mit)
       ("chez-srfi" ,chez-srfi)))
    (arguments
     `(#:make-flags ,(chez-make-flags name version)
       #:tests? #f                      ; no test suite
       #:phases
       (modify-phases %standard-phases
         (replace 'configure ,chez-configure)
         ;; Since the documentation is lacking, we install the source
         ;; code.  For things to work correctly we have to replace
         ;; relative paths by absolute ones in 'include' forms.  This
         ;; in turn requires us to compile the files in the final
         ;; destination.
         (delete 'build)
         (add-after 'install 'install-src
           (lambda* (#:key (make-flags '()) #:allow-other-keys)
             (apply invoke "make" "install-src" make-flags)))
         (add-after 'install-src 'absolute-path-in-scm-files
           (lambda* (#:key outputs #:allow-other-keys)
             (let ((out (assoc-ref outputs "out")))
               (for-each (lambda (file)
                           (substitute* file
                             (("include +\"\\./scmutils")
                              (string-append "include \"" (dirname file)))))
                         (find-files out "\\.sls"))
               (for-each (lambda (file)
                           (substitute* file
                             (("include +\"\\./scmutils/simplify")
                              (string-append "include \"" (dirname file)))))
                         (find-files out "fbe-syntax\\.scm"))
               #t)))
         (add-after 'absolute-path-in-scm-files 'build
           (lambda* (#:key outputs (make-flags '()) #:allow-other-keys)
             (let* ((out (assoc-ref outputs "out"))
                    (mk-file (car (find-files out "Makefile"))))
               (with-directory-excursion (dirname mk-file)
                 (apply invoke "make" "build" make-flags)))))
         (add-after 'build 'clean-up
           (lambda* (#:key outputs #:allow-other-keys)
             (let* ((out (assoc-ref outputs "out")))
               (for-each delete-file
                         (find-files out "Makefile|compile-all\\.ss"))
               #t))))))
    (synopsis "Port of MIT/GNU Scheme Scmutils to Chez Scheme")
    (description "This package provides a port of the MIT/GNU Scheme
Scmutils program to Chez Scheme.  The port consists of a set of
libraries providing most of the functionality of the original.")
    (license gpl3+)))


code:
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2020 Ekaitz Zarraga <ekaitz@elenq.tech>
;;; Copyright © 2020 Evan Hanson <evhan@foldling.org>
;;; Copyright © 2020 raingloom <raingloom@riseup.net>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu packages chicken)
  #:use-module (gnu packages)
  #:use-module (guix packages)
  #:use-module (guix build-system chicken)
  #:use-module (guix build-system gnu)
  #:use-module (guix download)
  #:use-module (guix git-download)
  #:use-module (guix svn-download)
  #:use-module (gnu packages commencement)
  #:use-module ((guix licenses)
                #:prefix license:))

(define-public chicken
  (package
    (name "chicken")
    (version "5.2.0")
    (source (origin
              (method url-fetch)
              (uri (string-append "https://code.call-cc.org/releases/"
                                  version "/chicken-" version ".tar.gz"))
              (sha256
               (base32
                "1yl0hxm9cirgcp8jgxp6vv29lpswfvaw3zfkh6rsj0vkrv44k4c1"))))
    (build-system gnu-build-system)
    (arguments
     `(#:modules ((guix build gnu-build-system)
                  (guix build utils)
                  (srfi srfi-1))

       ;; No `configure' script; run "make check" after "make install" as
       ;; prescribed by README.
       #:phases
       (modify-phases %standard-phases
         (delete 'configure)
         (delete 'check)
         (add-after 'install 'check
           (assoc-ref %standard-phases 'check)))

       #:make-flags (let ((out (assoc-ref %outputs "out")))
                      (list "PLATFORM=linux"
                            (string-append "PREFIX=" out)
                            (string-append "VARDIR=" out "/var/lib")))

       ;; Parallel builds are not supported, as noted in README.
       #:parallel-build? #f))
    (native-search-paths
     (list (search-path-specification
            (variable "CHICKEN_REPOSITORY_PATH")
            ;; TODO extract binary version into a module level definition.
            (files (list "var/lib/chicken/11")))))
    (propagated-inputs `(("gcc-toolchain" ,gcc-toolchain)))
    (home-page "https://www.call-cc.org/")
    (synopsis "R5RS Scheme implementation that compiles native code via C")
    (description
     "CHICKEN is a compiler for the Scheme programming language.  CHICKEN
produces portable and efficient C, supports almost all of the R5RS Scheme
language standard, and includes many enhancements and extensions.")
    (license license:bsd-3)))

(define-public chicken-srfi-1
  (package
    (name "chicken-srfi-1")
    (version "0.5.1")
    (source
     (origin
       (method svn-fetch)
       (uri (svn-reference
             (url (string-append
                   "https://code.call-cc.org/svn/chicken-eggs/"
                   "release/5/srfi-1/tags/"
                   version))
             (revision 39055)
             (user-name "anonymous")
             (password "")))
       (file-name (string-append "chicken-srfi-1" version "-checkout"))
       (sha256
        (base32
         "02940zsjrmn7c34rnp1rllm2nahh9jvszlzrw8ak4pf31q09cmq1"))))
    (build-system chicken-build-system)
    (arguments '(#:egg-name "srfi-1"))
    (inputs
     `(("chicken-test" ,chicken-test)))
    (home-page "https://wiki.call-cc.org/eggref/5/srfi-1")
    (synopsis "SRFI-1 list library")
    (description
     "The list library defined in
@uref{https://srfi.schemers.org/srfi-1/srfi-1.html, SRFI-1} contains a lot of
useful list processing procedures for construction, examining, destructuring
and manipulating lists and pairs.")
    (license license:bsd-3)))

(define-public chicken-srfi-14
  (package
    (name "chicken-srfi-14")
    (version "0.2.1")
    (source
     (origin
       (method svn-fetch)
       (uri (svn-reference
             (url (string-append "https://code.call-cc.org/svn/chicken-eggs/"
                                 "release/5/srfi-14/tags/" version))
             (revision 39057)
             (user-name "anonymous")
             (password "")))
       (file-name (string-append "chicken-srfi-14-" version "-checkout"))
       (sha256
        (base32
         "0wjsqfwawh9bx6vvii1gwag166bxkflc0ib374fbws14914g2ac1"))))
    (build-system chicken-build-system)
    (arguments '(#:egg-name "srfi-14"))
    (home-page "https://wiki.call-cc.org/eggref/5/srfi-14")
    (synopsis "Character set library")
    (description
     "Character sets can be created, extended, tested for the membership of
a characters and be compared to other character sets")
    (license (license:non-copyleft
              "http://wiki.call-cc.org/eggref/5/srfi-14#license"))))

(define-public chicken-srfi-69
  (package
    (name "chicken-srfi-69")
    (version "0.4.1")
    (source
     (origin
       (method svn-fetch)
       (uri (svn-reference
             (url (string-append "https://code.call-cc.org/svn/chicken-eggs/"
                                 "release/5/srfi-69/tags/"
                                 version))
             (revision 39057)
             (user-name "anonymous")
             (password "")))
       (file-name (string-append "chicken-srfi-69-" version "-checkout"))
       (sha256
        (base32
         "1z0m9vmg9bj9z0a941pls6igvg8nmhq4mj5psjjidbp0fac572mp"))))
    (arguments '(#:egg-name "srfi-69"))
    (build-system chicken-build-system)
    (home-page "https://wiki.call-cc.org/eggref/5/srfi-69")
    (synopsis "Implementation of SRFI 69 with SRFI 90 extensions")
    (description
     "This package provides an implementation of
@uref{https://srfi.schemers.org/srfi-69/srfi-69.html, SRFI-69 hash tables} for
CHICKEN Scheme, along with
@uref{https://srfi.schemers.org/srfi-90/srfi-90.html, SRFI-90} extensions.")
    (license license:bsd-3)))

(define-public chicken-agrep
  (package
    (name "chicken-agrep")
    (version "1.7")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url "https://github.com/iraikov/chicken-agrep")
             (commit version)))
       (file-name (string-append "chicken-agrep-" version "-checkout"))
       (sha256
        (base32
         "0z05x7f154n9bgmainrsmncf5i6dil43r9ymr3rdgwbg4wnxmz4s"))))
    ;; TODO do we really have to make these propagated?
    ;; I don't know Chicken's module system well enough to tell
    (propagated-inputs
     `(("chicken-datatype" ,chicken-datatype)
       ("chicken-srfi-1" ,chicken-srfi-1)
       ("chicken-srfi-14" ,chicken-srfi-14)))
    (inputs
     `(("chicken-test" ,chicken-test)))
    (build-system chicken-build-system)
    (arguments '(#:egg-name "agrep"))
    (synopsis "Approximate string matching library")
    (home-page "https://wiki.call-cc.org/eggref/5/agrep")
    (description
     "This library implements the Wu-Manber algorithm for approximate string
searching with errors, popularized by the agrep Unix command and the glimpse
file indexing tool.")
    (license license:gpl3+)))

(define-public chicken-datatype
  (package
    (name "chicken-datatype")
    (version "1.6")
    (source
     (origin
       (method svn-fetch)
       (uri (svn-reference
             (url (string-append "https://code.call-cc.org/svn/chicken-eggs/"
                                 "release/5/datatype/tags/" version))
             (revision 39266)
             (user-name "anonymous")
             (password "")))
       (file-name (string-append "chicken-datatype-" version "-checkout"))
       (sha256
        (base32
         "1a58q8ypjkjz6wdv25247wixds4179239d36nnvsfn6gp70s9jfq"))))
    (build-system chicken-build-system)
    (arguments '(#:egg-name "datatype"))
    (inputs
     `(("chicken-srfi-1" ,chicken-srfi-1)
       ("chicken-test" ,chicken-test)))
    (home-page "https://wiki.call-cc.org/eggref/5/datatype")
    (synopsis "Facility for creating and using variant records")
    (description
     "This CHICKEN Scheme library provides a facility for creating and using
variant records, as described in the book @i{Essentials of Programming
Languages} by Friedman, Wand, and Haynes.")
    (license license:bsd-3)))

(define-public chicken-iset
  (package
    (name "chicken-iset")
    (version "2.2")
    (source
     (origin
       (method svn-fetch)
       (uri (svn-reference
             (url (string-append "https://code.call-cc.org/svn/chicken-eggs/"
                                 "release/5/iset/tags/" version))
             (revision 39057)
             (user-name "anonymous")
             (password "")))
       (file-name (string-append "chicken-iset-" version "-checkout"))
       (sha256
        (base32
         "0gz50n5i561n5sk9prry0lrxz6bfrq9if5bclaq6a0f7lzinhnzb"))))
    (inputs
     `(("chicken-test" ,chicken-test)))
    (build-system chicken-build-system)
    (arguments '(#:egg-name "iset"))
    (synopsis "Integer set library")
    (home-page "https://wiki.call-cc.org/eggref/5/iset")
    (description
     "This ``integer set'' CHICKEN Scheme library implements bit vectors.
Bit-vectors provide an abstract interface to bitwise operations typically done
with integers.")
    (license license:bsd-3)))

(define-public chicken-test
  (package
    (name "chicken-test")
    (version "1.1")
    (source
     (origin
       (method svn-fetch)
       (uri (svn-reference
             (url (string-append "https://code.call-cc.org/svn/chicken-eggs/"
                                 "release/5/test/tags/" version))
             (revision 39263)
             (user-name "anonymous")
             (password "")))
       (file-name (string-append "chicken-test-" version "-checkout"))
       (sha256
        (base32
         "14i91cxsn6hjkx6kqf7i9syck73cw71jik61jmhg87vpxx5kfnzx"))))
    (build-system chicken-build-system)
    (arguments '(#:egg-name "test"))
    (home-page "https://wiki.call-cc.org/eggref/5/test")
    (synopsis "Yet another testing utility")
    (description
     "This package provides a simple testing utility for CHICKEN Scheme.")
    (license license:bsd-3)))