;; -*- emacs-lisp -*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; $Id: publish-init.el,v 1.5 2006/03/06 12:07:52 ole Exp $
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; publish init elisp files to html files 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; need a special one
(load (expand-file-name "htmlize.el" "/usr/share/emacs/site-lisp/htmlize/"))
(add-hook 'htmlize-before-hook 'font-lock-fontify-buffer)

;;;###autoload
(defun htmlize-all-files-in-directory (dir &optional targetdir)
  "htmlize all *.el files in a directory and write them to the target directory
the target directory is optional and defaults to te source directory."
  (interactive "D")
  (let ((files (directory-files dir t "^[^.]+\\.el$" nil)))
    (while files
      (let ((file (car files)))
        (htmlize-file file targetdir))
      (setq files (cdr files)))))

;;;###autoload
(defun copy-files-to-directory (dir targetdir &optional pattern)
  "copy all files from dir to a target directory"
  (interactive "DDirectory: \nDTarget Directory: ")
  (let ((files (directory-files dir t (or pattern  "^[^.]+.*$") nil)))
    (while files
      (let ((file (car files)))
        (if (file-regular-p file)
            (copy-file file (expand-file-name (file-name-nondirectory file) targetdir) t)))
      (setq files (cdr files)))))

;;;###autoload
(defun delete-directory-recursively (dir)
  "delete a directory recursively"
  (interactive "DDirectory: ")
  (if (and (file-exists-p dir) (file-directory-p dir))
      (let ((files (directory-files dir t )))
        (while files
          (let* ((file (car files))
                 (basename (file-name-sans-extension (file-name-nondirectory file))))
            (unless (or (string= basename ".") (string= basename ".."))
              (cond ((or (file-regular-p file)
                         (file-symlink-p file))
                     (delete-file file))
                    ((file-directory-p file)
                     (delete-directory-recursively file))))
            (setq files (cdr files))))
        (delete-directory dir))))


;;;###autoload
(defun publish-init-files (target-directory)
  "Publish all init files to the sugarshark website.
Makes HTML out of the elisp files and copies them to the 
sugarshark development website. This creates two subdirectories: elisp and downloads."
  (interactive
   (list (read-directory-name "Website Root: "  emacs-wiki-publishing-directory emacs-wiki-publishing-directory)))
  (let* ((init-file (expand-file-name "init.el" emacs-init-dir))
         (elisp-target-directory (expand-file-name "elisp/" target-directory))
         (init-target-file (expand-file-name "init.el" elisp-target-directory))
         (init-target-dir (expand-file-name "init/" elisp-target-directory))
         (mylisp-target-dir (expand-file-name "mylisp/" elisp-target-directory)))

     ;; clean and remake the necessary dirs
    (delete-directory-recursively init-target-dir)
    (delete-directory-recursively mylisp-target-dir)
    (make-directory init-target-dir t)
    (make-directory mylisp-target-dir t)

    ;; first publish init file
    (copy-file init-file init-target-file t)
    
    ;; now all init files
    (copy-files-to-directory init-lisp-dir init-target-dir "^[^.]+\\.el$")

    ;; and all homegrown lisp files
    (copy-files-to-directory emacs-mylisp-dir mylisp-target-dir "^[^.]+\\.el$")

    ;; make a tar
    (shell-command (concat "/bin/tar -C " elisp-target-directory " -czf "
                           (expand-file-name "emacs-config.tar.gz " elisp-target-directory)
                           "init.el init/ mylisp/"))

    ;; now htmlize all files
    (htmlize-file init-target-file)
    (save-excursion
      (with-temp-buffer
        (find-file (concat init-target-file ".html"))
        (goto-char (point-min))
        (replace-regexp "      <span class=\"string\">\"\\(.+?\\)\"</span>"
                        "      <span class=\"string\">\"<a href=\"init/\\1.el.html\">\\1</a>\"</span>")
        (save-buffer)
        (kill-buffer (current-buffer))))

    (htmlize-all-files-in-directory init-target-dir)
    (htmlize-all-files-in-directory mylisp-target-dir)
    ))

;;;###autoload
(defun publish-website ()
  "Publish all to the web site"
  (interactive)
  (shell-command (concat "cd " emacs-wiki-publishing-directory " && ( "
                         "/bin/tar -c -v -N .publish-stamp --exclude='*/CVS/*' --exclude='./.*' "
                         "--exclude='*~' -f - . |"
                         "ssh sugarshark.com \"tar -x -C /www -f -\" ) && "
                         "touch .publish-stamp && echo \"Published site.\" &")))

(provide 'publish-init)