#!/run/current-system/profile/bin/guile \
--no-auto-compile -e (jenkins-nix-version) -s
!#

;;;; nixos --- SYNOPSIS
;;;; Copyright © 2020, 2022 Oleg Pykhalov <go.wigust@gmail.com>
;;;; Released under the GNU GPLv3 or any later version.

(define-module (jenkins-nix-version)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-37)
  #:use-module (ice-9 popen)
  #:use-module (ice-9 rdelim)
  #:use-module (ice-9 regex)
  #:export (main))

;;; Commentary:
;;;
;;; DESCRIPTION
;;;
;;; Code:

(define %options
  (let ((display-and-exit-proc (lambda (msg)
                                 (lambda (opt name arg loads)
                                   (display msg) (quit)))))
    (list (option '(#\c "commit") #f #t
                  (lambda (opt name arg loads)
                    (alist-cons 'commit arg loads)))
          (option '(#\o "no-overlays") #f #f
                (lambda (opt name arg result . rest)
                  (apply values
                         (alist-cons 'overlay? #f
                                     (alist-delete 'overlay? result eq?))
                         rest)))
          (option '(#\v "version") #f #f
                  (display-and-exit-proc "nixos version 0.0.1\n"))
          (option '(#\h "help") #f #f
                  (display-and-exit-proc
                   "Usage: nixos ...")))))

(define %default-options
  '((overlay? . #t)))

(define (system->string prog args)
  (let* ((port (apply open-pipe* OPEN_READ prog args))
         (output (read-string port)))
    (close-pipe port)
    output))

(define (main args)
  (define opts
    (args-fold (cdr (program-arguments))
               %options
               (lambda (opt name arg loads)
                 (error "Unrecognized option `~A'" name))
               (lambda (op loads)
                 (cons op loads))
               %default-options))

  (define nix-commit
    (or (assoc-ref opts 'commit)
        (last (string-split (match:substring
                             (regexp-exec (make-regexp "([[:digit:]]*\\.[[:digit:]]*\\.[[:digit:]]*\\.[[:alnum:]]*)")
                                          (system->string "ssh"
                                                          '("jenkins.intr"
                                                            "--"
                                                            "nix-instantiate --eval -E '(import <nixpkgs> {}).lib.version'"))))
                            #\.))))

  (define nix-expression
    (format #f "\
with (import (builtins.fetchTarball {
  url = \"https://github.com/nixos/nixpkgs/archive/~a.tar.gz\";
}) {
  overlays = [
    ~a
  ];
}); ~a" nix-commit
        (if (assoc-ref opts 'overlay?) "(import /home/oleg/src/gitlab.intr/_ci/nixpkgs)" "")
        (car opts)))

  (define nix-command
    (list "nix-build"
          "--no-out-link"

          "--option" "trusted-public-keys"
          (format #f "~s"
                  (string-join (list "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
                                     "cache.nixos.intr:6VD7bofl5zZFTEwsIDsUypprsgl7r9I+7OGY4WsubFA=")))

          "--option" "builders" (format #f "~s" "ssh://nixos.intr x86_64-linux")

          "--arg" "debug" "true"

          "--substituters"
          (format #f "~s"
                  (string-join (list "https://cache.nixos.org/"
                                     "https://cache.nixos.intr/")))

          "--expr"
          (format #f "~s"
                  (string-join
                   (map string-trim
                        (string-split nix-expression
                                      #\newline))))))

  (with-output-to-port (current-error-port)
    (lambda ()
      (format #t "Invoking:~%~a~%" (string-join nix-command))))

  (system (string-join nix-command)))

;;; nixos ends here
