;; -*- emacs-lisp -*-
;;; emacs-mst-laptop.el --- laptop related code

;; Author: Mark Triggs <mst@dishevelled.net>
;; $Id: emacs-mst-laptop.el,v 1.27 2004/01/15 05:11:08 mst Exp $

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;;; Code:

(defun host-alive-p (host)
  "Determine whether a host is alive"

  (= (shell-command
      (concat "ping -q -c1 " host " > /dev/null 2> /dev/null")) 0))

(defun mounted-p (target)
  "Is target mounted?"

  (with-current-buffer (find-file-noselect "/etc/mtab" t)
    (goto-char (point-min))
    (prog1
        (block find-mount
          (while (not (eobp))
            (when (string-match (concat " ?" target " ")
                                (thing-at-point 'line))
              (return-from find-mount t))
            (next-line 1)))
      (kill-buffer nil))))

(defun mount-home ()
  (redraw-display)
  (unless (eq (shell-command (concat
                              "/home/mst/.bin/mount-home "
                              (read-passwd "Samba password?: ")))
              0)
    (error "Couldn't mount home directory")))

(defun unmount-home ()
  (unless (eq (shell-command
               "umount ~/main") 0)
    (message "Couldn't unmount home directory")))


(defun gnus-start ()
  "Start gnus."
  (interactive)

  (cond ((or (host-alive-p "thweeble") (mounted-p (expand-file-name "~/main")))
         (when (not (mounted-p (expand-file-name "~/main")))
           (mount-home)
           (add-hook 'gnus-exit-gnus-hook 'unmount-home))
         (add-hook 'gnus-started-hook (lambda ()
                                        (gnus-group-check-bogus-groups)
                                        (let ((gnus-subscribe-newsgroup-method
                                               'gnus-subscribe-topics))
                                          (gnus-group-find-new-groups))))
         (when (file-exists-p "~/.news/agent/nnml/main/agent.lib")
           ;; Synchronise our marks using my fangled script
           (shell-command "~/.bin/gnus-sync"))
         (gnus))
        (t (add-hook 'gnus-started-hook (lambda () (gnus-agent-toggle-plugged
                                                    nil)
                                          (gnus-agent-regenerate)
                                          (gnus-group-get-new-news)))
           (gnus))))

;; No mouse here
(global-unset-key [mouse-1])
(global-unset-key [down-mouse-1])

(provide 'emacs-mst-laptop)
;;; emacs-mst-laptop.el ends here
