#! /usr/bin/guile3.0 \
--no-auto-compile -e main -s
!#

;; SPDX-FileCopyrightText: 2016 Quentin "Sardem FF7" Glidic
;; SPDX-FileCopyrightText: 2018-2023 Fredrik "PlaTFooT" Salomonsson <plattfot@posteo.net>
;;
;; SPDX-License-Identifier: GPL-3.0-or-later

(eval-when (load expand eval)
  (set! %load-path
    (cons (format #f "~a/../share/guile/site/3.0" (dirname (current-filename))) %load-path))
  (set! %load-compiled-path
    (cons (format #f "~a/../lib64/guile/3.0/site-ccache" (dirname (current-filename))) %load-compiled-path)))

(use-modules (ice-9 getopt-long)
             (ice-9 format)
             (pinentry-rofi))
(define (main args)
  (let* ((option-spec
          '((display (single-char #\D) (value #t))
            (xauthority (single-char #\a) (value #t))
            (version (single-char #\v) (value #f))
            (debug (single-char #\d) (value #f))
            (ttyname (single-char #\T) (value #t))
            (ttytype (single-char #\N) (value #t))
            (lc-ctype (single-char #\C) (value #t))
            (lc-message (single-char #\M) (value #t))
            (timeout (single-char #\o) (value #t))
            (no-global-grab (single-char #\g) (value #f))
            (parent-wid (single-char #\W) (value #f))
            (colors (single-char #\c) (value #f))
            (ttyalert (single-char #\a) (value #f))
            (log (value #t))
            (help (single-char #\h) (value #f))))
         (default-display ":0")
         (default-lc-ctype "C")
         (default-lc-message "C")
         (options (getopt-long (command-line) option-spec))
         (pinentry (make-pinentry #t "Passphrase:" "Ok" "Cancel"
                                  (option-ref options 'display default-display)
                                  (let ((logfile (option-ref options 'log #f)))
                                    (when logfile
                                      (open-output-file
                                       (format #f "~a.~a" logfile (getpid)))))
                                  (option-ref options 'lc-ctype default-lc-ctype)
                                  (option-ref options 'lc-message default-lc-message)
                                  (option-ref options '()
                                              (if (option-ref options 'no-global-grab #f)
                                                    '("-no-lazy-grab")
                                                    '())))))
    (when (option-ref options 'help #f)
      (format #t "\
Usage: ~a [OPTIONS]...
Options:
  -D, --display DISPLAY      Set display, default is ~s
      --log LOGFILE          Log unknown commands to LOGFILE
  -v, --version              Display version.
  -h, --help                 Display this help.
  -C, --lc-ctype STRING      Set the tty LC_CTYPE value
  -M, --lc-messages STRING   Set the tty LC_MESSAGES value
  -g, --no-global-grab       Grab keyboard only while window is focused
  -- [ROFI-OPTIONS]...  Options after -- will be sent to rofi
Dummy options (does not do anything just for compatibility):
  -d, --debug                Turn on debugging output
  -T, --ttyname FILE         Set the tty terminal node name
  -N, --ttytype NAME         Set the tty terminal type
  -o, --timeout SECS         Timeout waiting for input after this many seconds
  -W, --parent-wid           Parent window ID (for positioning)
  -c, --colors STRING        Set custom colors for ncurses
  -a, --ttyalert STRING      Set the alert mode (none, beep or flash)
Author:
~a
"
              (car (command-line))
              default-display
              "Fredrik Salomonsson")
      (exit #t))
    (let ((unimplemented (lambda (option)
                           (when (option-ref options option #f)
                             (format (current-error-port) "warning: --~a is not implemented~%"
                                     (symbol->string option))))))
      (unimplemented 'debug)
      (unimplemented 'ttyname)
      (unimplemented 'ttytype)
      (unimplemented 'timeout)
      (unimplemented 'parent-wid)
      (unimplemented 'colors)
      (unimplemented 'ttyalert))
    (when (option-ref options 'version #f)
      (format #t "~a~%" "3.0.0")
      (exit #t))
    (format #t "OK Please go ahead\n")
    (force-output)
    (pinentry-loop pinentry (current-input-port))))
