bin/disorder-notify: New operation to cycle through configurations.
[profile] / bin / disorder-notify
index e67668f..d26a565 100755 (executable)
@@ -283,6 +283,31 @@ $OP{"notify-now-playing"} = sub {
   defined locked_by or watch_and_notify 0;
 };
 
+$OP{"next-config"} = sub {
+  (my $dir = $C{config}) =~ s:/[^/]*$::;
+  my (@conf, $curr, $conf, $min);
+
+  if (-l $C{config} && (my $t = readlink $C{config}) =~ /^passwd\.(.*)$/)
+    { $curr = $1; }
+
+  opendir my $dh, +$dir;
+  FILE: while (my $f = readdir $dh)
+    { push @conf, $1 if $f =~ /^passwd\.(.*[^~])$/; }
+
+  for (my $i = 0; $i < @conf; $i++) {
+    $min = $conf[$i] if (!defined $min) || $conf[$i] lt $min;
+    $conf = $conf[$i]
+      if ((!defined $curr) || $curr lt $conf[$i]) &&
+        ((!defined $conf) || $conf[$i] lt $conf);
+  }
+  $conf = $min unless defined $conf;
+
+  try_unlink "$dir/passwd.new";
+  symlink "passwd.$conf", "$dir/passwd.new";
+  rename "$dir/passwd.new", "$dir/passwd";
+  notify "DisOrder configuration", "Switched to `$conf'";
+};
+
 ###--------------------------------------------------------------------------
 ### Main program.