(use-modules ((guix licenses) #:prefix license:)
              (guix build-system python)
               (guix download)
               (guix packages)
			   (guix gexp)
               (gnu packages aidc)
               (gnu packages base)
               (gnu packages crates-io)
               (gnu packages compression)
               (gnu packages hardware)
               (gnu packages image)
               (gnu packages maths)
               (gnu packages pkg-config)
               (gnu packages python)
               (gnu packages python-xyz)
               (gnu packages python-web)
               (gnu packages python-crypto)
               (gnu packages curl)
               (gnu packages security-token)
               (gnu packages serialization)
               (gnu packages tls)
               (gnu packages xorg)
               (gnu packages web)
               (px packages common)
               (px packages python-xyz)
               (px packages tpm)
               (px packages library))

(define-public px-device-identity
  (package
   (name "px-device-identity")
   (version "0.10.8")
   (source (local-file (canonicalize-path ".")
                        #:recursive? #t))
   (build-system python-build-system)
   (arguments
    `(#:tests? #f
      #:phases
      (modify-phases %standard-phases
         (add-after 'install 'wrap-for-openssl-tss2-conf
           (lambda* (#:key outputs #:allow-other-keys)
             (let ((out              (assoc-ref outputs "out"))
                   (openssl          (assoc-ref %build-inputs "openssl"))
                   (tpm2-tss         (assoc-ref %build-inputs "tpm2-tss"))
                   (tpm2-tss-engine  (assoc-ref %build-inputs "tpm2-tss-engine")))
                        (wrap-program (string-append out "/bin/px-device-identity") 
                          `("OPENSSL_CONF" ":" prefix (,(string-append tpm2-tss-engine "/etc/openssl-tss2.conf"))))
                        (wrap-program (string-append out "/bin/px-device-identity") 
                          `("PATH" ":" prefix (,(string-append tpm2-tss-engine "/bin/"))))
                        (wrap-program (string-append out "/bin/px-device-identity") 
                          `("PATH" ":" prefix (,(string-append openssl "/bin/"))))
                        (wrap-program (string-append out "/bin/px-device-identity") 
                          `("TPM2TSSENGINE_TCTI" ":" prefix (,(string-append tpm2-tss "/lib/libtss2-tcti-device.so:/dev/tpm0"))))
                        (wrap-program (string-append out "/bin/px-device-identity") 
                          `("TPM2TOOLS_TCTI" ":" prefix (,(string-append tpm2-tss "/lib/libtss2-tcti-device.so:/dev/tpm0"))))
			#t))))))
   (inputs
    `(("openssl" ,openssl)
      ("python-idna" ,python-idna)
      ("python-requests" ,python-requests)
      ("python-falcon" ,python-falcon)
      ("python-authlib-0.14.3" ,python-authlib-0.14.3)
      ("python-pycryptodomex" ,python-pycryptodomex)
      ("python-jose" ,python-jose)
      ("python-pyyaml" ,python-pyyaml)
      ("python-shortuuid-v1.0.1" ,python-shortuuid-v1.0.1)
      ("python-appdirs" ,python-appdirs)
      ("python-psutil" ,python-psutil)
      ("tpm2-tss" ,tpm2-tss)
      ("tpm2-tss-engine" ,tpm2-tss-engine)))
   (native-inputs
    `(("python-setuptools" ,python-setuptools)
      ("pkg-config" ,pkg-config)
      ("python-requests" ,python-requests)))
   (home-page "https://www.pantherx.org/")
   (synopsis "PantherX Device Identity Manager")
   (description "Initiates device identity based on RSA or ECC key pair and optionally registers with Central Management.")
   (license license:expat)))

px-device-identity