#!/usr/bin/guile
!#

(import (sph filesystem) (sph hashtable)
  (sph io) (sph list)
  (sph number) (sph other) (sph process) (sph string) (sph vector) (sph) (sph other))

(define path-config-temp "/tmp/gui-group-config")

(define (get-last-used-directory)
  (and (file-exists? path-config-temp)
    (call-with-input-file path-config-temp (l (port) (read port)))))

(define (set-last-used-directory a)
  (call-with-output-file path-config-temp (l (port) (write a port))))

(define (get-entry-text-option) "")

(define (move-into-directory-zenity paths)
  "arguments: path-of-file-to-move ...
   asks for a target path and moves paths given as arguments into the provided target path. (grouping of files)"
  (if (not (null? paths))
    (let*
      ( (last-used (get-last-used-directory))
        (entry-text-option (if last-used (list "entry-text" last-used) null))
        (path-directory
          (string-trim-right
            (shell-eval->string
              (string-append "zenity "
                (cli-option-join
                  (list "--entry" (list "text" (string-quote "enter group name:"))
                    entry-text-option)))))))
      (if (> (string-length path-directory) 0)
        (begin
          (if (or (not last-used) (not (string-equal? last-used path-directory)))
            (set-last-used-directory path-directory))
          (apply execute "group" path-directory paths))))))

(move-into-directory-zenity (tail (program-arguments)))