-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalarm
More file actions
108 lines (97 loc) · 4.17 KB
/
Copy pathalarm
File metadata and controls
108 lines (97 loc) · 4.17 KB
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
#!/bin/bash
# Function to calculate the next occurrence of a given weekday
next_weekday() {
target_day=$(date -d "$1" +%u) # Convert weekday to a number (1=Monday, 7=Sunday)
current_day=$(date +%u) # Get current day of the week
if (( target_day >= current_day )); then
# If the target day is today or later this week
days_to_add=$((target_day - current_day))
else
# If the target day is earlier in the week, schedule for next week
days_to_add=$((7 - current_day + target_day))
fi
date -d "+$days_to_add day" +"%Y-%m-%d"
}
# Function to print usage/help message
alarm_print_help() {
echo "Usage: alarm [DATE] [TIME] [MESSAGE]"
echo "Sets an alarm using the 'at' command and displays a toast notification."
echo
echo "Arguments:"
echo " DATE Optional. Date in 'yyyy-mm-dd' format or a weekday abbreviation (e.g., MON, TUE)."
echo " If a weekday is provided, the alarm will be set for the first next occurrence of that weekday."
echo " TIME Time in 'HH:MM' format or '+<minutes>' format."
echo " - 'HH:MM': Sets the alarm at the specified time."
echo " - '+<minutes>': Sets the alarm after the specified number of minutes."
echo " MESSAGE Optional. Custom message to display with the alarm."
echo
echo "Examples:"
echo " alarm 15:30 \"Meeting starts now!\""
echo " alarm +10 \"Take a break!\""
echo " alarm MON 08:00 \"Monday morning alarm!\""
echo " alarm 2024-10-20 07:00 \"Important event!\""
echo " alarm -help Displays this help message."
}
alarm() {
# Display help if no arguments are provided or if '-help' is called
if [[ $# -eq 0 || $1 == "-help" ]]; then
alarm_print_help
return 0
fi
# Check if "at" is installed
if ! command -v at &> /dev/null; then
echo "'at' command not found. Attempting to install..."
if ! sudo apt-get install -y at; then
echo "Failed to install 'at'. Please re-run the script with sudo permissions."
return 1
fi
fi
# Check if "atd" is running and start it if not
if ! pgrep -x "atd" > /dev/null; then
echo "'atd' service is not running. Attempting to start it..."
if ! sudo service atd start; then
echo "Failed to start 'atd' service. Please check your system configuration."
return 1
fi
fi
# Locate the full path of the "toast" command
toast_path=$(whereis -b toast | awk '{print $2}')
if [[ -z "$toast_path" ]]; then
echo "Could not find 'toast'. Make sure it is installed and available."
return 1
fi
# Handle date or weekday input
if [[ $1 =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
# If the input is in yyyy-mm-dd format
alarm_date=$1
elif [[ $1 =~ ^(MON|TUE|WED|THU|FRI|SAT|SUN)$ ]]; then
# If the input is a weekday abbreviation
alarm_date=$(next_weekday "$1")
fi
# Shift the arguments if a date or weekday was provided
if [[ -n "$alarm_date" ]]; then
shift
fi
# Handle different time formats
if [[ $1 =~ ^[\+]?[0-9]+$ ]]; then
# Extract the number of minutes (remove the "+")
minutes_from_now=${1#+}
if [[ -n "$alarm_date" ]]; then
echo "\"$toast_path\" alarm \"${2:-It is now $minutes_from_now minutes from now on $alarm_date!}\"" | at $alarm_date now + $minutes_from_now minutes
else
echo "\"$toast_path\" alarm \"${2:-It is now $minutes_from_now minutes from now!}\"" | at now + $minutes_from_now minutes
fi
echo "Alarm set for $minutes_from_now minutes from now."
elif [[ $1 =~ ^([0-1]?[0-9]|2[0-3]):([0-5][0-9])$ ]]; then
# If the user passed a time in HH:MM format, pass it directly to 'at'
if [[ -n "$alarm_date" ]]; then
echo "\"$toast_path\" alarm \"${2:-It is now $1 on $alarm_date!}\"" | at $1 $alarm_date
else
echo "\"$toast_path\" alarm \"${2:-It is now $1!}\"" | at $1
fi
echo "Alarm set for $1."
else
echo "Please provide time in hh:mm format, +<minutes> format, or a valid date or weekday."
return 1
fi
}