;; remote-vm.el -- running vm remotely
;; SCCS Status     : %W%	%G%
;; Author          : Kai Grossjohann
;; Created On      : Mon Sep 28 17:48:19 1992
;; Last Modified By: Kai Grossjohann
;; Last Modified On: Mon Nov  2 19:15:53 1992
;; Update Count    : 37
;; Status          : Unknown, Use with caution!

;; HISTORY 
;; 8-Oct-1992		Kai Grossjohann	
;;    Last Modified: Tue Sep 29 15:08:44 1992 #6 (Ulrich Pfeifer)
;;    Now works regardless of setting of mail variable
;;    "alwaysignore".
;; 29-Sep-1992		Ulrich Pfeifer	
;;    Last Modified: Tue Sep 29 15:03:01 1992 #5 (Ulrich Pfeifer)
;;    No recursion if loaded twice
;; TODO
;;    1. Fix to work with vm-berkeley-mail-compatibility not equal to
;;       nil.
;;    2. Make new mails be marked "new" instead of just "unread".
;;    3. Real checking for mail on remote host ?
;;    4. Enter passwd if necessary ?



(if (featurep 'remote-vm) ()
  (progn

    (require 'vm)
    (provide 'remote-vm)

    (let ( (host (getenv "HOST"))
           (mailhost (getenv "MAILHOST")) )
      (if (and host mailhost
               (not (string-equal host mailhost)) )
          (progn
            (setq vm-spool-files (list "~/.vm-spooled-mail")))))

    (fset 'old-vm-get-spooled-mail (symbol-function 'vm-get-spooled-mail))

    (defun vm-get-spooled-mail ()
      "Kais remote mail feature, sets vm-spool-files to ~/.vm-spooled-mail, 
then executes the command
	rsh <mailhost> <movemail> /usr/spool/mail/<user> ~/.vm-spooled-mail
and finally calls the old definition of vm-get-spooled-mail."
      (interactive)
      (let ( (host (getenv "HOST"))
             (mailhost (getenv "MAILHOST"))
             (logname (getenv "LOGNAME")))
        (if (and host mailhost
                 (not (string-equal host mailhost)) )
	    (save-excursion
	      (let ((buf (get-buffer-create "*MAIL*")))
		(set-buffer buf)
		(message "Reading Mail on %s..." mailhost)
		(sit-for 0)
		(call-process "rsh" nil buf t
			      mailhost
			      (concat (getenv "LEMACSHOME") "/etc/movemail")
			      (concat "/usr/spool/mail/" logname)
			      (car vm-spool-files) "\n")
		(sit-for 0)
		(message "Reading Mail on %s...done" mailhost)
		(sit-for 0))) ; save-excursion
	  )) ; let
      (old-vm-get-spooled-mail))
    
    (fset 'old-vm-check-for-spooled-mail 
          (symbol-function 'vm-check-for-spooled-mail))

    (defun vm-check-for-spooled-mail ()
      "Kais remote mail feature"
      (let ( (host (getenv "HOST"))
             (mailhost (getenv "MAILHOST")) )
        (if (and host mailhost (string-equal host mailhost))
            (old-vm-check-for-spooled-mail)  t)))

    ))
