;;; **********************************************************************
;;; Module support for emacs
;;;
;;; Lars Heete 1994
;;;
;;; *********************************************************************
;;;


(defvar module-cmd "/app/unido-inf/sun4_55/modules/current/bin/modulecmd" "\
The variable module-cmd describes the full path of the module command.")

(defun module-split-string (str)
  (let ((lst nil)
	(pos 0))
    (while (and (< pos (length str)) (string-match "\\([^ \t\n]*\\)" str pos))
      (setq lst
	    (nconc lst 
		   (list (substring str (match-beginning 1) (match-end 1)))))
      (setq pos (+ (match-end 1) 1)))
    lst))
    
(defun module-command (command &rest args)
  (let ((buffer (get-buffer-create "*modulecmd*")))
    (set-buffer buffer)
    (let ((proc (get-buffer-process buffer)))  ; Blast any old process.
      (if proc (delete-process proc)))

    (erase-buffer)

    (let ((list-directory-brief-switches nil)
          (proc (apply 'call-process module-cmd nil buffer nil
		       "emacs" command args)))

;;; Hack for recognising information output
;;; because I don't know how to separate stdout from stderr output.
      (goto-char (point-min))
      (if (looking-at "[ \t\n]*--------\\|[ \t\n]*Currently\\|[ \t\n]*Avail")
	  (display-buffer buffer)
        (beginning-of-buffer)
;        (while (search-forward "(setenv " nil t)
;          (insert "\"")
;          (search-forward " ")
;          (backward-char 1)
;          (insert "\"")
;          (forward-char 1)
;          (insert "\"")
;          (end-of-line)
;          (backward-char 1)
;          (insert "\""))
	(eval-current-buffer)))))

;;;###autoload

(defun module (arg)
  "Interface to the module command."
  (interactive "sModule command: ")
  (apply 'module-command (module-split-string arg)))


