Made jira executable
[clearscm.git] / bin / setbg
index 93a339a..a1f71d1 100755 (executable)
--- a/bin/setbg
+++ b/bin/setbg
@@ -32,7 +32,7 @@ $Date: 2012/11/09 15:31:30 $
 
  Usage: setbg [-u|sage] [-h|elp] [-ve|rbose] [-d|ebug] [-s|leep <n>]
               [-bgdirs <bgdir> -bgdirs ...]
 
  Usage: setbg [-u|sage] [-h|elp] [-ve|rbose] [-d|ebug] [-s|leep <n>]
               [-bgdirs <bgdir> -bgdirs ...]
+
  Where:
 
  -u|sage:      Displays this usage
  Where:
 
  -u|sage:      Displays this usage
@@ -63,16 +63,17 @@ contents of the bgdirs incase it has changed and display a new image". This is
 useful for script to be able to alert setbg that something has changed. For
 example, a script named rmbg might look at ~/.setbg to get the name of the
 current background image file and remove it then signal setbg with SIGUSR1 to
 useful for script to be able to alert setbg that something has changed. For
 example, a script named rmbg might look at ~/.setbg to get the name of the
 current background image file and remove it then signal setbg with SIGUSR1 to
-have it re-evaluate the state of bgdirs. 
+have it re-evaluate the state of bgdirs.
 
 Finally setbg will perform the this re-evaluation at midnight everyday. This is
 
 Finally setbg will perform the this re-evaluation at midnight everyday. This is
-useful because we point setbg to look at -bgdirs from Dropbox where Camera 
+useful because we point setbg to look at -bgdirs from Dropbox where Camera
 Uploads is included and new pictures can arrive everyday.
 
 =cut
 
 use strict;
 use warnings;
 Uploads is included and new pictures can arrive everyday.
 
 =cut
 
 use strict;
 use warnings;
+use experimental qw(signatures);
 
 use FindBin;
 use Getopt::Long;
 
 use FindBin;
 use Getopt::Long;
@@ -130,12 +131,14 @@ sub fillPictures () {
   $totals{bgdirs} = 0;
 
   for (@{$opts{bgdirs}}) {
   $totals{bgdirs} = 0;
 
   for (@{$opts{bgdirs}}) {
-    my ($status, @pics) = Execute "find \"$_\" -type f";
+    my ($status, @pics) = Execute "find \"$_/\" -type f";
 
     chomp @pics;
 
     push @images, grep(/jpg$|png$|gif$/i, @pics);
 
 
     chomp @pics;
 
     push @images, grep(/jpg$|png$|gif$/i, @pics);
 
+    @pics = grep(/jpg$|png$|gif$/i, @pics);
+
     push @{$opts{bgdircnt}}, scalar @pics;
 
     $totals{bgdirs}++;
     push @{$opts{bgdircnt}}, scalar @pics;
 
     $totals{bgdirs}++;
@@ -143,30 +146,34 @@ sub fillPictures () {
 
   $totals{images} = scalar @images;
 
 
   $totals{images} = scalar @images;
 
+  displayStats;
+
   return @images;
 } # fillPictures
   return @images;
 } # fillPictures
-sub writeHistory($) {
-  my ($msg) = @_;
 
 
+sub writeHistory($msg) {
   open my $hist, '>>', "$ENV{HOME}/.$FindBin::Script.hist"
     or error "Unable to open $ENV{HOME}/.$FindBin::Script.hist for append - $!", 1;
 
   $msg = localtime() . $msg;
 
   open my $hist, '>>', "$ENV{HOME}/.$FindBin::Script.hist"
     or error "Unable to open $ENV{HOME}/.$FindBin::Script.hist for append - $!", 1;
 
   $msg = localtime() . $msg;
 
-  display $msg, $hist;
+  print $hist $msg;
 
   close $hist;
 } # writeHistory
 
 
   close $hist;
 } # writeHistory
 
-sub updateSetBG($$) {
-  my ($bgimage, $lockimage) = @_;
+sub writeSetBG ($filename, $image) {
+  open my $file, '>', $filename
+    or error "Unable to open $filename for writing - $!", 1;
 
 
-  open my $setbg, '>', "$ENV{HOME}/.$FindBin::Script"
-    or error "Unable to open $ENV{HOME}/.$FindBin::Script for writing - $!", 1;
+  print $file "$image\n";
 
 
-  display $bgimage, $setbg;
+  close $file;
+} # writeSetBG
 
 
-  close $setbg;
+sub updateSetBG($bgimage, $lockimage) {
+  writeSetBG "$ENV{HOME}/.$FindBin::Script", $bgimage;
+  writeSetBG "$ENV{HOME}/.$FindBin::Script.lock", $lockimage;
 
   my $msg  = ":$bgimage";
      $msg .= " lock:$lockimage" if $opts{lockscreen};
 
   my $msg  = ":$bgimage";
      $msg .= " lock:$lockimage" if $opts{lockscreen};
@@ -199,6 +206,7 @@ GetOptions (
   'sleep=i',
   'lockscreen',
   'bgdirs=s@',
   'sleep=i',
   'lockscreen',
   'bgdirs=s@',
+  'mate',
 ) || Usage;
 
 local $0 = "$FindBin::Script " . join ' ', @argvCopy;
 ) || Usage;
 
 local $0 = "$FindBin::Script " . join ' ', @argvCopy;
@@ -220,8 +228,16 @@ for (my $i = 0; $i < scalar @{$opts{bgdirs}}; $i++) {
 
 # Using gsettings
 my $setbg       = "gsettings";
 
 # Using gsettings
 my $setbg       = "gsettings";
-my $setbgOpts   = "set org.gnome.desktop.background picture-uri \"file://";
-my $setLockOpts = "set org.gnome.desktop.screensaver picture-uri \"file://";
+
+my ($setbgOpts, $setLockOpts);
+
+if ($opts{mate}) {
+  $setbgOpts   = 'set org.mate.background picture-filename ';
+  $setLockOpts = 'set org.mate.screensaver picture-filename ';
+} else {
+  $setbgOpts   = "set org.gnome.desktop.background picture-uri \"file://";
+  $setLockOpts = "set org.gnome.desktop.screensaver picture-uri \"file://";
+} # if
 
 my @images = fillPictures;
 
 
 my @images = fillPictures;
 
@@ -237,18 +253,28 @@ truncate "$ENV{HOME}/.$FindBin::Script.hist", 0;
 
 EnterDaemonMode unless defined $DB::OUT;
 
 
 EnterDaemonMode unless defined $DB::OUT;
 
+my $pickNewImages = 1;
+my ($bgimage, $lockimage);
+
 while () {
 while () {
-  my $bgimage   = escapeHTML ($images[int (rand $#images)]);
-  my $lockimage = escapeHTML ($images[int (rand $#images)]);
+  if ($pickNewImages) {
+    $bgimage   = escapeHTML ($images[int (rand $#images)]);
+    $lockimage = escapeHTML ($images[int (rand $#images)]);
+  } # if
 
   my $monitorIsOn;
 
   my ($status, @output) = Execute("xset q | grep Monitor | awk '{print \$3}'");
 
   if ($status or $output[0] eq 'Off') {
 
   my $monitorIsOn;
 
   my ($status, @output) = Execute("xset q | grep Monitor | awk '{print \$3}'");
 
   if ($status or $output[0] eq 'Off') {
-    writeHistory ":Monitor off, not setting background to $bgimage";
+    writeHistory ":Monitor off, not setting background to $bgimage - will keep trying";
+
+    $pickNewImages = 0;
   } else {
   } else {
-    my $cmd = "$setbg $setbgOpts$bgimage\" 2> /dev/null";
+    $pickNewImages = 1;
+
+    my $cmd = $opts{mate} ? "$setbg $setbgOpts\"$bgimage\" 2> /dev/null"
+                          : "$setbg $setbgOpts$bgimage\" 2> /dev/null";
 
     ($status, @output) = Execute $cmd;
 
 
     ($status, @output) = Execute $cmd;
 
@@ -261,7 +287,8 @@ while () {
     } # if
 
     if ($opts{lockscreen}) {
     } # if
 
     if ($opts{lockscreen}) {
-      $cmd = "$setbg $setLockOpts$lockimage\" 2> /dev/null";
+      $cmd = $opts{mate} ? "$setbg $setLockOpts\"$lockimage\" 2> /dev/null"
+                         : "$setbg $setLockOpts$lockimage\" 2> /dev/null";
 
       ($status, @output) = Execute $cmd;
 
 
       ($status, @output) = Execute $cmd;
 
@@ -275,10 +302,10 @@ while () {
     } # if
 
     updateSetBG $bgimage, $lockimage;
     } # if
 
     updateSetBG $bgimage, $lockimage;
-
-    displayStats;
   } # if
 
   } # if
 
+  displayStats;
+
   $today = YMD;
 
   sleep $opts{sleep} * 60;
   $today = YMD;
 
   sleep $opts{sleep} * 60;