(use-modules (gnu packages)
	     (gnu packages base)
	     (gnu packages compression)
	     (gnu packages databases)
	     (gnu packages geo)
	     (gnu packages messaging)
	     (gnu packages perl)
	     (gnu packages perl-check)
	     (gnu packages pkg-config)
	     (gnu packages protobuf)
	     (gnu packages web)
	     (gnu packages xml)
	     (guix build-system gnu)
	     (guix build-system perl)
	     (guix download)
	     (guix git-download)
	     (guix licenses)
	     (guix packages))

(define vat
  (package
    (name "vat")
    (version "0.3")
    (source (origin
	      (method url-fetch)
	      (uri (string-append
		     "https://dev.tandem.ws/tandem/" name "/archive/v" version ".tar.gz"))
	      (sha256
		(base32
		  "0jbgakz7ip09hrnbbg1dm02n5zx7sv0magvw7s6g7rbbpy6wpqwh"))))
    (build-system gnu-build-system)
    (arguments
      `(#:tests? #f
	#:make-flags
	(list (string-append "datadir=" (assoc-ref %outputs "out") "/share")
	      (string-append "docdir="(assoc-ref %outputs "out") "/share/doc")
	      (string-append "pkglibdir="(assoc-ref %outputs "out") "/lib")
	      (string-append "bindir=" (assoc-ref %outputs "out") "/bin"))
	#:phases
	(modify-phases %standard-phases
		       (delete 'configure))))
    (inputs
      `(("postgresql" ,postgresql-15)))
    (home-page "https://dev.tandem.ws/tandem/vat/")
    (synopsis "VAT identification numbers for PostgreSQL")
    (description "VAT identification numbers library for PostgreSQL")
    (license (x11-style "https://www.postgresql.org/about/licence/"))))

(define pg-libphonenumber
  (let ((commit "753e2fa4be452620455a099aeda917648f2da70a")
	(revision "1"))
    (package
      (name "pg-libphonenumber")
      (version (git-version "0.1.0" revision commit))
      (source (origin
		(method git-fetch)
		(uri (git-reference
		       (url "https://github.com/blm768/pg-libphonenumber")
		       (commit commit)))
		(file-name (git-file-name name version))
		(sha256
		  (base32
		    "01syw93giq0pz80qzrdr79cr4p6w8lx129y1gsn2avc97r7pqanj"))))
      (build-system gnu-build-system)
      (arguments
	`(#:tests? #f
	  #:make-flags
	  (list (string-append "datadir=" (assoc-ref %outputs "out") "/share")
		(string-append "docdir="(assoc-ref %outputs "out") "/share/doc")
		(string-append "pkglibdir="(assoc-ref %outputs "out") "/lib")
		(string-append "bindir=" (assoc-ref %outputs "out") "/bin"))
	  #:phases
	  (modify-phases %standard-phases
			 (delete 'configure))))
      (inputs
	`(("postgresql" ,postgresql-15)
	  ("libphonenumber" ,libphonenumber)
	  ("protobuf" ,protobuf)))
      (home-page "https://github.com/blm768/pg-libphonenumber")
      (synopsis "PostgreSQL extension for libphonenumber")
      (description "pg_libphonenumber is a (partially implemented!) PostgreSQL extension that provides access to Google’s libphonenumber.")
      (license asl2.0))))

(define pguri
  (let ((commit "00241b96b8a285aa7ec0a81b5a4c0a664a044192")
	(revision "1"))
    (package
      (name "pguri")
      (version (git-version "1.20151224" revision commit))
      (source (origin
		(method git-fetch)
		(uri (git-reference
		       (url "https://github.com/petere/pguri")
		       (commit commit)))
		(file-name (git-file-name name version))
		(sha256
		  (base32
		    "0lz5nlqix60mxcjkqn3zn7q62xx0qbmybng3v0h049mf68l80ch9"))))
      (build-system gnu-build-system)
      (arguments
	`(#:tests? #f
	  #:make-flags
	  (list (string-append "datadir=" (assoc-ref %outputs "out") "/share")
		(string-append "docdir="(assoc-ref %outputs "out") "/share/doc")
		(string-append "pkglibdir="(assoc-ref %outputs "out") "/lib")
		(string-append "bindir=" (assoc-ref %outputs "out") "/bin"))
	  #:phases
	  (modify-phases %standard-phases
			 (delete 'configure))))
      (inputs
	`(("postgresql" ,postgresql-15)
	  ("pkg-config" ,pkg-config)
	  ("uriparser" ,uriparser)))
      (home-page "https://github.com/petere/pguri")
      (synopsis "uri type for PostgreSQL ")
      (description "This is an extension for PostgreSQL that provides a uri data type. Advantages over using plain text for storing URIs include: URI syntax checking, functions for extracting URI components, and human-friendly sorting.")
      (license asl2.0))))

(define postgresql-iban
  (let ((commit "0e533afb4d6bdb5af615d71ee16db9528e501ba6")
	(revision "1"))
    (package
      (name "PostgreSQL-IBAN")
      (version (git-version "1.0.0" revision commit))
      (source (origin
		(method git-fetch)
		(uri (git-reference
		       (url "https://github.com/yorickdewid/PostgreSQL-IBAN.git")
		       (commit commit)))
		(file-name (git-file-name name version))
		(sha256
		  (base32
		    "1fqjk0amdr3mvhq6n7ig6lxs8xckm6vh5nxm8m1rlar82081agh2"))
		(patches (search-patches "postgresql-iban-enable-nls.patch"))))
      (build-system gnu-build-system)
      (arguments
	`(#:tests? #f
	  #:make-flags
	  (list (string-append "datadir=" (assoc-ref %outputs "out") "/share")
		(string-append "docdir="(assoc-ref %outputs "out") "/share/doc")
		(string-append "pkglibdir="(assoc-ref %outputs "out") "/lib")
		(string-append "bindir=" (assoc-ref %outputs "out") "/bin"))
	  #:phases
	  (modify-phases %standard-phases
			 (delete 'configure))))
      (inputs
	`(("postgresql" ,postgresql-15)))
      (home-page "https://github.com/yorickdewid/PostgreSQL-IBAN")
      (synopsis "PostgreSQL extension that can verify International Bank Account Numbers")
      (description "PostgreSQL IBAN extension that can verify International Bank Account Numbers. This ensures that only valid bank account numbers are stored.")
      (license gpl3+))))

(define postgresql-15/xml
  (package
    (inherit postgresql-15)
    (arguments
      `(#:configure-flags '("--with-uuid=e2fs" "--with-openssl" "--with-libxml")
	#:phases
	(modify-phases %standard-phases
		       (add-before 'configure 'patch-/bin/sh
				   (lambda _
				     ;; Refer to the actual shell.
				     (substitute* '("src/bin/pg_ctl/pg_ctl.c"
						    "src/bin/psql/command.c")
						  (("/bin/sh") (which "sh")))
				     #t))
		       (add-after 'build 'build-contrib
				  (lambda _
				    (invoke "make" "-C" "contrib")))
		       (add-after 'install 'install-contrib
				  (lambda _
				    (invoke "make" "-C" "contrib" "install"))))))
    (inputs
      `(("libxml2" ,libxml2)
	,@(package-inputs postgresql-15)))))

(define perl-tap-parser-sourcehandler-pgtap
  (package
    (name "perl-tap-parser-sourcehandler-pgtap")
    (version "3.36")
    (source
      (origin
	(method url-fetch)
	(uri (string-append "mirror://cpan/authors/id/D/DW/DWHEELER/"
			    "TAP-Parser-SourceHandler-pgTAP-" version ".tar.gz"))
	(sha256
	  (base32 "0rwcx6z0xg1jrwnsyhb4a3aq3g7ff1a510g5v1paqgh65r9m3gh7"))))
    (build-system perl-build-system)
    (inputs
      `(("perl-module-build" ,perl-module-build)
	("perl-test-pod" ,perl-test-pod)
	("perl-test-pod-coverage" ,perl-test-pod-coverage)))
    (home-page "https://metacpan.org/pod/TAP::Parser::SourceHandler::pgTAP")
    (synopsis "Stream TAP from pgTAP test scripts")
    (description "This source handler executes pgTAP tests. It does two things: 1) Looks at the TAP::Parser::Source passed to it to determine whether or not the source in question is in fact a pgTAP test (\"can_handle\"). And, 2) Creates an iterator that will call psql to run the pgTAP tests (\"make_iterator\").  Unless you're writing a plugin or subclassing TAP::Parser, you probably won't need to use this module directly.")
    (license perl-license)))

(define pgtap
  (package
    (name "pgtap")
    (version "1.2.0")
    (source (origin
	      (method url-fetch)
	      (uri (string-append
		     "https://api.pgxn.org/dist/pgtap/" version
		     "/pgtap-" version ".zip"))
	      (sha256
		(base32
		  "106p24wslq39h9hrscf415x7s1nb6l21xjdzpg3dh73gawslfmqv"))))
    (build-system gnu-build-system)
    (arguments
      `(#:tests? #f
	#:make-flags
	(list (string-append "datadir=" (assoc-ref %outputs "out") "/share")
	      (string-append "docdir="(assoc-ref %outputs "out") "/share/doc")
	      (string-append "pkglibdir="(assoc-ref %outputs "out") "/lib")
	      (string-append "bindir=" (assoc-ref %outputs "out") "/bin"))
	#:phases
	(modify-phases %standard-phases
		       (delete 'configure))))
    (inputs
      `(("postgresql" ,postgresql)))
    (native-inputs
      `(("perl" ,perl)
	("which" ,which)
	("unzip" ,unzip)))
    (home-page "https://pgtap.org")
    (synopsis "Unit testining for PostgreSQL")
    (description "pgTAP is a suite of database functions that make it easy to write TAP-emitting unit tests in psql scripts or xUnit-style test functions.")
    (license (x11-style "https://www.postgresql.org/about/licence/"))))

(package
  (name "camper")
  (version "0.1.0")
  (source (origin
	    (method git-fetch)
	    (uri (git-reference
		   (url "https://dev.tandem.ws/tandem/camper.git")
		   (commit "666935b54c8f2d735d8fe5591407ca231bba7e4c")))
	    (sha256
	      (base32
		"0p31j7qing7nhnpngsnnfvx6wz70ryj4q5k7l81anh2z19nzwbk8"))))
  (build-system gnu-build-system)
  (inputs
    `(("sqitch" ,sqitch)
      ("pgtap" ,pgtap)
      ("vat" ,vat)
      ("perl-tap-parser-sourcehandler-pgtap" ,perl-tap-parser-sourcehandler-pgtap)
      ("perl" ,perl)
      ("pg-libphonenumber" ,pg-libphonenumber)
      ("pguri" ,pguri)
      ("PostgreSQL-IBAN" ,postgresql-iban)
      ("postgresql" ,postgresql-15/xml)))
  (synopsis "Simple camping management and booking web application")
  (description "A simple web application to manage small campings and other touristic accomodations in Spain.")
  (home-page "https://dev.tandem.ws/tandem/camper")
  (license agpl3+))
