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