;;
;; $Id: uugrab.el,v 1.3 1994/05/25 19:45:16 ejb Exp $
;; $Source: /home/ejb/elisp/q/RCS/uugrab.el,v $
;; $Author: ejb $
;;
;; Get a file from uunet via uucp from the ls-lR.Z file
;;

(defun lslr-dirname ()
  (save-excursion
    ;; Search back to a line that ends with : and precedes the word "total"
    (search-backward ":\ntotal")
    ;; Return the string from the beginning of the line to the :
    (buffer-substring (save-excursion (beginning-of-line) (point)) (point))))

(defun lslr-filename ()
  (save-excursion
    ;; Go to the beginning of the line and skip forward to the
    ;; eighth field
    (beginning-of-line)
    (let ((i 0))
      (while (< i 7)
	(skip-chars-forward "^ \t")
	(skip-chars-forward " \t")
	(setq i (1+ i))))
    (buffer-substring (point) (save-excursion
				(skip-chars-forward "^ \t\n")
				(point)))))

(defun lslr-fullpath ()
  (concat (lslr-dirname) "/" (lslr-filename)))

(defun uugrab-file (arg)
  "With cursor positioned in a buffer containing the output of ls -lR,
determine the full path of the file on the line with the cursor and
generate the command 

uugrab fullpath [dest]

dest defaults to blank but will be prompted for if uugrab is invoked
with an argument."
  (interactive "P")
  (let (dest)
    (setq dest
	  (if arg
	      (read-string "Enter destination filename: ")
	    nil))
    (if dest
	(call-process "uugrab" nil nil nil "-fast" (lslr-fullpath) dest)
      (call-process "uugrab" nil nil nil "-fast" (lslr-fullpath)))))
