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

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

(define-module (firefox)
  #:use-module (json)
  #:use-module (srfi srfi-37)
  #:export (main))

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

(define %options
  (let ((display-and-exit-proc (lambda (msg)
                                 (lambda (opt name arg loads)
                                   (display msg) (quit)))))
    (list (option '(#\v "version") #f #f
                  (display-and-exit-proc "firefox version 0.0.1\n"))
          (option '(#\h "help") #f #f
                  (display-and-exit-proc
                   "Usage: firefox ...")))))

(define %default-options
  '())

(define %home
  (and=> (getenv "HOME")
         (lambda (home)
           home)))

(define %mjru
  (string-append %home "/.config/mjru/firefox.scm"))

(define %sql-query
  `(,`("Groups"
       ,@(load %mjru)
       ("Default Group"
        ("Modified" . 1624594397582)
        ("Type" . "local")
        ("Words" . #())
        ("DontShowOn" . #())
        ("ShowOn" . #())
        ("FindWords" . #t)
        ("Enabled" . #t)
        ("ShowInEditableFields" . #f)
        ("Fcolor" . "#000")
        ("Color" . "#ff6")))
    ("PerformanceSetting" . 100)
    ("Donate" . 1626322397582)
    ("PrintHighlights" . #t)
    ("ShowFoundWords" . #t)
    ("neverHighlightOn" . #())
    ("Version" . "13")))

(define %origin-key
  "78bc100e424d-6909-d8c4-5f4e-f0724f7c.:moz-extension")

(define %webappsstore
  (string-append %home "/.mozilla/firefox/j56dvo43.default-1520714705340/webappsstore.sqlite"))

(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))
  (system*
   "sqlite3"
   %webappsstore
   (format #f "SELECT * FROM webappsstore2 WHERE originKey = ~s"
           %origin-key))
  (system*
   "sqlite3"
   %webappsstore
   (format #f "UPDATE webappsstore2 SET value = '~a' WHERE originKey = ~s"
           (scm->json-string %sql-query) %origin-key)))

;;; firefox ends here
