;;; -*- 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>

;;;;;;;;;;;;;;;;;;;;;   DO NOT EDIT THIS FILE   ;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;     INTERNAL USE ONLY     ;;;;;;;;;;;;;;;;;;;;;

;;; Code

(defvar WLDIR "./wl")
(defvar ELMODIR "./elmo")
(defvar DOCDIR "./doc")
(defvar ICONDIR "./etc/icons")
(defvar UTILSDIR "./utils")
(defvar WL_PREFIX "wl")
(defvar ELMO_PREFIX "wl")

(defvar COMPRESS-SUFFIX-LIST '("" ".gz" ".Z" ".bz2"))

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

;;; INFO
(defconst wl-ja-info "wl-ja.info")
(defconst wl-ja-texi "wl-ja.texi")
(defconst wl-en-info "wl.info")
(defconst wl-en-texi "wl.texi")

(defvar wl-info-lang '("ja" "en")
  "The language of info file (\"ja\" or \"en\").")

;; for Nemacs (dirty!)
(if (not (fboundp 'file-executable-p))
    (fset 'file-executable-p 'file-exists-p))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'cl)
(defvar INFODIR nil)

(condition-case () (require 'custom) (error nil))
;; for wl-vars.el
(unless (and (fboundp 'defgroup)
	     (fboundp 'defcustom)
	     ;; ignore broken module
	     (not (featurep 'tinycustom)))
  (when (and (boundp 'emacs-major-version)
	     (eq emacs-major-version 19)
	     (>= emacs-minor-version 29))
    (message "%s" "
  Warning: You don't seem to have \"new custom\" package installed.
           See README file of APEL package for more information.
"))
  (require 'backquote)
  (defmacro defgroup (&rest args))
  (defmacro defcustom (symbol value &optional doc &rest args)
    (let ((doc (concat "*" (or doc ""))))
      (` (defvar (, symbol) (, value) (, doc))))))

(load "bytecomp" nil t)
(unless (fboundp 'byte-compile-file-form-custom-declare-variable)
  ;; Bind defcustom'ed variables.
  (put 'custom-declare-variable 'byte-hunk-handler
       'byte-compile-file-form-custom-declare-variable)
  (defun byte-compile-file-form-custom-declare-variable (form)
    (if (memq 'free-vars byte-compile-warnings)
	(setq byte-compile-bound-variables
	      (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
    form))

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

;; v18, v19
(if (or (boundp 'MULE)
	(fboundp 'nemacs-version))
    (setq max-lisp-eval-depth 400))

;; FIXME: it is currently needed to byte-compile with Emacs 21.
(setq recursive-load-depth-limit nil)

(condition-case () (require 'easymenu) (error nil))

(defvar config-wl-package-done nil)

(defun config-wl-package-subr ()
  (unless config-wl-package-done
    (setq config-wl-package-done t)
    (setq load-path (cons (expand-file-name ".") load-path))
    (setq load-path (cons (expand-file-name WLDIR)
			  (cons (expand-file-name ELMODIR) load-path)))
    (setq wl-icon-dir (expand-file-name ICONDIR))
    ;; load custom file if exists.  `WL-CFG.el' override for committer.
    (load "./WL-CFG" t nil nil)
    ;; load-path
    (if wl-install-utils
	(setq load-path (cons (expand-file-name UTILSDIR) load-path)))
    (require 'install)
    (load "./WL-ELS" nil nil t)
    (condition-case ()
	(require 'mime-setup)
      (error (error "No MIME module was detected. Please install SEMI or tm.")))
    ;; smtp.el version check.
    (require 'smtp)
    (if (not (fboundp 'smtp-send-buffer))
	(error "Please install FLIM 1.14.0 or later."))
    (princ (concat "\nMIME module is " (if wl-use-semi "SEMI" "tm-8") ".\n"))))

(defun config-wl-pixmap-dir (&optional packagedir)
  "Examine pixmap directory where icon files should go."
  (let ((pixmap-dir (car command-line-args-left)))
    (defvar PIXMAPDIR
      (if (string= pixmap-dir "NONE")
	  (if packagedir
	      (expand-file-name "etc/wl/" packagedir)
	    (if (or (featurep 'xemacs)
		    (and (boundp 'emacs-major-version)
			 (>= emacs-major-version 21)))
		(expand-file-name "wl/icons/" data-directory)))
	pixmap-dir)))
  (if PIXMAPDIR
      (princ (format "PIXMAPDIR is %s\n" PIXMAPDIR)))
  (setq command-line-args-left (cdr command-line-args-left)))

(defun config-wl-package ()
  (config-wl-package-subr)
  ;; LISPDIR check.
  (let ((elispdir (car command-line-args-left)))
    (if (string= elispdir "NONE")
	(defvar LISPDIR (install-detect-elisp-directory))
      (defvar LISPDIR elispdir)))
  (princ (format "LISPDIR is %s\n" LISPDIR))
  (setq command-line-args-left (cdr command-line-args-left))
  ;; PIXMAPDIR check.
  (config-wl-pixmap-dir)
  (princ "\n"))


(defun wl-scan-source (path)
  (let (ret)
    (mapcar
     '(lambda (x)
	(mapcar '(lambda (y)
		   (setq ret (append (list y (concat y "c")) ret)))
		(directory-files x nil "\\(.+\\)\\.el$" t)))
     path)
    ret))


(defun wl-uninstall (objs path)
  ;(message (mapconcat 'identity objs " "))
  (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 ()
  ;; For nemacs byte compiler's strange behavior(?).
  (config-wl-package)
  (if (fboundp 'nemacs-version)
      (load (expand-file-name "wl.el" WLDIR)))
  (mapcar
   '(lambda (x)
      (compile-elisp-modules (cdr x) (car x)))
   modules-alist))

(defun install-wl-icons ()
  (if (not (file-directory-p PIXMAPDIR))
      (make-directory PIXMAPDIR t))
  (let* ((case-fold-search t)
	 (icons (directory-files ICONDIR t "\\.x[bp]m$"))
	 icon dest)
    (while icons
      (setq icon (car icons)
	    icons (cdr icons)
	    dest (expand-file-name (file-name-nondirectory icon) PIXMAPDIR))
      (princ (format "%s->%s\n" icon dest))
      (copy-file icon dest t))))

(defun install-wl-package ()
  (compile-wl-package)
  (let ((wl-install-dir (expand-file-name WL_PREFIX LISPDIR))
	(elmo-install-dir (expand-file-name ELMO_PREFIX LISPDIR)))
    (mapcar
     '(lambda (x)
	(install-elisp-modules (cdr x) (car x)
			       (if (string= (car x) ELMODIR)
				   elmo-install-dir
				 wl-install-dir)))
     modules-alist))
  (if PIXMAPDIR
      (install-wl-icons)))


(defun uninstall-wl-package ()
  (config-wl-package)
  (let ((wl-install-dir (expand-file-name WL_PREFIX
					  LISPDIR))
	(elmo-install-dir (expand-file-name ELMO_PREFIX
					    LISPDIR)))
    (wl-uninstall (wl-scan-source (list WLDIR UTILSDIR))
		  wl-install-dir)
    (wl-uninstall (wl-scan-source (list ELMODIR))
		  elmo-install-dir))
  (if PIXMAPDIR
      (let* ((case-fold-search t)
	     (icons (directory-files PIXMAPDIR t "\\.x[bp]m$"))
	     icon)
	(while icons
	  (setq icon (car icons)
		icons (cdr icons))
	  (if (and (file-exists-p icon)
		   (file-writable-p icon))
	      (progn
		(princ (format "%s was uninstalled.\n" icon))
		(delete-file icon)))))))


(defun config-wl-package-xmas ()
  (if (not (featurep 'xemacs))
      (error "This directive is only for XEmacs."))
  (config-wl-package-subr)
  ;; 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" PACKAGEDIR))
    (setq command-line-args-left (cdr command-line-args-left)))
  ;; PIXMAPDIR check.
  (config-wl-pixmap-dir PACKAGEDIR)
  (princ "\n"))

;; from SEMI-MK
(defun compile-wl-package-xmas ()
  (config-wl-package-xmas)
  (setq autoload-package-name "wl")
  (add-to-list 'command-line-args-left WLDIR)
  (batch-update-directory)
  (add-to-list 'command-line-args-left WLDIR)
  (Custom-make-dependencies)
  ;; WL-AUTOLOAD-MODULES
  (compile-elisp-modules WL-AUTOLOAD-MODULES WLDIR)
  (mapcar
   '(lambda (x)
      (compile-elisp-modules (cdr x) (car x)))
   modules-alist))

(defun install-wl-package-xmas ()
  (compile-wl-package-xmas)
  (let ((LISPDIR (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 DATADIR)
	(make-directory DATADIR t))
    (or (file-exists-p INFODIR)
	(make-directory INFODIR t))
    ;; copy xpm files
    (install-wl-icons)

    (mapcar '(lambda (x)
             (install-elisp-modules (cdr x) (car x) LISPDIR))
                  modules-alist)
    ;; WL-AUTOLOAD-MODULES
    (install-elisp-modules WL-AUTOLOAD-MODULES WLDIR LISPDIR)
    ;;
    (wl-texinfo-format)
    (wl-texinfo-install)))

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

(defun wl-texinfo-format-file (lang)
  (let ((infofile (symbol-value (intern (format "wl-%s-info" lang))))
	(texifile (symbol-value (intern (format "wl-%s-texi" lang)))))
    (require 'wl-vars) ;; for 'wl-cs-local
    (or (file-newer-than-file-p (expand-file-name infofile DOCDIR)
				(expand-file-name texifile DOCDIR))
	(let (obuf beg)
	  ;; Support old texinfmt.el
	  (require 'ptexinfmt (expand-file-name "ptexinfmt.el" UTILSDIR))
	  (find-file (expand-file-name texifile DOCDIR))
	  (setq obuf (current-buffer))
	  ;; We can't know file names if splitted.
	  (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
	)))

(defun wl-texinfo-format ()
  (unless INFODIR
    (setq INFODIR (wl-detect-info-directory)))
  (cond ((listp wl-info-lang)
	 (mapcar 'wl-texinfo-format-file wl-info-lang))
	((stringp wl-info-lang)
	 (wl-texinfo-format-file wl-info-lang))))

(defun wl-texinfo-install-file (lang)
  (let ((infofile (symbol-value (intern (format "wl-%s-info" lang)))))
    (install-file infofile DOCDIR INFODIR)))

(defun wl-texinfo-install ()
  (cond ((listp wl-info-lang)
	 (mapcar 'wl-texinfo-install-file wl-info-lang))
	((stringp wl-info-lang)
	 (wl-texinfo-install-file wl-info-lang))))

(defun wl-primary-info-file ()
  "Get primary info file (for wl-detect-info-directory)."
  (cond
   ((listp wl-info-lang)
    (let ((wl-info-lang (car wl-info-lang)))
      (wl-primary-info-file)))
   ((stringp wl-info-lang)
    (symbol-value (intern (format "wl-%s-info" wl-info-lang))))))

(defun wl-detect-info-directory ()
  (config-wl-package-subr)
  (if (fboundp 'nemacs-version)
      (error "Cannot format info on Nemacs. Please use another formatter."))
  ;; INFODIR check.
  (require 'info)
  (if (fboundp 'info-initialize)
      (info-initialize))
  (let ((infodir (car command-line-args-left))
	(info (wl-primary-info-file))
	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))
    (setq command-line-args-left (cdr command-line-args-left))
    (princ (format "INFODIR is %s\n\n" INFODIR))
    INFODIR))

(defun install-wl-info ()
  (wl-texinfo-format)
  (wl-texinfo-install))

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

;;; End
