(use-modules
  ((guix licenses) #:prefix license:)
  (guix packages)
  (guix download)
  (guix gexp)
  (guix git-download)
  (guix build-system ruby)
  (guix build-system copy)
  (gnu packages databases)
  (gnu packages rails)
  (gnu packages ruby)
  (gnu packages tls)
  (ice-9 rdelim)
  (ice-9 popen))

(define-public ruby-guard-compat
  (package
    (name "ruby-guard-compat")
    (version "1.2.1")
    (source (origin
              (method url-fetch)
              (uri (rubygems-uri "guard-compat" version))
              (sha256
               (base32
                "1zj6sr1k8w59mmi27rsii0v8xyy2rnsi09nqvwpgj1q10yq1mlis"))))
    (build-system ruby-build-system)
    (native-inputs (list ruby-rspec ruby-rubocop))
    (synopsis "Helps creating valid Guard plugins and testing them")
    (description "Helps creating valid Guard plugins and testing them")
    (home-page "")
    (license license:expat)))

(define-public ruby-guard-rspec
  (package
    (name "ruby-guard-rspec")
    (version "4.7.3")
    (source (origin
              (method url-fetch)
              (uri (rubygems-uri "guard-rspec" version))
              (sha256
               (base32
                "1jkm5xp90gm4c5s51pmf92i9hc10gslwwic6mvk72g0yplya0yx4"))))
    (build-system ruby-build-system)
    (propagated-inputs (list ruby-guard ruby-guard-compat ruby-rspec))
    (synopsis
     "Guard::RSpec automatically run your specs (much like autotest).")
    (description
     "Guard::RSpec automatically run your specs (much like autotest).")
    (home-page "https://github.com/guard/guard-rspec")
    (license license:expat)))

(define-public ruby-blather
  (package
    (name "ruby-blather")
    (version "03f9cda14ed3f93a80504c2538cd514d6b025d04")
    (source
      (origin
      (method git-fetch)
      ;; Download from GitHub because the rubygems version requires old
      ;; version of bundler Rakefile.
      (uri (git-reference
          (url "https://github.com/adhearsion/blather")
          (commit version)))
      (file-name (git-file-name name version))
        (sha256
          (base32
            "0r098lx4av8qywb521ilin8j4lvrb0pmcdb2l0dkljmzkb6lxa3i"))))
    (build-system ruby-build-system)
    (arguments
     `(#:tests? #f #:phases
       (modify-phases %standard-phases
         (add-after 'extract-gemspec 'less-strict-dependencies
           (lambda _
             (substitute* "blather.gemspec"
               ((".*guard-rspec.*") "\n")
                ((".*bluecloth.*") "\n"))
             #t)))))
    (propagated-inputs
      (list ruby-activesupport
            ruby-eventmachine
            ruby-niceogiri
            ruby-nokogiri
            ruby-sucker-punch))
    (native-inputs
     (list ruby-rspec
           ruby-yard
           ruby-countdownlatch
           ruby-rb-fsevent
           ruby-mocha))
    (synopsis
      "An XMPP DSL for Ruby written on top of EventMachine and Nokogiri")
    (description
      "An XMPP DSL for Ruby written on top of EventMachine and Nokogiri")
    (home-page "http://adhearsion.com/blather")
    (license license:expat)))

(define-public ruby-hiredis
  (package
    (name "ruby-hiredis")
    (version "0.6.3")
    (source
     (origin
       (method url-fetch)
       (uri (rubygems-uri "hiredis" version))
       (sha256
        (base32 "04jj8k7lxqxw24sp0jiravigdkgsyrpprxpxm71ba93x1wr2w1bz"))))
    (build-system ruby-build-system)
    (arguments
     `(#:tests? #f
         #:phases
       (modify-phases %standard-phases
         (add-after 'extract-gemspec 'set-cc
           (lambda _
             (putenv "CC=gcc"))))))
     (native-inputs (list ruby-rake-compiler))
    (synopsis
     "Ruby wrapper for hiredis (protocol serialization/deserialization and blocking I/O)")
    (description
     "Ruby wrapper for hiredis (protocol serialization/deserialization and blocking
I/O)")
    (home-page "http://github.com/redis/hiredis-rb")
    (license #f)))

(define-public ruby-base64
  (package
    (name "ruby-base64")
    (version "0.3.0")
    (source (origin
              (method url-fetch)
              (uri (rubygems-uri "base64" version))
              (sha256
               (base32
                "0yx9yn47a8lkfcjmigk79fykxvr80r4m1i35q82sxzynpbm7lcr7"))))
    (build-system ruby-build-system)
    (arguments
     ;; No Rakefile
     `(#:tests? #f))
    (synopsis
     "Support for encoding and decoding binary data using a Base64 representation.")
    (description
     "Support for encoding and decoding binary data using a Base64 representation.")
    (home-page "https://github.com/ruby/base64")
    (license #f)))

(define-public ruby-mutex-m
  (package
    (name "ruby-mutex-m")
    (version "0.3.0")
    (source (origin
              (method url-fetch)
              (uri (rubygems-uri "mutex_m" version))
              (sha256
               (base32
                "0l875dw0lk7b2ywa54l0wjcggs94vb7gs8khfw9li75n2sn09jyg"))))
    (build-system ruby-build-system)
    (arguments
      ;; No Rakefile
      `(#:tests? #f))
    (synopsis "Mixin to extend objects to be handled like a Mutex.")
    (description "Mixin to extend objects to be handled like a Mutex.")
    (home-page "https://github.com/ruby/mutex_m")
    (license #f)))

(define-public ruby-eventmachine-openssl
    (package
      (inherit ruby-eventmachine)
      (inputs `(("openssl" ,openssl)))))

(define-public ruby-dhall
  (package
    (name "ruby-dhall")
    (version "0.5.7")
    (source
      (origin
        (method url-fetch)
        (uri (rubygems-uri "dhall" version))
        (sha256
          (base32
            "1fmd4jfd66sdyihqigc2vgdjzjy7gi513gjmwz07rds7f7x70fsq"))))
    (build-system ruby-build-system)
    (arguments
     ;; No test in gem archive
     `(#:tests? #f))
    (propagated-inputs
      `(("ruby-base32" ,ruby-base32)
        ("ruby-cbor" ,ruby-cbor)
        ("ruby-citrus" ,ruby-citrus)
        ("ruby-lazy-object" ,ruby-lazy-object)
        ("ruby-multihashes" ,ruby-multihashes)
        ("ruby-promise.rb" ,ruby-promise-rb)
        ("ruby-value-semantics" ,ruby-value-semantics)))
    (synopsis
      "This is a Ruby implementation of the Dhall configuration language. Dhall is a powerful, but safe and non-Turing-complete configuration language. For more information, see: https://dhall-lang.org")
    (description
      "This is a Ruby implementation of the Dhall configuration language.  Dhall is a powerful, but safe and non-Turing-complete configuration language.  For more information, see: https://dhall-lang.org")
    (home-page
      "https://git.sr.ht/~singpolyma/dhall-ruby")
    (license license:gpl3)))

(define-public ruby-socksify
  (package
    (name "ruby-socksify")
    (version "1.8.1")
    (source (origin
              (method url-fetch)
              (uri (rubygems-uri "socksify" version))
              (sha256
               (base32
                "0mm8m7zfvszbf9l750c2x693p8100rrk6ckvcp6909631ir02ang"))))
    (build-system ruby-build-system)
    (arguments
      ;; No rakefile
      `(#:tests? #f))
    (synopsis "Redirect all TCPSockets through a SOCKS5 proxy")
    (description "Redirect all TCPSockets through a SOCKS5 proxy")
    (home-page "https://github.com/astro/socksify-ruby")
    (license #f)))

(define-public ruby-wasabi
  (package
    (name "ruby-wasabi")
    (version "3.7.0")
    (source
     (origin
       (method url-fetch)
       (uri (rubygems-uri "wasabi" version))
       (sha256
        (base32 "1mffdf7z1rmcs0k678vhzgnb080zcwhkk94hvj3hxjakys339ndb"))))
    (build-system ruby-build-system)
    (arguments
     ;; No Rakefile
     `(#:tests? #f))
    (propagated-inputs (list ruby-addressable ruby-httpi ruby-nokogiri))
    (synopsis "A simple WSDL parser")
    (description "This package provides a simple WSDL parser.")
    (home-page "https://github.com/savonrb/wasabi")
    (license license:expat)))

(define-public ruby-nkf
  (package
    (name "ruby-nkf")
    (version "0.2.0")
    (source
     (origin
       (method url-fetch)
       (uri (rubygems-uri "nkf" version))
       (sha256
        (base32 "09piyp2pd74klb9wcn0zw4mb5l0k9wzwppxggxi1yi95l2ym3hgv"))))
    (build-system ruby-build-system)
    (native-inputs (list ruby-rake-compiler))
    (synopsis "Ruby extension for Network Kanji Filter")
    (description "Ruby extension for Network Kanji Filter.")
    (home-page "https://github.com/ruby/nkf")
    (license license:bsd-2)))

(define-public ruby-httpi
  (package
    (name "ruby-httpi")
    (version "2.5.0")
    (source
     (origin
       (method url-fetch)
       (uri (rubygems-uri "httpi" version))
       (sha256
        (base32 "0v8ah9indflp0w6jdzkzgs72xiwbam5v1c58migj0dkavkrai45h"))))
    (build-system ruby-build-system)
    (arguments ;; No Rakefile
      `(#:tests? #f))
    (propagated-inputs (list ruby-base64 ruby-mutex-m ruby-nkf ruby-rack))
    (synopsis "Common interface for Ruby's HTTP libraries")
    (description "Common interface for Ruby's HTTP libraries.")
    (home-page "http://github.com/savonrb/httpi")
    (license license:expat)))

(define-public ruby-gyoku
  (package
    (name "ruby-gyoku")
    (version "1.4.0")
    (source
     (origin
       (method url-fetch)
       (uri (rubygems-uri "gyoku" version))
       (sha256
        (base32 "1kd2q59xpm39hpvmmvyi6g3f1fr05xjbnxwkrdqz4xy7hirqi79q"))))
    (build-system ruby-build-system)
    (arguments
     ;; Tests need a whole other set of dependencies
      `(#:tests? #f))
    (propagated-inputs (list ruby-builder ruby-rexml))
    (synopsis "Gyoku translates Ruby Hashes to XML")
    (description "Gyoku translates Ruby Hashes to XML.")
    (home-page "https://github.com/savonrb/gyoku")
    (license license:expat)))

(define-public ruby-akami
  (package
    (name "ruby-akami")
    (version "1.3.1")
    (source
     (origin
       (method url-fetch)
       (uri (rubygems-uri "akami" version))
       (sha256
        (base32 "168y57kd9wshzqqk127w7lknd8lr0b9k50wazw4c92zshq3sw2jd"))))
    (build-system ruby-build-system)
    (arguments
      ;; No Rakefile
      `(#:tests? #f))
    (propagated-inputs (list ruby-base64 ruby-gyoku ruby-nokogiri))
    (synopsis "Building Web Service Security")
    (description "Building Web Service Security.")
    (home-page "https://github.com/savonrb/akami")
    (license license:expat)))

(define-public ruby-savon
  (package
    (name "ruby-savon")
    (version "2.14.0")
    (source
     (origin
       (method url-fetch)
       (uri (rubygems-uri "savon" version))
       (sha256
        (base32 "17fgsaprz5ihngmncl6f6qzja97rmfavmhhwhdspi7ql9hix5wyr"))))
    (build-system ruby-build-system)
    (native-inputs (list ruby-rspec))
    (propagated-inputs (list ruby-akami
                             ruby-builder
                             ruby-gyoku
                             ruby-httpi
                             ruby-mail
                             ruby-nokogiri
                             ruby-nori
                             ruby-socksify
                             ruby-wasabi))
    (synopsis "Heavy metal SOAP client")
    (description "Heavy metal SOAP client.")
    (home-page "http://savonrb.com")
    (license license:expat)))

(define-public ruby-em-hiredis
  (package
    (name "ruby-em-hiredis")
    (version "0.3.1")
    (source
     (origin
       (method url-fetch)
       (uri (rubygems-uri "em-hiredis" version))
       (sha256
        (base32 "0lh276x6wngq9xy75fzzvciinmdlys93db7chy968i18japghk6z"))))
    (build-system ruby-build-system)
    (arguments '(#:tests? #f))
    (propagated-inputs (list ruby-eventmachine ruby-hiredis))
    (synopsis "Eventmachine redis client using hiredis native parser")
    (description "Eventmachine redis client using hiredis native parser.")
    (home-page "http://github.com/mloughran/em-hiredis")
    (license #f)))


(define-public ruby-amazing-print
  (package
    (name "ruby-amazing-print")
    (version "1.5.0")
    (source
     (origin
       (method url-fetch)
       (uri (rubygems-uri "amazing_print" version))
       (sha256
        (base32 "0qik1igcs0ylw5b5dwx9npqac5r21d9fcv611h7klcspfari3x7r"))))
    (build-system ruby-build-system)
    (arguments '(#:tests? #f))
    (synopsis
     "Great Ruby debugging companion: pretty print Ruby objects to visualize their structure. Supports custom object formatting via plugins")
    (description
     "Great Ruby debugging companion: pretty print Ruby objects to visualize their
structure.  Supports custom object formatting via plugins.")
    (home-page "https://github.com/amazing-print/amazing_print")
    (license license:expat)))

(define-public ruby-em-socksify
  (package
    (name "ruby-em-socksify")
    (version "0.3.2")
    (source (origin
              (method url-fetch)
              (uri (rubygems-uri "em-socksify" version))
              (sha256
               (base32
                "0rk43ywaanfrd8180d98287xv2pxyl7llj291cwy87g1s735d5nk"))))
    (build-system ruby-build-system)
   (arguments
    ;; Tests are broken
    `(#:tests? #f))
    (propagated-inputs (list ruby-eventmachine))
    (synopsis "Transparent proxy support for any EventMachine protocol")
    (description "Transparent proxy support for any EventMachine protocol")
    (home-page "https://github.com/igrigorik/em-socksify")
    (license license:expat)))

(define-public ruby-cookiejar
  (package
    (name "ruby-cookiejar")
    (version "0.3.3")
    (source (origin
              (method url-fetch)
              (uri (rubygems-uri "cookiejar" version))
              (sha256
               (base32
                "0q0kmbks9l3hl0wdq744hzy97ssq9dvlzywyqv9k9y1p3qc9va2a"))))
    (build-system ruby-build-system)
   (native-inputs
    `(("ruby-rspec" ,ruby-rspec)
     ("ruby-rspec-collection-matchers" ,ruby-rspec-collection-matchers)
     ("ruby-yard" ,ruby-yard)))
    (synopsis
     "Allows for parsing and returning cookies in Ruby HTTP client code")
    (description
     "Allows for parsing and returning cookies in Ruby HTTP client code")
    (home-page "http://alkaline-solutions.com")
    (license #f)))

(define-public ruby-em-http-request
  (package
    (name "ruby-em-http-request")
    (version "b51f48fafa6443ad48c89930716c5c427561abd8")
    (source
      (origin
      (method git-fetch)
      (uri (git-reference
          (url "https://github.com/singpolyma/em-http-request")
          (commit version)))
      (file-name (git-file-name name version))
        (sha256
          (base32
            "1kbaghj1gqz1dpl2hikpa4xggfmavma2y442yzsgqk27gmfp9iqq"))))
    (build-system ruby-build-system)
    (arguments
     ;; Tests need a whole other set of dependencies
     `(#:tests? #f))
    (propagated-inputs (list ruby-addressable ruby-cookiejar ruby-em-socksify
                             ruby-eventmachine ruby-http-parser.rb))
    (synopsis "EventMachine based, async HTTP Request client")
    (description "EventMachine based, async HTTP Request client")
    (home-page "http://github.com/igrigorik/em-http-request")
    (license license:expat)))

(define-public ruby-promise-rb
  (package
    (name "ruby-promise-rb")
    (version "0.7.4")
    (source
     (origin
       (method url-fetch)
       (uri (rubygems-uri "promise.rb" version))
       (sha256
        (base32 "0a819sikcqvhi8hck1y10d1nv2qkjvmmm553626fmrh51h2i089d"))))
    (build-system ruby-build-system)
    (arguments
      ;; Depends on devtools, which has a crazy long dependency chain.
      `(#:tests? #f))
    (synopsis "Promises/A+ for Ruby")
    (description "Promises/A+ for Ruby.")
    (home-page "https://github.com/lgierth/promise")
    (license license:unlicense)))

(define-public ruby-em-promise-rb
  (package
    (name "ruby-em-promise-rb")
    (version "0.0.5")
    (source
     (origin
       (method url-fetch)
       (uri (rubygems-uri "em_promise.rb" version))
       (sha256
        (base32 "00rkmacyf6i2gq0giaxzwr24ygrwwzndrq7kkbpqcq9glk01w692"))))
    (build-system ruby-build-system)
    (arguments
      ;; No Rakefile
      `(#:tests? #f))
    (propagated-inputs (list ruby-eventmachine ruby-promise-rb))
    (synopsis "A subclass of promise.rb Promise for EventMachine.")
    (description
     "This package provides a subclass of promise.rb Promise for @code{EventMachine}.")
    (home-page "https://git.singpolyma.net/em_promise.rb")
    (license license:agpl3)))
(define-public ruby-ougai
  (package
    (name "ruby-ougai")
    (version "2.0.0")
    (source
     (origin
       (method url-fetch)
       (uri (rubygems-uri "ougai" version))
       (sha256
        (base32 "0zmngsm3lrliscry7ljw7x5gnf3m4cn4kdgnxccaidhwj0mm539f"))))
    (build-system ruby-build-system)
    ;; Tests broken
    (arguments
     `(#:tests? #f))
    (propagated-inputs (list ruby-oj))
    (synopsis
     "    A structured logging system is capable of handling a message, custom data or an exception easily.
    It has JSON formatters compatible with Bunyan or pino for Node.js and human readable formatter with Amazing Print for console.
")
    (description
     "This package provides a structured logging system is capable of handling a
message, custom data or an exception easily.  It has JSON formatters compatible
with Bunyan or pino for Node.js and human readable formatter with Amazing Print
for console.")
    (home-page "https://github.com/tilfin/ougai")
    (license license:expat)))

(define-public ruby-rspec-collection-matchers
  (package
    (name "ruby-rspec-collection-matchers")
    (version "1.2.0")
    (source (origin
              (method url-fetch)
              (uri (rubygems-uri "rspec-collection_matchers" version))
              (sha256
               (base32
                "1864xlxl7mi6mvjyp85a0gc10cyvpf6bj8lc86sf8737wlzn12ks"))))
    (build-system ruby-build-system)
   (arguments
    ;; No rakefile
    `(#:tests? #f))
    (propagated-inputs (list ruby-rspec-expectations))
    (synopsis
     "Collection cardinality matchers, extracted from rspec-expectations")
    (description
     "Collection cardinality matchers, extracted from rspec-expectations")
    (home-page "https://github.com/rspec/rspec-collection_matchers")
    (license license:expat)))

;;;;

(define %source-dir (dirname (current-filename)))
(define %git-dir (string-append %source-dir "/.git"))

; Bake a template by eval'ing the leaves
(define-public (bake tmpl)
 (list
  (car tmpl)
  (cons (caadr tmpl) (map
   (lambda (x) (list (car x) (eval (cadr x) (current-module))))
   (cdadr tmpl)))))

; double-escaped template of the sgx-endstream sexp
(define-public sgx-endstream-template
  '((package-input-rewriting `((,ruby-eventmachine . ,ruby-eventmachine-openssl)))
  (package
    (name "sgx-endstream")
    (version (read-line (open-pipe* OPEN_READ "git" "--git-dir" %git-dir "describe" "--always" "--dirty")))
    (source
     `(origin
       (method git-fetch)
       (uri (git-reference
             (recursive? #t)
             (url "https://git.singpolyma.net/sgx-endstream")
             (commit ,(read-line (open-pipe* OPEN_READ "git" "--git-dir" %git-dir "rev-parse" "HEAD")))))
       (file-name (git-file-name name version))
       (sha256
        (base32
         ,(read-line (open-pipe* OPEN_READ "guix" "hash" "-rx" %source-dir))))))
    (build-system 'copy-build-system)
    (arguments
     ''(#:install-plan '(("." "share/sgx-endstream"))
       #:phases
       (modify-phases %standard-phases
         (add-after 'install 'wrap
           (lambda* (#:key outputs #:allow-other-keys)
             (use-modules (ice-9 ftw))
             (let* ((out (assoc-ref outputs "out"))
                    (appdir (string-append out "/share/sgx-endstream/"))
                    (bindir (string-append out "/bin/")))
               (mkdir-p bindir)
               (let ((binstub (string-append bindir "sgx_endstream")))
                 (call-with-output-file binstub
                   (lambda (port)
                     (format port
                       "#!~a~%ENV['GEM_PATH'] = ['~a', ENV['GEM_PATH']].compact.join(':')~%Gem.clear_paths~%load '~a~a'~%"
                       (which "ruby")
                       (getenv "GEM_PATH")
                       appdir
                       "sgx_endstream.rb")))
                 (chmod binstub #o755)))
             #t)))))
    (propagated-inputs
      '(list
          ruby
          ruby-amazing-print
          ruby-blather
          ruby-dhall
          ruby-em-hiredis
          ruby-em-http-request
          ruby-em-promise-rb
          ruby-eventmachine
          ruby-ougai
          ruby-savon
          ruby-sentry))
    (synopsis
      "Endstream gateway component")
    (description
      "")
    (home-page
      "https://git.singpolyma.net/sgx-endstream")
    (license 'license:agpl3))))

; Baked version of sgx-endstream-template with leaves eval'd
(define-public sgx-endstream-baked
  (bake sgx-endstream-template))

; Build clean from git the version from a local clone
; To build whatever is sitting in local use:
; guix build --with-source=$PWD -f guix.scm
(eval sgx-endstream-baked (current-module))
