(defun uuencode (fullname)
  "Insert a file after compressing and uuencoding it"
  (interactive "*fInsert uuencoded file or directory: ")
  (let* ((here (point))
         (name (if (string-match ".*/$" fullname)
                   (substring fullname 0 -1)
                   fullname))
         (prefix (file-name-directory name))
         (suffix (file-name-nondirectory name))
         (finalname (read-string "Final name? " suffix))
         (tmpname (concat finalname ".uu"))
         (isdir (file-directory-p name))
         (package (if isdir (concat finalname ".tar") finalname))
         (alreadycompressed 
            (or (string-match ".*[.]Z$" name)
                (string-match ".*[.]gz$" name)))
         (Z (concat package ".gz"))
         (buildcmd (if isdir 
                       (concat "tar chf - -C " prefix " " suffix)
                     (concat "cat " name )))
         (cmd 
          (if alreadycompressed
              (concat buildcmd " | uuencode " package)
              (concat buildcmd " | gzip --best -c | uuencode " Z)))
         end-msg)
    (insert "Put the remainder of this message in a temporary file " 
            tmpname "\n"
            "and use the following commands to recover the original\n"
            (if isdir "directory" "file") " "
            finalname ":\n\n"
            "uudecode " tmpname "\n"
            (if alreadycompressed "" (concat "gunzip " Z "\n"))
            (if isdir (concat "tar xvf " package "\n") "")
            "\n"
            "------------------------- "
            " cut here " 
            "-------------------------\n")
    (message "%s ..." cmd)
    (sit-for 0)
    (setq end-msg (point))
    (shell-command-on-region (point) (point) cmd t)
    (goto-char here)
    (set-mark end-msg)
    (message "")))
  
(defun uudecode ()
  "Uudecode and print (or untar, etc.) a file"
  (interactive)
  (goto-char (point-min))
  (if (not (re-search-forward "^begin ... \\(.*\\)\n" (point-max) t))
      (error "Can't find beginning of uuencoded text"))
  (let ((name (buffer-substring (match-beginning 1) (match-end 1)))
        (preface (match-beginning 0))
        (beg (match-end 0))
        (uu (make-temp-name "/tmp/uu"))
        (contents (make-temp-name "/tmp/"))
        end file junk cmd)
    (message "Extracting %s..." name)
    (re-search-forward "^end\n")
    (setq end (match-end 0))
    (setq file (buffer-substring beg end))
    (goto-char preface)
    (set-mark end)
    (save-window-excursion
      (find-file uu)
      (insert "begin 600 " contents "\n")
      (insert file)
      (save-buffer)
      (kill-buffer nil))
    (message "uudecode %s ..." uu)
    (shell-command (concat "uudecode " uu))
    (sit-for 1)
    (setq cmd (concat "cat " contents))
    (if (string-match ".*[.]Z$" name)
        (progn
          (setq name (substring name 0 -2))
          (setq cmd (concat cmd " | uncompress "))
          ))
    (if (string-match ".*[.]gz$" name)
        (progn
          (setq name (substring name 0 -3))
          (setq cmd (concat cmd " | gzip -d -c "))
          ))
    (cond 
     ((string-match ".*[.]dvi$" name)
        (setq cmd (concat cmd " | lpr ")))
     ((string-match ".*[.]ps$" name)
        (setq cmd (concat cmd " | lpr ")))
     ((string-match ".*[.]tar$" name)
      (setq cmd (concat "cd ~/tmp; " cmd " | tar xvf -")))
     ((string-match ".*[.]tex$" name)
      (setq cmd (concat cmd " > " (getenv "HOME") "/" name)))
     (t 
      (setq cmd (concat "cd ~/tmp; " cmd " > " name))))
    (setq cmd (read-string "" cmd))
    (shell-command cmd)
))



