#!/usr/bin/perl -w # # chad c d clark < chad . clark _AT_ gmail _DOT_ com > # created: 2006-08-18 # $Id$ use strict; use Data::Dumper; use XML::Simple; use File::Basename; use File::Copy; use File::Finder; use File::Find; use Net::FTP; # -- Gobals -------------------------------------------------- # set $DEBUG to non-zero for printed debug output. my $DEBUG = 0; my $username = getpwuid($<); my $home_dir = (getpwnam($username))[7]; my $BACKUP_FNAME = "$home_dir/.wp/wallpaper-previous-image"; my $FTP_SERVER = 'ftp.mydomain.com'; my $FTP_USER = 'myusername'; my $FTP_PASS = 'mypassword'; # -- Subroutines --------------------------------------------- sub dbprint(@); sub get_bg_image_fname($$); # -- main() -------------------------------------------------- # GOAL : get the current background image file name for current user. my $cur_fname; get_bg_image_fname($username, \$cur_fname) or die "$0: unable to get background image file name. '$!'\n"; # GOAL : if the image is the same as the last image exit silently. my $cmd = "diff $cur_fname $BACKUP_FNAME"; `$cmd`; if ((( $? >> 8) & 0xFF) == 0) { dbprint("$0: nothing to do because image has not changed. $cur_fname $BACKUP_FNAME\n"); exit(0); } # GOAL : copy the image and make a second, smaller version. # make a temporary working directory my $tmp_dir = "/tmp/wallpaper_copy_$$/"; mkdir $tmp_dir, 0755 or die "$0: unable to create temp dir. '$tmp_dir'\n"; # DOC : the main part of the script is wrapped in eval so we can remove # the temp dir if something goes wrong. eval { my $file_ext = ""; if ($cur_fname =~ m/\.([a-zA-Z]+)$/) { $file_ext = $1; } dbprint("file extension: $file_ext"); my $now = time(); my $large_fname = $tmp_dir . $now . "_large"; my $small_fname = $tmp_dir . $now . "_small"; if ($file_ext) { $large_fname .= ".$file_ext"; $small_fname .= ".$file_ext"; } dbprint("large filename: $large_fname"); dbprint("small filename: $small_fname"); copy($cur_fname, $large_fname) or die "$0: unable to copy '$cur_fname' to '$large_fname' $!\n"; $cmd = "convert $large_fname -resize 600x480 $small_fname"; dbprint("executing: " . $cmd); `$cmd`; # we check return code of $cmd. (the man page does not comment on the # exit status but it seems to return zero when it works.) unless ((( $? >> 8) & 0xFF) == 0) { dbprint("$0: failed to resize image. '$cmd'\n"); exit(0); } # GOAL : transfer the files to the web server chmod 0644, $large_fname, $small_fname; my $ftp = Net::FTP->new($FTP_SERVER, Port => 21, Debug => 0) or die "$0: unable to connect to $FTP_SERVER: $@"; $ftp->login("$FTP_USER",$FTP_PASS) or die "$0: unable to login to FTP server ", $ftp->message; $ftp->binary() or die "$0: unable to use BINARY mode for FTP ", $ftp->message; $ftp->put($large_fname) or die "$0: unable to FTP PUT file. '$large_fname'\n"; $ftp->put($small_fname) or die "$0: unable to FTP PUT file. '$small_fname'\n"; $ftp->quit; # GOAL : save a copy of the original image so it can be compared against # to see when the image changes again. copy($cur_fname, $BACKUP_FNAME) or die "$0: unable to copy '$cur_fname' to '$BACKUP_FNAME' $!\n"; }; warn $@ if $@; # GOAL : cleanup our temporary working directory # If you know a nice way to do "rm -rf $dir" in perl let me know. - Chad # recursively remove all files my @names = File::Finder->type('f')->in($tmp_dir); foreach (@names) { unlink $_ or warn "$0: unable to unlink '$_'\n" } # recursively remove all directories @names = File::Finder->type('d')->in($tmp_dir); foreach (@names) { rmdir $_ or warn "$0: unable to rmdir '$_'\n" } exit 0; # -- Subroutines --------------------------------------------- sub dbprint(@) { # prints the input arguments when global $DEBUG is non-zero. if ($DEBUG) { while (my $line = shift) {print "DB: ", $line, "\n"}; } } sub get_bg_image_fname($$) { # returns the filename of the gnome background image. # - $username is the username as found in /etc/passwd # - $fname_ref is a reference to the scalar to store the filename in. # eg: get_bg_image_fname(getpwuid($<), \$cur_fname) or die "ahhh"; my $username = shift; my $fname_ref = shift; dbprint("getting bg image fname for username '$username'"); my $home_dir = (getpwnam($username))[7]; dbprint("home dir for '$username' is '$home_dir'"); my $xml_fname = $home_dir . "/.gconf/desktop/gnome/background/%gconf.xml"; dbprint("xml file is '$xml_fname'"); my $mXML = XMLin($xml_fname, forcearray => 1); # dbprint(Dumper($mXML)); my $img_fname = $mXML->{entry}{picture_filename}{stringvalue}[0]; dbprint("bg image file name is '$img_fname'"); $$fname_ref = $img_fname; 1; }