summaryrefslogtreecommitdiff
path: root/hyprshutdown.sh
blob: d9587eda9fa7393e4e695b923005280554bb5e2c (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
#!/usr/bin/bash

WAIT=10 # in seconds
SELF=$$
CANCEL="Cancel"
CONFIRM="Confirm"
DUP_ACTION=reboot # or shutdown or reboot
DUP_AMOUNT=2 # this only matters if its shutdown or reboot
# maybe add hyprsession --save or whatever thats called to restore prev apps
SHUTDOWN="hyprshutdown --post-cmd poweroff"
REBOOT="hyprshutdown --post-cmd reboot"
# default
ACTION=Shutdown

other_pids=$(pidof -x -o $$ "$(basename "$0")")
dup_count=$(echo "$other_pids" | wc -w)

# kill dialog RAII
trap 'kill -- -$$' EXIT

function exec_action {
	if [[ $ACTION == "Shutdown" ]]; then
		$SHUTDOWN & disown
	elif [[ $ACTION == "Reboot" ]]; then
		$REBOOT & disown
	fi
	kill $SELF
}

function dialog {
	ans=$(timeout 1.2 hyprland-dialog --apptitle hyprshutdown --title "$ACTION in $1" --buttons "$CANCEL;$CONFIRM")
	if [[ $ans == $CONFIRM ]]; then
		exec_action
	elif [[ $ans == $CANCEL ]]; then
		kill $SELF
	fi
}

if [[ $DUP_ACTION == "kill" ]]; then
	if [ $dup_count -gt 0 ]; then
		kill $other_pids
	fi
elif [[ $DUP_ACTION == "shutdown" ]]; then
	if [ $dup_count -ge $DUP_AMOUNT ]; then
		kill $other_pids
		exec_action
	fi
elif [[ $DUP_ACTION == "reboot" ]]; then
	if [ $dup_count -ge $DUP_AMOUNT ]; then
		kill $other_pids
		ACTION=Reboot
	fi
fi

for ((i=WAIT; i>=0; i--)); do
	dialog $i &
	sleep 1
done

exec_action