From 4080f4e19c260f4e23e6e98e287891277b1ad45f Mon Sep 17 00:00:00 2001
From: Joe <rbo@gmx.us>
Date: Wed, 8 Jan 2025 13:16:51 +0100
Subject: up

---
 .local/bin/setwp | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/.local/bin/setwp b/.local/bin/setwp
index 98938c5..e5fce65 100755
--- a/.local/bin/setwp
+++ b/.local/bin/setwp
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 use File::HomeDir qw(home);
 use File::Copy;
+use File::Find;
 
 use constant {
 	WP_POOL	=> home() . '/pics/wp/'
@@ -12,11 +13,13 @@ use constant FEH_PATH	=> 'feh';
 
 sub get_pool_files
 {
+	my ($pool) = (@_);
 	my @files;
 
-	opendir(DIR, WP_POOL) or die "Couldn't open directory " . WP_POOL . ": $!";
-	@files = grep { !/^\./ } readdir(DIR);
-	closedir(DIR);
+	$pool = WP_POOL if not @_;
+	find(sub {
+			push @files, $File::Find::name if -f;
+		}, $pool);
 	return @files;
 }
 
@@ -27,7 +30,7 @@ sub choose_wp
 	my $wp;
 
 	$rand = int(rand(@files));
-	$wp = WP_POOL . $files[$rand];
+	$wp = $files[$rand];
 	return $wp;
 }
 
@@ -87,8 +90,12 @@ sub main
 	if (@ARGV != 0 && -f $ARGV[0]) {
 		$ret = set_wp($ARGV[0]);
 	}
+	elsif (@ARGV != 0 && -d $ARGV[0]) {
+		$wp = choose_wp(get_pool_files($ARGV[0]));
+		$ret = set_wp($wp);
+	}
 	else {
-		$wp = choose_wp(get_pool_files());
+		$wp = choose_wp(get_pool_files(WP_POOL));
 		$ret = set_wp($wp);
 	}
 	if (@ARGV == 0 || (@ARGV != 0 && $ARGV[0] ne "-nw")) {
-- 
cgit v1.2.3