summaryrefslogtreecommitdiffstats
path: root/.local/bin/shutup
blob: d49c2d99407d16a04d5f08c552a758bb9dfc08ea (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
104
105
#!/usr/bin/env perl

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

use constant {
	BSDSETSID_PATH			=> '/usr/local/bin/bsdsetsid',
	DUNSTRC_PATH			=> home() . '/.config/dunst/dunstrc',
	DUNST_PATH				=> '/usr/local/bin/dunst',
	MPDNOTIFICATION_PATH	=> '/usr/local/bin/mpd-notification',
	NOTIFYSEND_PATH			=> '/usr/local/bin/notify-send',
	PKILL_PATH				=> '/bin/pkill'
};

sub kill_programs
{
	system(PKILL_PATH, 'espeak');
	system(PKILL_PATH, 'mpd-notification');
	system(PKILL_PATH, 'dunst');
}

sub start_programs
{
	system(BSDSETSID_PATH, DUNST_PATH);
	system(BSDSETSID_PATH, MPDNOTIFICATION_PATH);
}

sub get_buffer
{
	my @lines;

	open(FH, '<', DUNSTRC_PATH) or die $!;
	@lines = <FH>;
	close(FH);
	return @lines;
}

sub notify
{
	my ($notif) = @_;

	if ($notif) {
		system(NOTIFYSEND_PATH,
			   '-u', 'normal',
			   '-t', '1750',
			   'espeak', '墳 espeak restored');
	}
	else {
		system(NOTIFYSEND_PATH,
			   '-u', 'normal',
			   '-t', '1750',
			   'espeak', '婢 espeak silenced');
	}
}

sub write_config
{
	my (@lines) = @_;

	open(FH, '>', DUNSTRC_PATH) or die $!;
	print FH @lines;
	close(FH);
}

sub toggle_config
{
	my (@lines) = @_;
	my $ret;
	my $i;
	my $j;

	for ($i = 0; $i < @lines; $i++) {
		last if $lines[$i] =~ m/# >shutup/;
	}
	$j = 0;
	while ($j <= 2) {
		if ($lines[$i + $j] =~ m/^#/) {
			$lines[$i + $j] =~ s/^#//;
			$ret = 1;
		}
		else {
			$lines[$i + $j] =~ s/^/#/;
			$ret = 0;
		}
		$j++;
	}
	write_config(@lines);
	return $ret;
}

sub main
{
	my $notif;

	kill_programs();
	$notif = toggle_config(get_buffer());
	start_programs();
	notify($notif);
	return;
}

main();

__END__