;;; gdrive.el --- Gdrive integration                 -*- lexical-binding: t; -*-

;; Copyright (C) 2022  Case Duckworth

;; Author: Case Duckworth <case@bob>
;; Keywords: convenience, data, docs

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; [[https://github.com/prasmussen/gdrive][gdrive]] is a Go program to interface with Google Drive.  This library connects
;; that to Emacs.

;;; Code:

(require 'cl-lib)

(defgroup gdrive nil
  "Customizations for Emacs's gdrive module."
  :group 'applications
  :prefix "gdrive-")

(defcustom gdrive-bin (executable-find "gdrive")
  "Where gdrive binary is located."
  :type 'string)

(defcustom gdrive-buffer "*gdrive*"
  "Default buffer for gdrive output."
  :type 'string)

;;; Global flags

;;;; -c, --config <configDir>
;;;;; Application path, default: /Users/<user>/.gdrive
(defcustom gdrive-config-dir nil
  "Application path.")

;;;; --refresh-token <refreshToken>
;;;;; Oauth refresh token used to get access token (for advanced users)
(defcustom gdrive-refresh-token nil
  "Oauth refresh token used to get access token.
(For advanced users).")

;;;; --access-token <accessToken>
;;;;; Oauth access token, only recommended for short-lived requests because of
;;;;; short lifetime (for advanced users)
(defcustom gdrive-access-token nil
  "Oauth access token.
Only recommended for short-lived requests because of short
lifetime (for advanced users).")

;;;; --service-account <accountFile>
;;;;; Oauth service account filename, used for server to server communication
;;;;; without user interaction (file is relative to config dir)
(defcustom gdrive-service-account nil
  "Oauth service account filename.
Used for server to server communication without user
interaction (file is relative to config dir).")

(defun gdrive--global-arguments ()
  "Build global arguments for gdrive."
  (append
   (when gdrive-config-dir (list "--config" gdrive-config-dir))
   (when gdrive-refresh-token (list "--refresh-token" gdrive-refresh-token))
   (when gdrive-access-token (list "--access-token" gdrive-access-token))
   (when gdrive-service-account (list "--service-account" gdrive-service-account))))

;;; List files
;; gdrive [global] list [options]
;;;; -m, --max <maxFiles>
;;;; Max files to list, default: 30
;;;; -q, --query <query>
;;;;; Default query: "trashed = false and 'me' in owners". See https://developers.google.com/drive/search-parameters
;;;; --order <sortOrder>
;;;;; Sort order. See https://godoc.org/google.golang.org/api/drive/v3#FilesListCall.OrderBy
;;;; --name-width <nameWidth>
;;;;; Width of name column, default: 40, minimum: 9, use 0 for full width
;; NOTE: gdrive-list will pass 0 for this argument.
;;;; --absolute		Show absolute path to file (will only show path from first parent)
;;;; --no-header	Dont print the header
;; NOTE: gdrive-list will always pass this argument.
;;;; --bytes		Size in bytes
(cl-defun gdrive-list (&key max query order absolute no-header bytes)
  "Run the \"gdrive list\" command.
MAX is the max files to list; it defaults to 30.  QUERY is the
query to pass; the default is \"trashed = false and 'me' in
owners\"."
  (gdrive--run (append (gdrive--global-arguments)
                       (list "list")
                       (when max (list "--max" max))
                       (when query (list "--query" query))
                       (when order (list "--order" order))
                       (list "--name-width" "0")
                       (when absolute (list "--absolute"))
                       (when no-header (list "--no-header"))
                       (when bytes (list "--bytes")))))


(defmacro gdrive-query)


(defun gdrive--build-command-name (command)
  "INTERNAL: Build a string name for COMMAND."
  (concat "gdrive-" (car command)))

(defun gdrive--run (command &optional buffer)
  "Run 'gdrive COMMAND', collecting results in BUFFER.
COMMAND, if not a list, will be made a list and appended to
`gdrive-bin'.
BUFFER defaults to `gdrive-buffer'."
  (let ((command (if (listp command) command (list command)))
        (buffer (or buffer gdrive-buffer)))
    (make-process :name (gdrive--build-command-name command)
                  :buffer buffer
                  :command (cons gdrive-bin command))))

(provide 'gdrive)
;;; gdrive.el ends here
