(load "util.cl")

;; -------------------
;; cURL SFTP Interface
;; -------------------

(defun sftp-send-file (&key file
			    server
			    username
			    password
			    remote-path
			    remote-file-name)
  (let ((curl-command "curl -s"))
    
    (or (and file server remote-path) 
	(error "sftp-send-file: file, server and remote-path arguments needed"))
    
    (setf server (sftp-protocol-prefix server))
    
    (if username
	(progn
	  (setf curl-command (concatenate 'string curl-command " -u " username))
	  (if password (setf curl-command (concatenate 'string curl-command ":" password)))))
    
    (setf curl-command (concatenate 'string curl-command " -T " file))
    
    (setf remote-path (make-path-end-with-slash remote-path))
    
    (if remote-file-name
	(setf remote-path (concatenate 'string remote-path remote-file-name)))
    
    (setf curl-command (concatenate 'string curl-command " " server remote-path))
    
    ;; finally supress all output
    (setf curl-command (concatenate 'string curl-command " >/dev/null 2>&1"))
    
    ;; make the request!
    ;;(princ curl-command) (terpri)
    (run-shell-command curl-command :wait t)))

(defun sftp-get-file (&key remote-path
			   server
			   username
			   password
			   local-path
			   local-name)
  (let ((curl-command "curl -s"))
    
    (or (and remote-path server local-path) 
	(error "sftp-get-file: server local-path and remote-path arguments needed"))
    
    (setf server (sftp-protocol-prefix server))
    
    (if username
	(progn
	  (setf curl-command (concatenate 'string curl-command " -u " username))
	  (if password (setf curl-command (concatenate 'string curl-command ":" password)))))
    
    (setf curl-command
	  (concatenate 'string curl-command " " (remove-trailing-slash server) remote-path))
    
    (setf curl-command
	  (if local-name
	      (concatenate 'string curl-command " -o " (make-path-end-with-slash local-path) local-name)
	    (concatenate 'string "cd " local-path " && " curl-command " -O")))
    
    ;; finally supress all output
    (setf curl-command (concatenate 'string curl-command " >/dev/null 2>&1"))
    
    ;; make the request!
    ;;(princ curl-command) (terpri)
    (run-shell-command curl-command :wait t)))

(defun sftp-list-directory (&key server
                                 (remote-path "/")
                                 username
                                 password)
  (let ((curl-command "curl -s")
	(temp-filename (make-temporary-filename "/tmp/")))
    (or server (error "sftp-list-directory: server argument needed!"))
    
    (setf server (sftp-protocol-prefix server))
    
    (if username
	(progn
	  (setf curl-command (concatenate 'string curl-command " -u " username))
	  (if password (setf curl-command (concatenate 'string curl-command ":" password)))))
    
    (setf curl-command
	  (concatenate 'string
		       curl-command 
		       " -l " 
		       (remove-trailing-slash server) 
		       (make-path-end-with-slash remote-path)
		       " -o "
		       (namestring temp-filename)))
    
    ;; finally supress all output
    (setf curl-command (concatenate 'string curl-command " >/dev/null 2>&1"))
    
    (run-shell-command curl-command :wait t)
    
    (let ((files (mapcar #'(lambda (string) (string-trim '(#\Space #\Tab) string)) (file-lines temp-filename))))
      (delete-file temp-filename)
      (remove-if #'(lambda (file) (or (string= file ".") (string= file ".."))) files))))



;; -----
;; Tests
;; -----
;;(sftp-send-file :file "/Users/john/Desktop/pac.jpg" 
;;		:server "cryon.se" 
;;		:remote-path "/home/john/PublicHTML/stuff"
;;		:remote-file-name "pacman.jpg"
;;		:username "john"
;;		:password "---")
;;
;;(sftp-get-file :server "cryon.se"
;;	       :remote-path "/home/john/PublicHTML/stuff/seen_live.txt"
;;	       :local-path "/Users/john/Desktop/"
;;	       :local-name "konserter.txt"
;;	       :username "john"
;;	       :password "---")

(princ (sftp-list-directory :server "cryon.se"
			    :username "john"
			    :password "---"
			    :remote-path "/home/john/PublicHTML/stuff"))
(terpri)

(sftp-send-file :file "pac.png" 
		:server "cryon.se" 
		:remote-path "/home/john/PublicHTML/stuff"
		:remote-file-name "pacman.png"
		:username "john"
		:password "---")

(princ (sftp-list-directory :server "cryon.se"
			    :username "john"
			    :password "---"
			    :remote-path "/home/john/PublicHTML/stuff"))

(terpri)

(sftp-get-file :server "cryon.se"
	       :remote-path "/home/john/PublicHTML/stuff/thunderstone-solo.txt"
	       :local-path "."
	       :local-name "rules.txt"
	       :username "john"
	       :password "---")

