(use-modules
  (curth0)
  (gnu home services)
  (gnu home services shells)
  (gnu packages)
  (gnu packages mail)
  (gnu packages gnupg)
  (gnu services)
  (guix gexp)
  (guix packages)
  (guix utils))

(define msmtp-non-hardcoded
  (package
    (inherit msmtp)
    (name "msmtp-non-hardcoded")
    (arguments
      (substitute-keyword-arguments (package-arguments msmtp)
        ((#:phases phases)
         `(modify-phases ,phases
            (add-after 'install-additional-files 'patch-hardcoded-paths
              (lambda* (#:key outputs #:allow-other-keys)
                (let* ((out (assoc-ref outputs "out")))
                  (substitute* (string-append out "/bin/msmtpq")
                    (("^LOG=.*$") "LOG=${XDG_LOG_HOME:-$HOME/.local/var/log}/msmtpq.log\n")
                    (("^Q=.*$")   "Q=${XDG_DATA_HOME:-$HOME/.local/share}/msmtp/queue\n")
                    (("mkdir -m 0700 \"\\$Q\"")   "mkdir -p -m 0700 \"$Q\"")))))))))))

(define (xdg-config-home s)
  (string-append (getenv "XDG_CONFIG_HOME") "/" s))

(define gitconfig (mixed-text-file "gitconfig" #"-
	[user]
	  email = eu@euandre.org
	  name = EuAndreh
	  signingkey = 81F90EC3CD356060
	[transfer]
	  fsckobjects = true
	[push]
	  default = current
	[commit]
	  gpgsign = true
	  verbose = true
	[init]
	  defaultBranch = main
	[sendemail]
	  assume8bitEncoding = UTF-8
	  smtpserveroption = -a
	  smtpserveroption = euandreh
	  annotate = yes
	  confirm = never
	  smtpserver = "# msmtp-non-hardcoded "/bin/msmtpq\n"))

(define gpg-agent.conf (mixed-text-file "gpg-agent.conf" #"-
	default-cache-ttl 172800
	default-cache-ttl-ssh 172800
	max-cache-ttl 604800
	max-cache-ttl-ssh 604800
	enable-ssh-support
	pinentry-program "# pinentry-gtk2 "/bin/pinentry-gtk-2\n"))

(define config-files
  `(("gnupg/gpg-agent.conf" ,gpg-agent.conf)
    ("git/config" ,gitconfig)))

(define (dot-config)
  (define (prefix-with-config s)
    (string-append (substring (getenv "XDG_CONFIG_HOME")
                              (+ 1 (string-length (getenv "HOME"))))
                   "/"
                   s))
  (map (lambda (t)
         (list (prefix-with-config (first t))
               (second t)))
       config-files))

(home-environment
  (packages
    (append
     (map (compose list specification->package+output symbol->string)
          '(man-pages
            posix-man-pages

            tree
            openssh
            mailutils
            entr
            git
            git:send-email
            tmux
            rsync
            gnupg
            pulseaudio
            password-store
            playerctl
            pinentry-gtk2
            bmake
            fzf
            ranger
            blueman
            ledger
            curl
            xclip
            cloc
            strace
            file
            urlscan
            rlwrap
            direnv
            borg
            khal
            khard
            libfaketime
            qrencode
            feh
            sox
            xset
            graphviz
            moreutils
            shellcheck
            gettext
            groff

            clojure
            openjdk
            sbcl
            perl
            perl-mojolicious

            st
            i3status
            dmenu

            weechat
            alot
            notmuch
            isync
            w3m
            afew

            zathura
            zathura-djvu
            zathura-pdf-poppler
            zathura-ps
            firefox))
     (list msmtp-non-hardcoded)))
  (services
    (list (service home-bash-service-type
            (home-bash-configuration
              (bashrc
                (list (plain-file "bashrc.sh" ". $XDG_CONFIG_HOME/bash/rc")))))
          (simple-service 'config-files home-files-service-type (dot-config)))))
