;;==============================================================================
;;; work.el
;;
;;; Emacs Work Settings
;;
;;; Author: Kyle W T Sherman
;;
;; Time-stamp: <2008-07-09 11:27:35 (kyle)>
;;==============================================================================

(message ";;; work --> Start")

;;==============================================================================
;;; Load Modules
;;==============================================================================

(message ";;; work --> Load Work Modules")

;; local paths
(add-to-list 'load-path "~/.elisp/kyle") ; work elisp projects

;; marketdata gateway
(message ";;; work --> marketdata-gateway")
(require-if-available 'marketdata-gateway)
(when (>= emacs-major-version 22)
  (defalias 'mdgq 'marketdata-gateway-query "Run `marketdata-gateway-query'.")
  (defalias 'mdgql `(lambda () (marketdata-gateway-query "localhost"))
    "Run `marketdata-gateway-query' on localhost."))

;;==============================================================================
;;; General Functions
;;==============================================================================

(message ";;; work --> General Functions")

(message ";;; work --> log4-toggle-debug-error")
(defun log4-toggle-debug-error ()
  "Toggle \"DEBUG\" to \"ERROR\" and vice versa on current line."
  (interactive)
  (let ((debug-regexp "\"DEBUG\"")
        (error-regexp "\"ERROR\""))
    (save-excursion
      ;; preserve search data
      (save-match-data
        ;; toggle on current line only
        (goto-char (point-at-bol))
        (if (re-search-forward debug-regexp (point-at-eol) t)
            (progn
              (goto-char (point-at-bol))
              (while (re-search-forward debug-regexp (point-at-eol) t)
                (replace-match "\"ERROR\"" nil nil)))
          (progn
            (goto-char (point-at-bol))
            (while (re-search-forward error-regexp (point-at-eol) t)
              (replace-match "\"DEBUG\"" nil nil))))
        ))))
(global-set-key "\M-t" 'log4-toggle-debug-error) ; defaults to `transpose-words'

(message ";;; work --> find-file-log")
(defun find-file-log (file)
  "Runs `find-file' with FILE, then moves the point to the end of buffer."
  (find-file file)
  (goto-char (point-max)))

;;==============================================================================
;;; Cylon Project
;;==============================================================================

(message ";;; work --> Cylon Project")

;; run setenv or setenv-cylon and copy the results into the emacs environment
(message ";;; work --> marketdata-cylon-setenv")
(defun marketdata-cylon-setenv (&optional file no-dot-classpath)
  "Run FILE script and copy the resulting Cylon environment
settings into the emacs environment.

If FILE is nil, a local setenv is used if found, otherwise
setenv-cylon and .classpath are searched for going up the
directory tree.

If NO-DOT-CLASSPATH is non-nil, .classpath is not used."
  (interactive)
  (let (dot-classpath)                  ; location of .classpath file
    (unless file
      ;; first check for a local setenv file
      (if (file-exists-p "./setenv")
          (setq file "./setenv")
        ;; otherwise, try to find setenv-cylon and .classpath in dir tree
        (let ((dir (directory-file-name (expand-file-name ".")))
              (old-dir "")
              test-file)
          (while (and (not file)
                      (not (equal dir old-dir)))
            ;; look for setenv-cylon
            (setq test-file (concat dir "share/scripts/setenv-cylon"))
            (when (file-exists-p test-file)
              (setq file test-file))
            ;; look for .classpath
            (setq test-file (concat dir ".classpath"))
            (when (file-exists-p test-file)
              (setq dot-classpath test-file))
            (setq dir (file-name-directory (directory-file-name dir)))))))
    ;; list of environment variables that are to be set
    (let ((vars '("JAVA_HOME" "JRE_LIBS" "JRE_SERVER_LIBS"
                  "ANT_HOME" "GS_HOME" "JSHOMEDIR" "MD_HOME"
                  "PATH" "CLASSPATH" "LD_LIBRARY_PATH")))
      (if (file-executable-p file)
          (with-temp-buffer
            (shell-command file t)
            (dolist (var vars)
              (goto-char (point-min))
              (while (re-search-forward (concat "^" var ": ") nil t)
                (message (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
                (setenv var (buffer-substring-no-properties (point) (point-at-eol))))))
        (if (file-exists-p file)
            (error "Could not execute file: %s" file)
          (error "Could not find file: %s" file))))
    ;; set classpath from dot-classpath, if it was found
    (when (and dot-classpath (not no-dot-classpath))
      ;; load dot-classpath xml file into temp buffer
      (let ((base-dir (file-name-directory (directory-file-name dot-classpath)))
            classpath)
        (with-temp-buffer
          (insert-file-contents dot-classpath)
          (goto-char (point-min))
          (while (re-search-forward "kind=\"lib\" path=\"\\(.*\\)\"" nil t)
            (setq classpath (concat classpath ":" base-dir (match-string 1)))))
        (when (> (length classpath) 0)
          (setenv "CLASSPATH" (substring classpath 1)))))
    ;; set jde-global-classpath
    (setq jde-global-classpath (getenv "CLASSPATH"))
    ))

;;==============================================================================
;;; Work Menu
;;==============================================================================

(message ";;; work --> Menu")

;; work menu
(let ((users '(("dj615869" . "")
               ("mdapps" . "/opt/mdapps")
               ("root" . "/")))
      (servers-dev '("sbkdevmdp09" "sbkdevmdp90"))
      (servers-prod '("sbkmdwebp01" "sbkmdwebp02" "sbkmdwebp03" "sbkmdwebp04"
                      "secmdwebp01" "secmdwebp02" "secmdwebp03" "secmdwebp04")))
  (auto-menu
   "Work"
   `(("Dired..."
      ,(auto-menu-dired '(("dj" . "~/svn/MarketData/dj")
                          ("cylon" . "~/svn/MarketData/dj/cylon/trunk")
                          ("cylon-core" . "~/svn/MarketData/dj/cylon/trunk/core")
                          ("pubsub" . "~/svn/MarketData/dj/PubSub/trunk")
                          ("gateway-monitor-posts" . "~/svn/MarketData/dj/cylon/trunk/admin-tools/gateway-monitor/posts")
                          ("webservices" . "~/svn/MarketData/dj/webservices/trunk"))))
     ("Remote..."
      (("DEV..."
        ,(auto-menu-dired-remote users servers-dev))
       ("PROD..."
        ,(auto-menu-dired-remote users servers-prod))
       ("Coding..."
        (("sbkdevmdp09"
          ,(auto-menu-dired '(("dj" . "/dj615869@sbkdevmdp09:/home/dj615869/svn/MarketData/dj")
                              ("cylon" . "/dj615869@sbkdevmdp09:/home/dj615869/svn/MarketData/dj/cylon/trunk")
                              ("cylon-core" . "/dj615869@sbkdevmdp09:/home/dj615869/svn/MarketData/dj/cylon/trunk/core")
                              ("pubsub" . "/dj615869@sbkdevmdp09:/home/dj615869/svn/MarketData/dj/PubSub/trunk")
                              ;; ("beanfactory" . "/dj615869@sbkdevmdp09:/home/dj615869/svn/MarketData/dj/cylon/trunk/beanFactory/src/com/dowjones/marketdata/beanfactory")
                              ;; ("datadictionary" . "/dj615869@sbkdevmdp09:/home/dj615869/svn/MarketData/dj/cylon/trunk/datadictionary/src/com/dowjones/marketdata")
                              ;; ("marketdata" . "/dj615869@sbkdevmdp09:/home/dj615869/svn/MarketData/dj/cylon/trunk/marketdata/src/com/dowjones/marketdata/marketdata")
                              ("webservices" . "/dj615869@sbkdevmdp09:/home/dj615869/svn/MarketData/dj/webservices/trunk"))))))))
     ("Load..."
      (("Log Files..."
        (("sbkmdlogp01:webservices" "(find-file-log \"/mdapps@sbkmdlogp01:/opt/mdapps/logs/webservices.log\")"
          "Load `/mdapps@sbkmdlogp01:/opt/mdapps/logs/webservices.log' file.")
         ("secmdlogp01:webservices" "(find-file-log \"/mdapps@secmdlogp01:/opt/mdapps/logs/webservices.log\")"
          "Load `/mdapps@secmdlogp01:/opt/mdapps/logs/webservices.log' file.")))))
     ("Gateway Monitor Post Files..."
      ,(auto-menu-file-dir "~/svn/MarketData/dj/cylon/trunk/admin-tools/gateway-monitor/posts" "-post$" "find-file" t))
     ("Query Gateway" "(marketdata-gateway-query)" "Query a MarketData Gateway.")
     ("Set Cylon Environment" "(marketdata-cylon-setenv)" "Set MarketData Cylon Environment.")
     ("Toggle Log4 DEBUG/ERROR" "(log4-toggle-debug-error)" "Toggle between \"DEBUG\" and \"ERROR\" on current line.")
     )))

(message ";;; work --> End")

;;==============================================================================
;;; End of File
;;==============================================================================