;;; -*- Emacs-Lisp -*-
;;; WL-MK for byte-compile, install, uninstall
;;;
;;; Original by OKUNISHI Fujikazu <fuji0924@mbox.kyoto-inet.or.jp>
;;; Modified by Yuuichi Teranishi <teranisi@gohome.org>
;;;
;;; Time-stamp: <99/06/28 16:43:49 teranisi>

;;; Code

(defvar ELMODIR "./elmo")
(defvar DOCDIR "./doc")
(defvar UTILSDIR "./utils")
(defvar WL_PREFIX "")
(defvar ELMO_PREFIX "")
(defvar COMPRESS-SUFFIX-LIST '("" ".gz" ".Z" ".bz2"))
(setq load-path (cons "." (cons ELMODIR load-path)))

;;; load custom file if exists.
(load "./WL-CFG" t nil t)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; WL modules
(defconst WL-MODULES '(
   "wl" "wl-folder" "wl-summary" "wl-message"
   "wl-vars" "wl-draft" "wl-util" "wl-address"
   "wl-highlight" "wl-demo" "wl-refile" "wl-thread" 
   "wl-fldmgr" "wl-expire" "wl-template"
   ))

(defconst WL-MODULES-SEMI '(
   "wl-mime"
   ))		    

(defconst WL-MODULES-TM '(
   "tm-wl" "smtp"
   ))

(defconst WL-MODULES-MULE '(
   "wl-mule"
   ))

(defconst WL-MODULES-NEMACS '(
   "wl-nemacs"
   ))
   
(defconst WL-MODULES-XMAS '(
   "wl-dnd" "wl-xmas"
   ))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ELMO modules

(defconst ELMO-MODULES '(
   "elmo-util" "elmo-imap4" "elmo-nntp" "elmo-archive"
   "elmo-localdir" "elmo-msgdb" "elmo-vars" "elmo2"
   "elmo-cache" "elmo-multi" "elmo-filter"
   "elmo-dop" "elmo-pop3" "elmo-localnews" "elmo-maildir"
   "elmo-date" "elmo-internal"
   ))

(defconst ELMO-MODULES-SEMI '(
   "mmelmo" "mmelmo-imap4"
   ))

(defconst ELMO-MODULES-XMAS '(
   "elmo-database"
   ))

(defconst WL-AUTOLOAD-MODULES '(
   "auto-autoloads" "custom-load"
   ))

;;; Utils
(defvar wl-utils-modules '("bbdb-wl" ; "im-wl" "wl-user-agent.el" 
			   )
  "if `wl-install-utils' is Non-nil, modules in this list are installed.")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; INFO files
(defconst WL-INFOS '("wl-ja.info"))
(defconst WL-TEXIS '("wl-ja.texi"))


;;;;;;;;;;;;;;;;;;;;; DO NOT EDIT THE FOLLOWINGS ;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;     INTERNAL USE ONLY      ;;;;;;;;;;;;;;;;;;;;;
(require 'cl)

(setq byte-compile-warnings '(free-vars unresolved callargs redefine))

(condition-case ()
    (require 'mime-setup)
  (error (error "No MIME module was detected. Please install SEMI or tm.")))

(defvar wl-install-utils (module-installed-p 'bbdb)
  "if Non-nil, install `wl-utils-modules'.")

(require 'easymenu)


(defun config-wl-package ()
  ;; MIME module check
  (defvar wl-use-semi nil)
  (if (module-installed-p 'mime-view)	; SEMI is installed
      (progn
	(princ "\nUse SEMI.\n")
	(setq wl-use-semi t))
    (if (module-installed-p 'tm-view)	; tm is installed
	(princ "\nUse tm.\n")))
  ;; ELISPDIR check.
  (let ((elispdir (car command-line-args-left)))
    (if (string= elispdir "NONE")
	(progn
	  (condition-case ()
	      (require 'install)
	    (error (error "Please set variable \"ELISPDIR\" in Makefile.")))
	  (defvar ELISPDIR (install-detect-elisp-directory)))
      (defvar ELISPDIR elispdir)))
  (princ (format "ELISPDIR is %s\n\n" ELISPDIR))
  (setq command-line-args-left (cdr command-line-args-left)))

(defun wl-els (modules)
  (mapcar (lambda (x) (concat x ".el")) modules))

(defun wl-elcs (modules)
  (mapcar (lambda (x) (concat x ".elc")) modules))

(defun wl-el-to-elc (el-file)
  (if (string-match "\\(.*\\).el" el-file)
      (concat (substring el-file (match-beginning 1) (match-end 1)) ".elc")))

(defun wl-compile (srcs)
  (unless (featurep 'wl)
    (load "./wl.el"))
  (mapcar (lambda (x)
	    (unless (and (file-exists-p (wl-el-to-elc x))
			 (file-newer-than-file-p (wl-el-to-elc x) x))
	      (save-excursion
		(byte-compile-file x))))
	  srcs))

(defun wl-install (objs path &optional copy-only)
  (mapcar (lambda (x)
	    (let ((filename (expand-file-name x path)))
	      (if (file-exists-p x)
		  (progn
		    (princ (format "%s->%s\n" x filename))
		    (if copy-only
			(copy-file x filename t t)
		      (rename-file x filename t)))
		(error "%s does not exist." x))))
	  objs))

(defun wl-uninstall (objs path)
  (mapcar (lambda (x)
	    (let ((filename (expand-file-name x path)))
	      (if (and (file-exists-p filename)
		       (file-writable-p filename))
		  (progn
		    (princ (format "%s was uninstalled.\n" filename))
		    (delete-file filename)))))
	  objs))

(defun compile-wl-package ()
  (config-wl-package)
  (wl-compile (wl-els WL-MODULES))
  (if wl-use-semi
      (wl-compile (wl-els WL-MODULES-SEMI))
    (wl-compile (wl-els WL-MODULES-TM)))
  (setq elmo-use-semi wl-use-semi)
  (if (fboundp 'nemacs-version)
      (wl-compile (wl-els WL-MODULES-NEMACS))
    (if (featurep 'xemacs)
	(wl-compile (wl-els WL-MODULES-XMAS))
      (wl-compile (wl-els WL-MODULES-MULE))))
  (cd ELMODIR)
  (wl-compile (wl-els ELMO-MODULES))
  (if wl-use-semi
      (wl-compile (wl-els ELMO-MODULES-SEMI)))
  (if (featurep 'xemacs)
      (wl-compile (wl-els ELMO-MODULES-XMAS)))	
  (cd "..")
  (when wl-install-utils
      (cd UTILSDIR)
      (wl-compile (wl-els wl-utils-modules))
      (cd "..")))	


(defun install-wl-package ()
; (config-wl-package)
  (compile-wl-package)
  (let ((wl-install-dir (expand-file-name WL_PREFIX
					  ELISPDIR))
	(elmo-install-dir (expand-file-name ELMO_PREFIX
					    ELISPDIR)))
    (or (file-exists-p wl-install-dir)
	(make-directory wl-install-dir t))
    (or (file-exists-p elmo-install-dir)
	(make-directory elmo-install-dir t))
    (if (file-writable-p wl-install-dir)
	(progn
	  (wl-install (wl-elcs WL-MODULES) wl-install-dir)
	  (if wl-use-semi
	      (wl-install (wl-elcs WL-MODULES-SEMI) wl-install-dir)
	    (wl-install (wl-elcs WL-MODULES-TM) wl-install-dir))
	  (if (fboundp 'nemacs-version)
	      (wl-install (wl-elcs WL-MODULES-NEMACS) wl-install-dir))
	  (if (featurep 'xemacs)
	      (wl-install (wl-elcs WL-MODULES-XMAS) wl-install-dir)
	    (wl-install (wl-elcs WL-MODULES-MULE) wl-install-dir))
	  (when wl-install-utils
	    (cd UTILSDIR)
	    (wl-install (wl-elcs wl-utils-modules) wl-install-dir)
	    (cd "..")))
      (error "%s is not writable." wl-install-dir))
    (cd ELMODIR)
    (if (file-writable-p elmo-install-dir)
	(progn
	  (wl-install (wl-elcs ELMO-MODULES) elmo-install-dir)
	  (if wl-use-semi
	      (wl-install (wl-elcs ELMO-MODULES-SEMI) elmo-install-dir))
	  (if (featurep 'xemacs)
	      (wl-install (wl-elcs ELMO-MODULES-XMAS) elmo-install-dir)))
      (error "%s is not writable." elmo-install-dir))
    (cd "..")))

(defun uninstall-wl-package ()
  (config-wl-package)
  (let ((wl-install-dir (expand-file-name WL_PREFIX
					  ELISPDIR))
	(elmo-install-dir (expand-file-name ELMO_PREFIX
					    ELISPDIR)))
    (wl-uninstall (wl-elcs (append WL-MODULES WL-MODULES-SEMI 
				   WL-MODULES-TM WL-MODULES-XMAS
				   WL-MODULES-MULE WL-MODULES-NEMACS
				   WL-AUTOLOAD-MODULES wl-utils-modules))
		  wl-install-dir)
    (wl-uninstall (wl-elcs (append ELMO-MODULES ELMO-MODULES-SEMI))
		  elmo-install-dir)))

(defun cleanup-wl-package ()
  ;; (config-wl-package)
  (wl-uninstall (append (wl-elcs (append WL-MODULES WL-MODULES-SEMI 
					 WL-MODULES-TM WL-MODULES-XMAS
					 WL-MODULES-MULE WL-MODULES-NEMACS
					 WL-AUTOLOAD-MODULES
					 ))
			(wl-els WL-AUTOLOAD-MODULES))
		".")
  (wl-uninstall (wl-elcs (append ELMO-MODULES ELMO-MODULES-SEMI
			         ELMO-MODULES-XMAS))
		ELMODIR)
  (wl-uninstall (wl-elcs wl-utils-modules)
		UTILSDIR))

(defun config-wl-package-xmas ()
  (if (not (featurep 'xemacs))
      (error "This directive is only for XEmacs."))
  ;; MIME module check
;  (condition-case ()
;      (require 'mime-setup)
;    (error (error "No MIME module was detected. Please install SEMI or tm.")))
  (defvar wl-use-semi nil)
  (if (module-installed-p 'mime-view)	; SEMI is installed
      (progn
	(princ "\nUse SEMI.\n")
	(setq wl-use-semi t))
    (if (module-installed-p 'tm-view)	; tm is installed
	(princ "\nUse tm.\n")))
  ;; PACKAGEDIR check.
  (let (package-dir)
    (and (setq package-dir (car command-line-args-left))
	 (if (string= "NONE" package-dir)
	     (defvar PACKAGEDIR
	       (if (boundp 'early-packages)
		   (let ((dirs (append (if early-package-load-path
					   early-packages)
				       (if late-package-load-path
					   late-packages)
				       (if last-package-load-path
					   last-packages)))
			 dir)
		     (while (not (file-exists-p
				  (setq dir (car dirs))))
		       (setq dirs (cdr dirs)))
		     dir)))
	   (defvar PACKAGEDIR package-dir)))
    (princ (format "PACKAGEDIR is %s\n\n" PACKAGEDIR))
    (setq command-line-args-left (cdr command-line-args-left))))

;; from SEMI-MK
(defun compile-wl-package-xmas ()
  (config-wl-package-xmas)
  (setq autoload-package-name "wl")
  (add-to-list 'command-line-args-left ".")
  (batch-update-directory)
  (add-to-list 'command-line-args-left ".")
  (Custom-make-dependencies)
  (wl-compile (wl-els WL-MODULES))
  (wl-compile (wl-els (append WL-MODULES-XMAS WL-AUTOLOAD-MODULES)))
  (if wl-use-semi
      (wl-compile (wl-els WL-MODULES-SEMI))
    (wl-compile (wl-els WL-MODULES-TM)))
  (setq elmo-use-semi wl-use-semi)
  (wl-compile (wl-els WL-MODULES-XMAS))
  (cd ELMODIR)
  (wl-compile (wl-els ELMO-MODULES))
  (if wl-use-semi
      (wl-compile (wl-els ELMO-MODULES-SEMI)))
  (wl-compile (wl-els ELMO-MODULES-XMAS))
  (cd "..")
  (when wl-install-utils
    (cd UTILSDIR)
    (wl-compile (wl-els wl-utils-modules))
    (cd "..")))

(defun install-wl-package-xmas ()
;  (config-wl-package-xmas)
  (compile-wl-package-xmas)
  (let ((ELISPDIR (expand-file-name "wl"
				    (expand-file-name "lisp"
						      PACKAGEDIR)))
	(DATADIR  (expand-file-name "wl"
				    (expand-file-name "etc"
						      PACKAGEDIR)))
	(INFODIR  (expand-file-name "info" PACKAGEDIR)))
    (or (file-exists-p ELISPDIR)
	(make-directory ELISPDIR t))
    (or (file-exists-p DATADIR)
	(make-directory DATADIR t))
    (or (file-exists-p INFODIR)
	(make-directory INFODIR t))
    (wl-install (wl-elcs WL-MODULES) ELISPDIR)
    ;; copy xpm files
    (mapcar (lambda (x) 
	      (let* ((src-file-nondirectory (file-name-nondirectory x))
		     (dst-file (expand-file-name src-file-nondirectory
						 DATADIR)))
		(princ (format "%s->%s\n"
			       x ; fullpath
			       dst-file))
		(copy-file x dst-file t)))
	    (directory-files "etc" t "xpm"))
    (if wl-use-semi
	(wl-install (wl-elcs WL-MODULES-SEMI) ELISPDIR)
      (wl-install (wl-elcs WL-MODULES-TM) ELISPDIR))
    (wl-install (wl-elcs WL-MODULES-XMAS) ELISPDIR)
    (wl-install (wl-elcs WL-AUTOLOAD-MODULES) ELISPDIR)
    (when wl-install-utils
      (cd UTILSDIR)
      (wl-install (wl-elcs wl-utils-modules) ELISPDIR)
      (cd ".."))
    (cd ELMODIR)
    (wl-install (wl-elcs ELMO-MODULES) ELISPDIR)
    (if wl-use-semi
	(wl-install (wl-elcs ELMO-MODULES-SEMI) ELISPDIR))
    (wl-install (wl-elcs ELMO-MODULES-XMAS) ELISPDIR)
    (cd "..")
    ;; 
    (if (not (file-exists-p (expand-file-name (car WL-INFOS) DOCDIR)))
	(wl-texinfo-format))
    (cd DOCDIR)
    (wl-install WL-INFOS INFODIR)
    (cd "..")
    ))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Texinfo stuff

(defun wl-texinfo-format ()
  (require 'wl) ;; for 'wl-cs-local
  (cd DOCDIR)
  (mapcar (lambda (x)
	    (let (obuf beg)
	      (find-file x)
	      (setq obuf (current-buffer))
	      (condition-case nil
		  ;; We can't know file names if splitted.
		  (texinfo-format-buffer t)
		(error ; one more try with no @direntry
		 ;(kill-buffer (current-buffer))
		 (set-buffer obuf)
		 (goto-char (point-min))
		 (message "Format failed. Trying no @direntry")
		 (when (re-search-forward "@direntry" nil t)
		   (setq beg (match-beginning 0))
		   (when (re-search-forward "@end direntry" nil t)
		     (delete-region beg (match-end 0))))
		 (texinfo-format-buffer t)))
	      ;; Emacs20.2's default is 'raw-text-unix.
	      (and (fboundp 'set-buffer-file-coding-system)
	           (set-buffer-file-coding-system wl-cs-local))
	      (save-buffer)
	      (kill-buffer (current-buffer)) ;; info
	      (kill-buffer obuf))) ;; texi
	  WL-TEXIS)
  (cd ".."))

(defun wl-detect-info-directory ()
  ;; INFODIR check.
  (require 'info)
  (let ((infodir (car command-line-args-left))
	(info (car WL-INFOS))
	previous INFODIR)
    (setq INFODIR
	  (if (string= infodir "NONE")
	      (if (setq previous
			(exec-installed-p info Info-directory-list
					  COMPRESS-SUFFIX-LIST))
		  ;;(progn
		  ;;(condition-case nil (delete-file previous))
		  (directory-file-name (file-name-directory previous));)
		(car Info-directory-list))
	    infodir))
    (princ (format "INFODIR is %s\n\n" INFODIR))
    (setq command-line-args-left (cdr command-line-args-left))
    INFODIR))

(defun install-wl-info ()
  (let ((INFODIR (wl-detect-info-directory)))
    (cd DOCDIR)
    (wl-install WL-INFOS INFODIR)
    (cd "..")))


;;; ToDo
;;; * MORE refine code (^_^;

;;; End
