summaryrefslogtreecommitdiffstats
path: root/.local/bin/setwp
blob: 98938c57568a1c4033d4655f95f4f0ae8a869f71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env perl

use strict;
use warnings;
use File::HomeDir qw(home);
use File::Copy;

use constant {
	WP_POOL	=> home() . '/pics/wp/'
};
use constant FEH_PATH	=> 'feh';

sub get_pool_files
{
	my @files;

	opendir(DIR, WP_POOL) or die "Couldn't open directory " . WP_POOL . ": $!";
	@files = grep { !/^\./ } readdir(DIR);
	closedir(DIR);
	return @files;
}

sub choose_wp
{
	my @files = (@_);
	my $rand;
	my $wp;

	$rand = int(rand(@files));
	$wp = WP_POOL . $files[$rand];
	return $wp;
}

sub set_wp
{
	my ($wp) = @_;

	return 1 if !(-r $wp) || !(-f $wp);
	system(
		FEH_PATH,
		'--no-fehbg',
		'--image-bg',
		'#1d2021',
		'--bg-fill',
		$wp,
		'--bg-fill',
		$wp,
		'--bg-fill',
		$wp
	);
	return 0;
}

sub notify
{
	system(
		'notify-send',
		'-u',
		'low',
		'-t',
		'2000',
		'setwp',
		'  wp set'
	);
	return;
}

sub notify_error
{
	system(
		'notify-send',
		'-u',
		'critical',
		'-t',
		'4000',
		'setwp',
		'  Wallpaper does not exist or is not a valid file'
	);
	return;
}

sub main
{
	my $wp;
	my $ret;

	if (@ARGV != 0 && -f $ARGV[0]) {
		$ret = set_wp($ARGV[0]);
	}
	else {
		$wp = choose_wp(get_pool_files());
		$ret = set_wp($wp);
	}
	if (@ARGV == 0 || (@ARGV != 0 && $ARGV[0] ne "-nw")) {
		notify() if ($ret == 0);
		notify_error() if ($ret != 0);
	}
	return;
}

main();

__END__