summaryrefslogtreecommitdiff
path: root/hyprshutdown.sh
blob: 0c65e766d34083c84603ca1ee1d0b78596b3f803 (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
#!/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_CMD="poweroff"
SHUTDOWN="hyprshutdown --post-cmd $SHUTDOWN_CMD"
REBOOT_CMD="reboot"
REBOOT="hyprshutdown --post-cmd $REBOOT_CMD"
# 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 &
		(wait $!; $SHUTDOWN_CMD) &
	elif [[ $ACTION == "Reboot" ]]; then
		$REBOOT & disown
		(wait $!; $REBOOT_CMD) &
	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