;;; CMAIL-MK					-*-Emacs-Lisp-*-

(defvar install-apel nil)
(defvar compile-cmail-options t)
(defvar install-cmail-options t)

(defun config-cmail ()
  (let (prefix exec-prefix lisp-dir)
    (and (setq prefix (car command-line-args-left))
	 (or (string-equal "NONE" prefix)
	     (defvar PREFIX prefix)
	     ))
    (setq command-line-args-left (cdr command-line-args-left))
    (and (setq exec-prefix (car command-line-args-left))
	 (or (string-equal "NONE" exec-prefix)
	     (defvar EXEC_PREFIX exec-prefix)
	     ))
    (setq command-line-args-left (cdr command-line-args-left))
    (and (setq lisp-dir (car command-line-args-left))
	 (or (string-equal "NONE" lisp-dir)
	     (defvar LISPDIR lisp-dir)
	     ))
    (setq command-line-args-left (cdr command-line-args-left))
    (and (setq info-dir (car command-line-args-left))
	 (or (string-equal "NONE" info-dir)
	     (defvar INFODIR info-dir)
	     ))
    (setq command-line-args-left (cdr command-line-args-left))
    (and (setq version-specific-lisp-dir (car command-line-args-left))
	 (or (string-equal "NONE" version-specific-lisp-dir)
	     (progn
	       (defvar VERSION_SPECIFIC_LISPDIR version-specific-lisp-dir)
	       (princ (format "VERSION_SPECIFIC_LISPDIR=%s\n"
			      VERSION_SPECIFIC_LISPDIR)))
	     ))
    (setq command-line-args-left (cdr command-line-args-left))
    )
  (load-file "CMAIL-CFG")
  (load-file "CMAIL-ELS")
  (princ (format "PREFIX=%s\nEXEC_PREFIX=%s\nLISPDIR=%s\n"
		 PREFIX EXEC_PREFIX LISPDIR)))

(defun compile-info (file)
  (condition-case err
      (progn
	(if buffer-file-name (kill-buffer (current-buffer)))
	(find-file file)
	(buffer-disable-undo (current-buffer))
	(set-buffer-modified-p nil)
	(texinfo-mode)
	(message "texinfo formatting %s..." file)
	(texinfo-format-buffer nil)
	(if (buffer-modified-p)
	    (progn (message "Saving modified %s" (buffer-file-name))
		   (save-buffer)
		   (kill-buffer (current-buffer)))))
    (error
     (message ">> Error: %s" (prin1-to-string err))
     (message ">>  point at")
     (let ((s (buffer-substring (point)
				(min (+ (point) 100)
				     (point-max))))
	   (tem 0))
       (while (setq tem (string-match "\n+" s tem))
	 (setq s (concat (substring s 0 (match-beginning 0))
			 "\n>>  "
			 (substring s (match-end 0)))
	       tem (1+ tem)))
       (message ">>  %s" s))
     (setq error 1))))

(defun compile-cmail ()
  (let ((curdir (expand-file-name ".")))
    (config-cmail)
    (princ (format "%s\n" (emacs-version)))
    (if install-apel (compile-apel-from-cmail))
    (compile-elisp-modules cmail-modules ".")
    (if compile-cmail-options (compile-cmail-options-from-cmail))
    (if (file-exists-p "doc/cmail.en.info")
	nil
      (compile-info "doc/cmail.en.texi"))
    (if (file-exists-p "doc/cmail.info")
	nil
      (if (>= (string-to-int emacs-version) 20)
	  (set-language-environment "Japanese"))
      (compile-info "doc/cmail.texi"))
    (install-change-dir curdir)))

(defun compile-cmail-no-options ()
  (setq compile-cmail-options nil)
  (compile-cmail))

(defun compile-apel-from-cmail (&optional just-print)
  (let ((curdir (expand-file-name ".")))
    (install-change-dir (expand-file-name "apel"))
    (load "APEL-MK")
    (compile-apel)
    (install-change-dir curdir)))

(defun compile-cmail-options-from-cmail ()
  (let ((curdir (expand-file-name ".")))
    (install-change-dir (expand-file-name "options"))
    (setq CMAIL_OPTIONS_PREFIX (concat CMAIL_DIR "/options"))
    (load "CMAIL-OPTIONS-MK")
    (compile-cmail-options)
    (install-change-dir curdir)))

(defun install-cmail (&optional just-print)
  (if (null just-print) (compile-cmail))
  (princ (format "%s\n" (emacs-version)))
  (if install-apel (install-apel-from-cmail just-print))
  ;; Use cmail-vars.elc to check if it has been compiled.  If not
  ;; compiled yet, most likely we should not use del-elc flag.
  (let ((del-elc (file-exists-p "cmail-vars.elc")))
    (install-elisp-modules cmail-modules "." CMAIL_DIR just-print del-elc))
  (if install-cmail-options (install-cmail-options-from-cmail just-print))
  (if (or (featurep 'xemacs) (>= emacs-major-version 21))
      (install-files (directory-files "icon" nil "\\.x[bp]m$")
		     "icon" CMAIL_ICON_DIR nil t just-print))
  (install-files '("cmail.info" "cmail.en.info") "doc" INFODIR
		 t t just-print))

(defun install-cmail-no-options (&optional just-print)
  (setq install-cmail-options nil)
  (install-cmail just-print))

(defun install-cmail-options-from-cmail (&optional just-print)
  (let ((curdir (expand-file-name ".")))
    (install-change-dir (expand-file-name "options"))
    (setq CMAIL_OPTIONS_PREFIX (concat CMAIL_DIR "/options"))
    (load "CMAIL-OPTIONS-MK")
    (install-cmail-options just-print)
    (install-change-dir curdir)))

(defun install-apel-from-cmail (&options just-print)
  (let ((curdir (expand-file-name ".")))
    (install-change-dir (expand-file-name "apel"))
    (load "APEL-MK")
    (install-apel just-print)
    (install-change-dir curdir)))

(defun install-change-dir (dir)
  (princ (format "Changing Directory to %s\n" dir))
  (setq-default default-directory dir)
  (mapcar (function
	   (lambda (buffer)
	     (save-excursion
	       (set-buffer buffer)
	       (setq default-directory dir))))
	  (buffer-list)))

(defun what-where-cmail ()
  (config-cmail)
  (load-file "CMAIL-ELS")
  (install-cmail 'just-print))

;;; CMAIL-MK ends here
