-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsunshine
executable file
·135 lines (117 loc) · 4.38 KB
/
sunshine
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
#!/bin/bash
# Making a shell program for switching themes
CURR_DIR="$(pwd)"
CONF_DIR="$HOME/.config"
SUNSHINE_CONF_DIR="$CONF_DIR/sunshine"
sourceConfig () {
if [ -f "$SUNSHINE_CONF_DIR/sunshine.conf" ]; then
source "$SUNSHINE_CONF_DIR/sunshine.conf"
else
echo "Config File Not Found using 6:00 and 18:00 as Default"
fi
}
setLightsOn () {
if [[ ! -n "$LIGHTS_ON" ]] || [[ $LIGHTS_ON != $(date --date="$LIGHTS_ON" +%H:%M) ]]; then
echo "Invalid Date or Date not Provided for LIGHTS_ON !!!"
LIGHTS_ON="06:00"
fi
echo "Making Light Mode Turn On at $LIGHTS_ON"
}
setLightsOff () {
if [[ ! -n "$LIGHTS_OFF" ]] || [[ $LIGHTS_OFF != $(date --date="$LIGHTS_OFF" +%H:%M) ]]; then
echo "Invalid Date or Date not Provided for LIGHTS_OFF !!!"
LIGHTS_OFF="18:00"
fi
echo "Making Light Mode Turn Off at $LIGHTS_OFF"
}
send_notification () {
if command -v "notify-send" &> /dev/null; then
local ICON;
if [[ -f "$HOME/.local/share/icons/sun-with-face.svg" ]]; then
ICON="$HOME/.local/share/icons/sun-with-face.svg"
elif [[ -f "$CURR_DIR/sun-with-face.svg" ]]; then
ICON="$CURR_DIR/sun-with-face.svg"
else
ICON=""
fi
notify-send -i "$ICON" "Sunshine" "$*"
fi
}
if [[ $1 == "init" ]]; then
if [ ! -f "$CONF_DIR/systemd/user/" ]; then
mkdir -p "$CONF_DIR/systemd/user/"
fi
# Making Systemd Service
SERVICE=$CONF_DIR/systemd/user/sunshine.service
if [ -f "$SERVICE" ]; then
systemctl --user disable sunshine.service
rm "$SERVICE"
fi
echo "Making Sunshine Service File"
printf "[Unit]\nDescription=Sunshine: Schedules Light/Dark Mode" > $SERVICE
printf "\n\n[Service]\nType=Simple\nExecStart=%%h/.local/bin/sunshine service" >> $SERVICE
printf "\n\n[Install]\nWantedBy=default.target" >> $SERVICE
echo "Enabling and Starting Sunshine Service File"
systemctl --user enable sunshine.service
systemctl --user start sunshine.service
# Making Systemd Timer
## Reading Config File for Times
sourceConfig
## Making sure that Times are Correct
setLightsOn
setLightsOff
TIMER=$CONF_DIR/systemd/user/sunshine.timer
if [ -f "$TIMER" ]; then
systemctl --user disable sunshine.timer
rm "$TIMER"
fi
echo "Making Sunshine Timer File"
printf "[Unit]\nDescription=Sunshine: Schedules Light/Dark Mode" > $TIMER
printf "\n\n[Timer]\nOnCalendar=*-*-* $LIGHTS_ON:00\nOnCalendar=*-*-* $LIGHTS_OFF:00" >> $TIMER
printf "\nPersistent=true\nUnit=sunshine.service" >> $TIMER
printf "\n\n[Install]\nWantedBy=timers.target" >> $TIMER
echo "Enabling and Starting Sunshine Timer File"
systemctl --user enable sunshine.timer
systemctl --user start sunshine.timer
#Run for the First Time
sunshine service
elif [[ $1 == "service" ]]; then
## Reading Config File for Times
sourceConfig
## Making sure that Times are Correct
setLightsOn
setLightsOff
HOUR=$(date "+%H")
MIN=$(date "+%M")
LIGHTS_ON_H=$(date --date="$LIGHTS_ON" +%H)
LIGHTS_ON_M=$(date --date="$LIGHTS_ON" +%M)
LIGHTS_OFF_H=$(date --date="$LIGHTS_OFF" +%H)
LIGHTS_OFF_M=$(date --date="$LIGHTS_OFF" +%M)
if [[ $LIGHTS_OFF_H < $LIGHTS_ON_H ]] ||
[[ $LIGHTS_OFF_H == $LIGHTS_ON_H && $LIGHTS_OFF_M < $LIGHTS_ON_M ]];
then
LIGHTS_OFF_H=$(( $LIGHTS_OFF_H + 24 ))
fi
if [[ $HOUR > $LIGHTS_ON_H || ($HOUR == $LIGHTS_ON_H && $MIN -ge $LIGHTS_ON_M) ]] &&
[[ $HOUR < $LIGHTS_OFF_H || ($HOUR == $LIGHTS_OFF_H && $MIN < $LIGHTS_OFF_M) ]];
then
if [ -f "$SUNSHINE_CONF_DIR/light.sh" ]; then
echo "Sourcing light.sh"
source "$SUNSHINE_CONF_DIR/light.sh"
send_notification "Light Mode Activated till $LIGHTS_OFF"
else
echo "light.sh File Not Found In '$SUNSHINE_CONF_DIR'. Try Again!!"
fi
else
if [ -f "$SUNSHINE_CONF_DIR/dark.sh" ]; then
echo "Sourcing dark.sh"
source "$SUNSHINE_CONF_DIR/dark.sh"
send_notification "Dark Mode Activated till $LIGHTS_ON"
else
echo "dark.sh File Not Found In '$SUNSHINE_CONF_DIR'. Try Again!!"
fi
fi
else
echo -e "Usage \n sunshine init #For Initializing Sunshine"
echo -e " sunshine service #For Changing Themes based on Time"
fi