-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwecker
executable file
·140 lines (128 loc) · 2.75 KB
/
wecker
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env bash
#
# wecker
#
# Use your computer as an alarm clock
#
# Author: Emanuel Duss
#
NOW=`date +%s`
PrintUsage() {
echo "Usage:
wecker option <params>
Options:
set <time>
Set the alarm at <time>
sleep <minutes>
Suspend the computer after <minutes>
disable
Disable the alarm
status
Shows the current alarm time
Examples:
wecker set 23:00
The computer will start at 23:00h
wecker sleep 42m
The computer will suspend in 42 minutes
wecker disable
The computer will not start automatically"
}
case "$1" in
"set")
if [ "$UID" -ne 0 ]
then
echo "Only root can set the alarm"
exit 1
fi
if [ -z "$2" ]
then
echo "You must specify the time (eg. 23:05)!"
exit 1
fi
if ! date -d $2 >/dev/null 2>&1
then
echo "Alarm time is invalid."
exit 1
fi
# Alarm only possible in the future
if [ "`date -d $2 +%s`" -gt "$NOW" ]
then
echo "Set alarm for today"
ALARMTIME="`date -d $2 +%s`"
elif [ "$((`date -d $2 +%s` - $NOW))" -ge 86400 ]
then
echo "Alarm time not possible."
exit 1
else
echo "Set alarm for tomorrow"
ALARMTIME="$((`date -d $2 +%s` + 86400))"
fi
if rtcwake -m no -t "$ALARMTIME" >/dev/null 2>&1
then
$0 status
else
echo "Alarm set failed!"
exit 1
fi
;;
"sleep")
if [ "$UID" -eq 0 ] && which pm-suspend >/dev/null 2>&1
then
SUSPENDCMD="pm-suspend"
fi
if which systemctl >/dev/null 2>&1
then
SUSPENDCMD="systemctl suspend"
fi
if [ -z "$SUSPENDCMD" ]
then
echo "Your computer is not able to suspend."
echo "Please check permissions or install required software."
exit 1
fi
if [ -z "$2" ]
then
echo "You must specify how many minutes for sleeping in"
exit 1
fi
if ! [[ "$2" =~ ^[0-9]+$ ]]
then
echo "This is not a number."
exit 1
fi
echo "Computer will standby in $2 minutes."
sleep ${2}m
$SUSPENDCMD
;;
"status")
echo -n "Current status: "
if [ "`awk '/alarm_IRQ/{ print $3 }' /proc/driver/rtc`" == "yes" ]
then
ALARM=`cat /sys/class/rtc/rtc0/wakealarm`
date -d @"$ALARM" "+Alarm is set: %A %Y-%m-%d %H:%M:%S"
DIFF=`expr $ALARM - $NOW`
HRS=`expr $DIFF / 60 / 60`
MINS=`expr \( $DIFF - $HRS \* 60 \* 60 \) / 60`
echo "Alarm will start in $HRS h $MINS m"
else
echo "Alarm is not set."
fi
;;
"disable")
if [ "$UID" -ne 0 ]
then
echo "Only root can disable the alarm."
exit 1
fi
echo "Disable alarm."
rtcwake -m disable
$0 status
;;
"-h")
PrintUsage
;;
*)
PrintUsage
exit 1
;;
esac