
; randback.jl by Aaron Lehmann <aaronl@vitelus.com>
; Thanks to Rahsheen Porter for his Background hack, upon which this was based.

; Sawmill does not have support for backgrounds. According to the authors,
; this is a good thing, keeping sawmill as strictly a window manager.
; For people like me who don't want to use a "desktop environment", it is more
; difficult to set backgrounds in a sane way.
;
; I don't like chosing a background - I want to rotate them out and see new ones
; every day. The way I prefer to do this is by chosing a random image every hour
; or so. The problem that arises is that no program that I use has this support.
; Well, I'm a hacker. I can write it in C or perl without too much difficulty.
; But the problem with this is deciding how the script should be run. It could
; be a program that stays open for the duration of an X session, which would
; mean having another process in existance that consumes resources, especially
; if the program was writen in perl, which would require that a perl interpreter
; be kept in memory. The script or program could be run from a cron job, but
; this introduces all kinds of headaches involving X authentication that I would
; not want to touch. I have concluded that the solution is to let my window
; manager rotate the backgrounds. With most other window managers, this would
; not be an option, but thanks to sawmill's extensibility I was able to hack
; this file up. IMHO it makes it a lot less resource-intensive and painful to
; display a random background every X seconds.


; Usage: copy this file (randback.jl) to ~/.sawmill add these to ~/.sawmillrc

;  (load "~/.sawmill/randback.jl") ; Load randback
;  (randback) ; Set the background on startup

; Suggestion for background textures: grab the propaganda-all tarball.

; End of documentation - you don't want to read on unless you like to read LISP.

; Patched to work with sawfish1.3 by M.R.Muthu kumar(m_muthukumar@users.sf.net)


(require 'rep.io.timers)
 (provide 'randback)

(defun cut-last-two (lst)
  (reverse (cdr (cdr (reverse lst)))))

(defun basename (path)
  "Returns just the file name. No path or extension (thanks oGMo)"
  (let ((name (file-name-nondirectory path)))
    (if (string-match "\\.[^.]+$" name)
	(substring name 0 (match-start))
      name)))

(defun read-files-recursively (directory)
  (if (file-directory-p directory)
      (cons directory
	    (mapcar 
	     (lambda (file)
	       (let ((path-to-file (concat directory "/" file)))
		 (cond ((file-directory-p path-to-file) (read-files-recursively path-to-file))
		       (t path-to-file))))
	     (cut-last-two (directory-files directory))))))

(defun read-files-path (pathlist)
  (mapcar 
   (lambda (path)
     (read-files-recursively path))
   pathlist))

(defun set-background (path)
  (cond ((null path) nil)
	(t (system (concat background-set-app " " path " &")))))



;; CUSTOMIZATION STUFF
(defgroup background "Background")

(defcustom background-image-path (concat (user-home-directory) ".backgrounds")
  "Full pathname to directory to choose background from"
  :type string
  :group background)

(defcustom background-set-app "Esetroot"
  "Application to set desktop background"
  :type program-name
  :group background)

(defvar randback-timer nil)

(defun randback-interval-changed ()
  "Called when the interval changes"
  (set-timer randback-timer randback-interval 0))

(defcustom randback-interval 3600
  "Number of seconds before desktop background is changed"
  :type number
  :group background
  :after-set randback-interval-changed)

(defun randback ()
  "Sets a random background"
  (let ((file-list (cdr (car (read-files-path (list background-image-path))))))
  (set-background (nth (random (length file-list)) file-list))))

(defun randback-timer-func (unused-timer)
  "This function is called by the asynchronus timer"
  (randback)
  (set-timer randback-timer))

(random t) ; Seed PRNG
(setq randback-timer (make-timer randback-timer-func randback-interval 0))
