(define-module (px services package-management)
  #:use-module (gnu)
  #:use-module (gnu packages bash)
  #:use-module (gnu services shepherd)
  #:use-module (guix gexp)
  #:use-module (guix records)
  #:use-module (ice-9 match)
  #:use-module (px packages package-management)
  #:export (px-unattended-upgrades-configuration
            px-unattended-upgrades-configuration?
            px-unattended-upgrades-service-type))

;;
;; px-unattended-upgrades-service-type
;;

(define-record-type* <px-unattended-upgrades-configuration>
                     px-unattended-upgrades-configuration
                     make-px-unattended-upgrades-configuration
  px-unattended-upgrades-configuration?
  (package
    px-unattended-upgrades-configuration-package
    (default px-unattended-upgrades))
  (timeout px-unattended-upgrades-timeout
           (default 300)))

(define (px-unattended-upgrades-configuration->script config)
  (match config
    (($ <px-unattended-upgrades-configuration> package timeout)
     (computed-file "px-unattended-upgrades.sh"
                    #~(begin
                        (call-with-output-file #$output
                          (lambda (port)
                            (display
                             "# AUTO GENERATED BY: px-unattended-upgrades-shepherd-service

"
                             port)
                            (display "echo \"$(date) > service started: \"\n"
                                     port)
                            (format port "echo \"$(date) > sleep for: ~a\"\n"
                                    #$timeout)
                            (format port "sleep ~a\n"
                                    #$timeout)
                            (display "echo \"$(date) > start upgrade\"\n" port)
                            (format port "~a/bin/px-unattended-upgrades\n"
                                    #$package))))))))

(define (px-unattended-upgrades-shepherd-service config)
  (match config
    (($ <px-unattended-upgrades-configuration> package timeout)
     (let ((script (px-unattended-upgrades-configuration->script config)))
       (list (shepherd-service (provision '(px-unattended-upgrades))
                               (documentation
                                "Run px-unattended-upgrades once after reboot")
                               (requirement '(networking user-processes))
                               (one-shot? #t)
                               (start #~(make-forkexec-constructor (list (string-append #$bash
                                                                          "/bin/bash")
                                                                         #$script)
                                         #:log-file
                                         "/var/log/px-unattended-upgrades.log"))
                               (stop #~(make-kill-destructor))))))))

(define px-unattended-upgrades-service-type
  (service-type (name 'px-unattended-upgrades)
                (description "PantherX unattended upgrades service")
                (extensions (list (service-extension
                                   shepherd-root-service-type
                                   px-unattended-upgrades-shepherd-service)))
                (default-value (px-unattended-upgrades-configuration))))
