Initial commit
[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
61 use lib "$FindBin::Bin/../lib";
62
63 use Display;
64 use Utils;
65
66 my $VERSION  = '$Revision: 1.10 $';
67   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
68
69 my $processes = new Proc::ProcessTable;
70
71 foreach my $process (@{$processes->table}) {
72   if ($process->cmndline =~ /setbg/ and
73       $process->pid != $$) { 
74     kill 12, $process->pid;
75
76     exit 0;
77   } # if
78 } # foreach
79
80 $0 = "$FindBin::Script " . join ' ', @ARGV;
81
82 verbose "$FindBin::Script v$VERSION";
83
84 my $sleep  = 60 * 60;
85 my $imgDir = $ENV{SETBG_DIR} ? $ENV{SETBG_DIR} : '/web/Pictures';
86
87 GetOptions (
88   'usage'               => sub { Usage },
89   'verbose'             => sub { set_verbose },
90   'debug'               => sub { set_debug },
91   'sleep=i'             => \$sleep,
92   'bgdir=s'             => \$imgDir,
93 ) || Usage;
94
95 error "$imgDir is not a directory", 1 unless -d $imgDir;
96
97 # Using gsettings
98 my $setbg       = "gsettings";
99 my $setbgOpts   = "set org.gnome.desktop.background picture-uri \"file://";
100
101 chomp (my @images = `find $imgDir -type f -name "*.jpg"`);
102
103 sub SwitchWallPaper {
104   # We don't need to do anything here, just handle the interrupt and
105   # let the while loop continue.
106   debug 'SwitchWallPaper: Interrupt received';
107 } # SwitchWallPaper
108
109 $SIG{USR2} = \&SwitchWallPaper;
110
111 my $debugger = $DB::OUT;
112
113 EnterDaemonMode unless defined $DB::OUT;
114
115 while () {
116   my $image = $images[int (rand $#images)];
117
118   open my $log, '>', "$ENV{HOME}/.$FindBin::Script"
119     or error "Unable to open $ENV{HOME}/.setbg for writing - $!", 1;
120
121   display "Current background: $image", $log;
122
123   my $cmd = "$setbg $setbgOpts$image\" 2> /dev/null";
124
125   `$cmd`;
126
127   close $log;
128   
129   sleep $sleep;
130 } # while