09311f582ebf069f2b65488942a5879abfea4700
[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 Utils;
68
69 my $VERSION  = '$Revision: 1.10 $';
70   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
71
72 my $processes = new Proc::ProcessTable;
73 my @imgDirs;
74
75 foreach my $process (@{$processes->table}) {
76   if ($process->cmndline =~ /setbg/ and
77       $process->pid != $$) { 
78     kill 12, $process->pid;
79
80     exit 0;
81   } # if
82 } # foreach
83
84 sub fillPictures () {
85   my @images;
86   
87   foreach (@imgDirs) {
88     my @pics = `find "$_" -type f -name "*.jpg"`;
89     
90     chomp @pics;
91     push @images, @pics;
92   } # foreach
93
94   return @images;  
95 } # fillPictures
96
97 $0 = "$FindBin::Script " . join ' ', @ARGV;
98
99 verbose "$FindBin::Script v$VERSION";
100
101 my $sleep  = 60 * 60;
102
103 GetOptions (
104   'usage'   => sub { Usage },
105   'verbose' => sub { set_verbose },
106   'debug'   => sub { set_debug },
107   'sleep=i' => \$sleep,
108   'bgdir=s' => \@imgDirs,
109 ) || Usage;
110
111 for (my $i = 0; $i < scalar @imgDirs; $i++) {
112   error "$imgDirs[$i] is not a directory", 1 unless -d $imgDirs[$i];
113   
114   $imgDirs[$i] = File::Spec->rel2abs ($imgDirs[$i]);
115 } # foreach
116
117 # Using gsettings
118 my $setbg           = "gsettings";
119 my $setbgOpts   = "set org.gnome.desktop.background picture-uri \"file://";
120
121 my @images = fillPictures;
122
123 Usage "No images to display. Must specify -bgdir" unless @images;
124  
125 sub SwitchWallPaper {
126   # We don't need to do anything here, just handle the interrupt and
127   # let the while loop continue.
128   debug 'SwitchWallPaper: Interrupt received';
129 } # SwitchWallPaper
130
131 $SIG{USR2} = \&SwitchWallPaper;
132 $SIG{USR1} = \&fillPictures;
133
134 my $debugger = $DB::OUT;
135 my $today;
136
137 EnterDaemonMode unless defined $DB::OUT;
138
139 while () {
140   my $image = escapeHTML ($images[int (rand $#images)]);
141
142   open my $log, '>', "$ENV{HOME}/.$FindBin::Script"
143     or error "Unable to open $ENV{HOME}/.$FindBin::Script for writing - $!", 1;
144
145   display $image, $log;
146
147   my $cmd = "$setbg $setbgOpts$image\" 2> /dev/null";
148
149   my @output = `$cmd`;
150
151   if ($? != 0) {
152     display "ERROR Trying to set background - command used \"$cmd\"", $log;
153     display "Output:";
154     display join "\n", @output, $log;
155   } # if
156   
157   close $log;
158   
159   $today = YMD;  
160   
161   sleep $sleep;
162   
163   if ($today ne YMD){
164     @images = fillPictures;
165   } # if
166 } # while