(use-modules
  ((ice-9 textual-ports) #:prefix textual-ports:)
  ((guix licenses) #:prefix licenses:)
  ((xyz euandreh heredoc) #:prefix heredoc:)
  (gnu home)
  (gnu home services)
  (gnu home services mcron)
  (gnu home services shells)
  (gnu home services shepherd)
  (gnu home services xdg)
  (gnu packages)
  (gnu packages base)
  (gnu packages dunst)
  (gnu packages freedesktop)
  (gnu packages gnupg)
  (gnu packages gpodder)
  (gnu packages libreoffice)
  (gnu packages mail)
  (gnu packages music)
  (gnu packages wget)
  (gnu packages lisp)
  (gnu packages texinfo)
  (gnu packages tmux)
  (gnu packages version-control)
  (gnu packages video)
  (gnu packages xdisorg)
  (gnu services)
  (guix build-system trivial)
  (guix download)
  (guix gexp)
  (guix git-download)
  (guix modules)
  (guix packages)
  (guix utils)
  (xyz euandreh queue))
(heredoc:enable-syntax)

(define-public hunspell-iconv
  (package
    (inherit hunspell)
    (name "hunspell-iconv")
    (inputs
      `(("libiconv" ,libiconv)
        ,@(package-inputs hunspell)))))

(define (hunspell-dictionary-utf8 dict-name)
  (package
    (name (string-append "hunspell-dict-" dict-name "-utf8"))
    (version "630b34e6f8f3cbe7aa7b27b6d8ab118e27252fd1")
    (source
      (origin
        (method git-fetch)
        (uri
          (git-reference
            (url "https://github.com/wooorm/dictionaries")
            (commit version)))
        (file-name
          (git-file-name "hunspell-dictionary-utf8" version))
        (sha256
          (base32 "1iknwzh5h04m067ddw9hlzc1qqj4mr9mdkcfapsnay304laaiyn5"))))
    (build-system trivial-build-system)
    (arguments
      `(#:modules ((guix build utils))
        #:builder
        (begin
          (use-modules (guix build utils))
          (let ((source-prefix (string-append (assoc-ref %build-inputs "source")
                                              "/dictionaries/"
                                              ,dict-name
                                              "/index"))
                (install-prefix (string-append %output "/share/hunspell/")))
            (mkdir-p install-prefix)
            (copy-file (string-append source-prefix ".aff")
                       (string-append install-prefix ,dict-name ".aff"))
            (copy-file (string-append source-prefix ".dic")
                       (string-append install-prefix ,dict-name ".dic"))))))
    (synopsis    (string-append "Hunspell " dict-name " dictionary in UTF-8"))
    (description (string-append "Hunspell " dict-name " dictionary in UTF-8"))
    (license licenses:expat)
    (home-page "https://github.com/wooorm/dictionaries")))

(define cmucl
  (package
    (name "cmucl-binary")
    (version "21b")
    (source
      (origin
        (method url-fetch)
        (uri (string-append "https://common-lisp.net/project/cmucl/downloads/release/"
                            version
                            "/cmucl-"
                            version
                            "-x86-linux.tar.bz2"))
        (sha256
          (base32 "13k3b5ygnbsq6n2i3r5i4ljw3r1qlskn2p5f4x9hrx6vfvbb3k7a"))))
    (build-system trivial-build-system)
    (arguments
      `(#:modules ((guix build utils))
        #:builder
        (begin
          (use-modules (guix build utils))
          (let ((source (assoc-ref %build-inputs "source"))
                (bin (string-append %output "/bin")))
            (mkdir-p bin)
            (copy-file (string-append source "/bin/lisp")
                       (string-append bin "/lisp"))))))
    (home-page "https://www.cons.org/cmucl/")
    (synopsis "The CMU implementation of Common Lisp")
    (description #"-
      CMUCL is a free implementation of the Common Lisp programming language
      which runs on most major Unix platforms.  It mainly conforms to the
      ANSI Common Lisp standard."#)
    (license licenses:public-domain)))

(define (slurp name)
  (string-trim-both
   (call-with-input-file
    name
    textual-ports:get-string-all)))

(define (script name content)
  (package
    (name name)
    (version "latest")
    (source #f)
    (build-system trivial-build-system)
    (arguments
      `(#:modules ((guix build utils))
        #:builder
        (begin
          (use-modules (guix build utils))
          (let* ((bin (string-append %output "/bin"))
                 (prog (string-append bin "/" ,name)))
            (mkdir-p bin)
            (call-with-output-file prog
              (lambda (port)
                (format port "~a" ,content)))
            (chmod prog #o755)))))
    (home-page "")
    (synopsis "")
    (description "")
    (license #f)))

(define cronjobs
  (list
   #~(job "0  0   * * *" "cronjob check")
   #~(job "30 4   * * *" "cronjob x update AND upgrade")
   #~(job "30 23  * * *" "cronjob backup -q cron")))

(define (home-service name package bin)
  (service-type
    (name name)
    (extensions
      (list
       (service-extension home-shepherd-service-type
         (lambda _
           (list
            (shepherd-service
              (provision (list name))
              (documentation
                (format #f "Shepherd service that manages ~a." name))
              (start
                #~(make-forkexec-constructor
                   (list #$(file-append package bin))))
              (stop #~(make-kill-destructor))))))
       (service-extension home-profile-service-type
         (lambda _ (list package)))))
    (default-value '())
    (description
      (format #f
       #"-
         Service that runs ~a as a daemon under Shepherd.

         It has no configuration."#
       name))))

(define xdg-prefix "$HOME/.usr")
(define (xdg path)
  (string-append xdg-prefix "/" path))

(home-environment
  (packages
    (append
     (map (compose list specification->package+output symbol->string)
          '(nss-certs
            bash
            coreutils
            findutils
            diffutils
            grep
            sed
            tar
            gawk
            bc
            nvi
            patchelf

            man-pages
            man-pages-posix

            bash-completion

            git
            git:send-email
            git:gui
            git-open
            git-remote-gcrypt
            git-lfs
            mercurial
            fossil
            darcs
            subversion
            cvs
            rcs
            cssc
            quilt

            wget
            tmux
            texinfo
            gnupg
            rsync
            tree
            diffoscope
            mailutils
            entr
            pulseaudio
            password-store
            playerctl
            pinentry-gtk2
            bmake
            make
            tup
            autoconf
            automake
            libtool
            pcre:out
            pcre:bin
            pcre:doc
            pcre:static
            pcre2
            avahi
            libgcrypt
            qbe
            cproc
            doxygen
            gperf
            readline
            gmp
            help2man
            libtomcrypt
            libtommath
            lz4
            lokke
            meson
            ninja
            sparse
            ant
            mpc
            maven
            pkg-config
            fzf
            ranger
            blueman
            pavucontrol
            ledger
            bind:utils
            stunnel
            netcat
            siege
            curl
            curl:doc
            xclip
            cloc
            strace
            file@5.39
            urlscan
            rlwrap
            direnv
            borg
            khal
            khard
            libfaketime
            qrencode
            feh
            sox
            xset
            graphviz
            moreutils
            shellcheck
            gettext
            lilypond
            groff
            groff:doc
            grap
            ghostscript
            texlive
            jq
            recutils
            units
            ncurses
            trash-cli
            lsof
            autojump
            unzip
            powertop
            md4c
            timidity++
            cmark
            cmake
            makefile2graph
            po4a
            mdpo
            universal-ctags
            gron
            reptyr
            xpdf
            perf-tools
            scdoc
            rpm
            dpkg
            cpio
            pv

            ;; for compiling ECL
            libatomic-ops
            libgc
            libffi
            ;; for compiling CLISP
            libffcall
            libsigsegv

            cryptsetup
            btrfs-progs
            sysstat
            rrdtool

            flatpak
            xdg-desktop-portal

            sqlite
            clojure
            clojure-tools
            leiningen
            openjdk
            perl
            perl-dbi
            perl-dbd-sqlite
            perl-critic
            perl-json
            perl-mojolicious
            perl-regexp-grammars
            perl-commonmark
            perl-aliased
            perl-uri-escape
            ruby
            python
            python-sphinx
            python-slixmpp
            python-unidecode
            python-yubikey-manager
            python-coverage
            python-pytest
            python-requests
            python-beautifulsoup4
            python-docx
            python-telegram-bot
            ; python-docutils ; broken: conflicts with python-sphinx
            python-mkdocs
            valgrind
            flex
            bison
            gcc-toolchain
            clang
            tcc
            fuse
            ; node ; broken: conflicts with archivebox
            quickjs
            m4
            go
            xrandr
            arandr
            openssl
            fswatch
            ;; rust         ; broken
            ;; rust:cargo   ; broken
            ;; rust:rustfmt ; broken
            vala
            tcl

            sbcl
            gcl
            ecl
            clisp
            ccl
            abcl
            janet
            kawa
            chez-scheme
            racket
            chibi-scheme
            ; chicken ; broken: conflicts with gcc-toolchain
            gambit-c
            gauche

            dash
            fish
            rc
            es
            tcsh
            zsh
            oksh
            loksh
            mksh
            oil
            gash
            nushell

            gtk
            gtk:bin
            gtk:doc
            glade
            ; libglade ; broken: conflicts with zathura
            cambalache
            tk
            qtbase
            qtbase:debug
            qtdeclarative

            st
            i3status
            scrot
            xmessage
            dmenu
            httpd ;; for htpasswd

            weechat
            qtox
            telescope
            imagemagick
            ffmpeg
            pandoc
            mktorrent
            jekyll
            flac
            mediainfo
            libnotify
            espeak-ng
            procps
            htop
            btop
            zenity
            util-linux
            lightning
            lmdb
            guile
            guile-heredoc-latest
            gzip
            xz
            bzip2
            lzip
            lzop
            which
            libxml2
            psmisc
            less
            nano
            patch
            youtube-dl
            tmux-plugin-resurrect
            tmux-plugin-continuum

            ;; ArchiveBox and some of its optional dependencies
            archivebox
            ripgrep

            poezio
            freetalk
            mcabber
            profanity
            newsboat
            vlc
            hicolor-icon-theme

            keepassxc

            xbacklight

            gpodder
            mate-calc
            gnote
            telegram-desktop
            zathura
            zathura-djvu
            zathura-pdf-poppler
            zathura-ps
            dino
            poedit
            transmission
            transmission:gui
            audacity
            inkscape
            libreoffice
            quodlibet
            ungoogled-chromium
            icedove
            firefox))
     (list ;; cmucl
           hunspell-iconv
           (hunspell-dictionary-utf8 "en")
           (hunspell-dictionary-utf8 "pt")
           (hunspell-dictionary-utf8 "fr")
           (hunspell-dictionary-utf8 "eo")
           (script "cronjob" (slurp (string-append (getenv "XDG_CONFIG_HOME")
                                                   "/sh/cronjob.sh"))))))
  (services
    (list
     (service (home-service 'clipmenu    clipmenu    "/bin/clipmenud"))
     (service (home-service 'dunst       dunst       "/bin/dunst"))
     (service (home-service 'poweralertd poweralertd "/bin/poweralertd"))
     (service home-xdg-base-directories-service-type
       (home-xdg-base-directories-configuration
         (cache-home  (xdg "var/cache"))
         (config-home (xdg "etc"))
         (data-home   (xdg "share"))
         (log-home    (xdg "var/log"))
         (state-home  (xdg "var/state"))))
     (simple-service 'my-shell-profile home-shell-profile-service-type
       (list (plain-file
              "my-profile"
              (format #f
               #"-
                 export XDG_PREFIX="~a"
                 . "$XDG_CONFIG_HOME"/sh/rc"#
               xdg-prefix))))
     (service home-mcron-service-type
       (home-mcron-configuration
         (jobs cronjobs))))))
