 ;;; install (if not yet) and load packages
 (defvar *package-names*
   '(
-	;; first things first
-	"async"
-	"exec-path-from-shell"
+    ;; first things first
+    "async"
+    "exec-path-from-shell"
 
     ;; color themes
     "solarized-theme"

    "use-package"
    "runner"
    "hideshow-org"
	"popwin"
	"direx"
	"point-undo"
	"eval-sexp-fu"
	"0blayout"

    ;; auto complete mode
    "auto-complete"
	"ac-c-headers"
	"ac-capf"							; completion at point
	"ac-clang"
	"ac-cider"
	"ac-emoji"
    "ac-helm"
    "ac-math"
    "ac-ispell"

    "ess-R-object-popup"
    "ess-smart-equals"

    ;; Markdown
    "markdown-mode"
    "markdown-toc"

    ;; misc
    "auctex"
    "engine-mode"
    "nasm-mode"
    "x86-lookup"
    "gist"
    "hackernews"
    "zencoding-mode"
    ))

(defun load-file-if-exists (file)
  "Load FILE when (file-exists-p FILE); otherwise no-op."
  (let ((file (expand-file-name file)))
    (when (file-exists-p file)
      (load-file file))))

(load-file-if-exists "~/.emacs.d/config/pre-package-load.el")

(let ((dir (expand-file-name "~/.emacs.d/config/pre-package-load.d/")))
  (when (file-accessible-directory-p dir)
    (dolist (f (directory-files dir t "\\.el$"))
      (load-file-if-exists f))))

(let ((refreshed-p nil))
  (dolist (package-name *package-names*)
    (let ((package (intern package-name))
          (package-config-path (format "~/.emacs.d/config/%s.el" package-name)))
      (unless (package-installed-p package)
        (unless refreshed-p
          (package-refresh-contents)
          (setq refreshed-p t))
        (package-install package))
      (load-file-if-exists package-config-path))))

(load-file-if-exists "~/.emacs.d/config/post-package-load.el")

(let ((dir (expand-file-name "~/.emacs.d/config/post-package-load.d/")))
  (when (file-accessible-directory-p dir)
    (dolist (f (directory-files dir t "\\.el$"))
      (load-file-if-exists f))))

(load-file-if-exists "~/.emacs.local")
(custom-set-variables
