gnome_bg_rotate


#!/usr/bin/perl -w

use strict;

#
# rotate gnome background to least-recently-used picture
# in named directory
#

use File::stat;

my $picdir;     # picture directory
my @files;      # list of files in directory
my $guy;        # filename
my $sb;         # stat result for file
my $oldtime;    # timestamp on oldest file seen
my $oldguy;     # name of oldest file seen
my $now;        # current time

( $#ARGV == 0 ) || die "Usage: $0 dirname\n";

#
# get list of .jpg files in specified directory
#
$picdir = shift;
opendir( DIR, $picdir ) || die "Can't open $picdir: $!\n";
@files = grep { /\.jpg$/ } readdir(DIR);
closedir(DIR);

#
# find "oldest" file (least recently touched)
#
$oldtime = time();
foreach $guy (@files) {
    $sb = stat("$picdir/$guy");
    if ( $sb->mtime < $oldtime ) {
        $oldtime = $sb->mtime;
        $oldguy  = $guy;
    }
}

#
# get machine ID from /var/lib/dbus/machine-id
#
my $midfile = "/var/lib/dbus/machine-id";
open( MID, "<$midfile" ) || die "Can't open $midfile: $!\n";
chomp( my $machine_id = <MID> );
close(MID);

#
# environment variables are in $HOME/.dbus/session-bus/machine_id-0;
# if this file doesn't exist, just exit, there's no GNOME session
#
my $dbsfile = $ENV{'HOME'} . "/.dbus/session-bus/" . $machine_id . "-0";
exit unless open( DBS, "<$dbsfile" );

#
# dig out and set DBUS_SESSION_BUS_ADDRESS
#
while (<DBS>) {
    /DBUS_SESSION_BUS_ADDRESS=\s*(.*)\s*$/
      && ( $ENV{'DBUS_SESSION_BUS_ADDRESS'} = $1 );
}
close(DBS);

#
# finally, use gconftool-2 to set the background picture...
#
system(
"/usr/bin/gconftool-2 -s /desktop/gnome/background/picture_filename $picdir/"
      . quotemeta($oldguy)
      . " -t string" );

#
# and touch the picture file to accomplish the rotation
#
$now = time;
utime( $now, $now, "$picdir/$oldguy" );