Added additional reporting to setbg
[clearscm.git] / bin / setbg
1 #!/usr/bin/perl
2
3 =pod
4
5 =head1 NAME $RCSfile: setbg,v $
6
7 Set background
8
9 =head1 VERSION
10
11 =over
12
13 =item Author
14
15 Andrew DeFaria <Andrew@ClearSCM.com>
16
17 =item Revision:
18
19 $Revision: 1.10 $
20
21 =item Created:
22
23 Fri Mar 18 01:14:38 PST 2005
24
25 =item Modified:
26
27 $Date: 2012/11/09 15:31:30 $
28
29 =back
30
31 =head1 SYNOPSIS
32
33  Usage: setbg [-u|sage] [-ve|rbose] [-d|ebug] [-s|leep <n>] [-bgdir <bgdir>]
34  
35  Where:
36
37  -u|sage:     Displays this usage
38  -ve|rbose:   Be verbose
39  -d|ebug:     Output debug messages
40
41  -s|leep:     Number of minutes to sleep between setting the background
42              (Default: 1 hour)
43  -b|gdir:     Directory to scan for images (Default: /web/Pictures)
44
45 =head1 DESCRIPTION
46
47 This script sets the background image randomly based on images $imgDir. Note
48 if this script is run again it senses that it was previously run and sends the
49 previous script a SIGUSR2 which the script intrprets as "Change the background
50 now", then exits.
51
52 =cut
53
54 use strict;
55 use warnings;
56
57 use FindBin;
58 use Getopt::Long;
59 use Proc::ProcessTable;
60 use File::Spec;
61 use CGI qw/:standard/;
62
63 use lib "$FindBin::Bin/../lib";
64
65 use DateUtils;
66 use Display;
67 use Logger;
68 use Utils;
69
70 my $VERSION  = '$Revision: 1.10 $';
71   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
72
73 my $processes = new Proc::ProcessTable;
74 my @imgDirs;
75 my %totals;
76
77 foreach my $process (@{$processes->table}) {
78   if ($process->cmndline =~ /setbg/ and
79       $process->pid != $$) { 
80     kill 12, $process->pid;
81
82     exit 0;
83   } # if
84 } # foreach
85
86 sub displayStats () {
87   my $statsFile = Logger->new(
88     name      => ".$FindBin::Script.stats",
89     path      => $ENV{HOME},
90     extension => '',
91   );
92
93   $statsFile->log('At ' . localtime());
94
95   Stats \%totals, $statsFile;
96
97   return;
98 } # displayStats
99
100 sub fillPictures () {
101   my @images;
102
103   foreach (@imgDirs) {
104     my @pics = `find "$_" -type f -name "*.jpg"`;
105
106     chomp @pics;
107     push @images, @pics;
108   } # foreach
109
110   $totals{images} = scalar @images;
111
112   displayStats;
113
114   return @images;
115 } # fillPictures
116
117 sub updateSetBG ($) {
118   my ($image) = @_;
119
120   open my $log, '>', "$ENV{HOME}/.$FindBin::Script"
121     or error "Unable to open $ENV{HOME}/.$FindBin::Script for writing - $!", 1;
122
123   display $image, $log;
124
125   close $log;
126
127   open $log, '>>', "$ENV{HOME}/.$FindBin::Script.hist"
128     or error "Unable to open $ENV{HOME}/.$FindBin::Script.hist for append - $!", 1;
129
130   my $msg = localtime() . ":$image";
131
132   display $msg, $log;
133
134   close $log;
135
136   return;
137 } # updateSetBG
138
139 $0 = "$FindBin::Script " . join ' ', @ARGV;
140
141 verbose "$FindBin::Script v$VERSION";
142
143 my $sleep  = 60 * 60;
144
145 GetOptions (
146   'usage'   => sub { Usage },
147   'verbose' => sub { set_verbose },
148   'debug'   => sub { set_debug },
149   'sleep=i' => \$sleep,
150   'bgdir=s' => \@imgDirs,
151 ) || Usage;
152
153 for (my $i = 0; $i < scalar @imgDirs; $i++) {
154   error "$imgDirs[$i] is not a directory", 1 unless -d $imgDirs[$i];
155
156   $imgDirs[$i] = File::Spec->rel2abs ($imgDirs[$i]);
157 } # foreach
158
159 # Using gsettings
160 my $setbg     = "gsettings";
161 my $setbgOpts= "set org.gnome.desktop.background picture-uri \"file://";
162
163 my @images = fillPictures;
164
165 Usage "No images to display. Must specify -bgdir" unless @images;
166
167 sub SwitchWallPaper {
168   # We don't need to do anything here, just handle the interrupt and
169   # let the while loop continue.
170   debug 'SwitchWallPaper: Interrupt received';
171   displayStats;
172 } # SwitchWallPaper
173
174 $SIG{USR2} = \&SwitchWallPaper;
175 $SIG{USR1} = \&fillPictures;
176
177 my $debugger = $DB::OUT;
178 my $today;
179
180 truncate "$ENV{HOME}/.$FindBin::Script.hist", 0;
181
182 EnterDaemonMode unless defined $DB::OUT;
183
184 while () {
185   my $image = escapeHTML ($images[int (rand $#images)]);
186
187   my $cmd = "$setbg $setbgOpts$image\" 2> /dev/null";
188
189   my @output = `$cmd`;
190
191   if ($? != 0) {
192     error "Trying to set background - command used \"$cmd\"\n\nOutput\n\n" . 
193       join "\n", @output;
194     $totals{errors}++;
195   } else {
196     $totals{'Images displayed'}++;
197
198     updateSetBG $image;
199   } # if
200
201   $today = YMD;
202
203   sleep $sleep;
204
205   if ($today ne YMD){
206     @images = fillPictures;
207   } # if
208 } # while