#!/run/current-system/profile/bin/guile \
--no-auto-compile -e (dotfiles-update-commit) -s
!#

;;;; guix-latest --- Build Guix system with latest channels.
;;;; Copyright © 2024, 2025 Oleg Pykhalov <go.wigust@gmail.com>
;;;; Released under the GNU GPLv3 or any later version.

(define-module (dotfiles-update-commit)
  #:use-module (gnu system)
  #:use-module (guix channels)
  #:use-module (guix ci)
  #:use-module (guix inferior)
  #:use-module (guix profiles)
  #:use-module (guix records)
  #:use-module (guix scripts pull)
  #:use-module (guix store)
  #:use-module (guix ui)
  #:use-module (ice-9 format)
  #:use-module (ice-9 match)
  #:use-module (ice-9 popen)
  #:use-module (ice-9 pretty-print)
  #:use-module (ice-9 rdelim)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-37)
  #:autoload   (guix openpgp) (openpgp-format-fingerprint)
  #:export (main))

(define (main args)
  (let* ((port (open-pipe* OPEN_READ "git" "rev-parse" "HEAD"))
         (output (read-string port))
         (commit (string-take (string-trim-right output #\newline) 8)))
    (close-port port)
    (for-each (lambda (file)
                (system* "yq" "--inplace"
                         (string-append ".spec.template.metadata.annotations[\"guix-default-channel-commit\"] = \""
                                        commit "\"")
                         file)
                (system* "git" "add" file)
                (system* "git" "commit"
                         (format #f "--message=apps: cluster1: guix: ~a: Update to ~a."
                                 (basename (dirname (canonicalize-path file)))
                                 commit)))
              (if (> (length args) 1)
                  (cdr args)
                  (reverse (append (apply append
                                          (fold (lambda (hostname hostnames)
                                                  (cons (list (string-append "/home/oleg/.local/share/chezmoi/apps/cluster1/maintenance-guix-pull-" hostname "/job.yaml")
                                                              (string-append "/home/oleg/.local/share/chezmoi/apps/cluster1/maintenance-guix-system-reconfigure-" hostname "/job.yaml")
                                                              (string-append "/home/oleg/.local/share/chezmoi/apps/cluster1/maintenance-guix-package-manifest-" hostname "/job.yaml")
                                                              (string-append "/home/oleg/.local/share/chezmoi/apps/cluster1/maintenance-guix-home-reconfigure-" hostname "/job.yaml")
                                                              (string-append "/home/oleg/.local/share/chezmoi/apps/cluster1/maintenance-nix-home-reconfigure-" hostname "/job.yaml"))
                                                        hostnames))
                                                '()
                                                '("kube1" "kube2" "kube3")))
                                   '("/home/oleg/.local/share/chezmoi/apps/cluster1/maintenance-git-dotfiles-kube2/job.yaml")
                                   '("/home/oleg/.local/share/chezmoi/apps/cluster1/maintenance-git-dotfiles-kube3/job.yaml"
                                     "/home/oleg/.local/share/chezmoi/apps/cluster1/maintenance-guix-copy-kube3/job.yaml")
                                   '("/home/oleg/.local/share/chezmoi/apps/cluster1/maintenance-guix-image-workstation/job.yaml"
                                     "/home/oleg/.local/share/chezmoi/apps/cluster1/maintenance-guix-deploy-vm-guix-datavolume/job.yaml"
                                     "/home/oleg/.local/share/chezmoi/apps/cluster1/maintenance-guix-rde-image-workstation/job.yaml")
                                   '("/home/oleg/.local/share/chezmoi/apps/cluster1/build-workstation-controller-kube1/job.yaml")

                                   (apply append
                                          (fold (lambda (hostname hostnames)
                                                  (cons (list (string-append "/home/oleg/.local/share/chezmoi/apps/cluster2/maintenance-guix-pull-" hostname "/job.yaml")
                                                              (string-append "/home/oleg/.local/share/chezmoi/apps/cluster2/maintenance-guix-system-reconfigure-" hostname "/job.yaml")
                                                              (string-append "/home/oleg/.local/share/chezmoi/apps/cluster2/maintenance-guix-package-manifest-" hostname "/job.yaml")
                                                              (string-append "/home/oleg/.local/share/chezmoi/apps/cluster2/maintenance-guix-home-reconfigure-" hostname "/job.yaml")
                                                              (string-append "/home/oleg/.local/share/chezmoi/apps/cluster2/maintenance-nix-home-reconfigure-" hostname "/job.yaml"))
                                                        hostnames))
                                                '()
                                                '("kube5001")))
                                   '("/home/oleg/.local/share/chezmoi/apps/cluster2/maintenance-git-dotfiles-kube5001/job.yaml")))))))
