-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sh
267 lines (217 loc) · 7.67 KB
/
main.sh
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/bin/bash
# OSALT: Open Source Scripting Automation Linux Toolkit
# Copyright (c) 2023 KawaCoder@duck.com
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>. *
#
function show_help() {
echo "osalt [options] [script_name]"
echo
echo "Options:"
echo " -s, --startup <script_name> Add a startup script."
echo " -w, --shutdown <script_name> Add a shutdown script."
echo " -c, --scheduled <script_name> <scheduled> Add a scheduled script."
echo " -d, --delete <script_name> Delete a script."
echo " -l, --list List all available scripts."
echo " -h, --help Prints this help."
echo
echo "Examples:"
echo " osalt -s my_startup_script.sh"
echo " osalt -h my_shutdown_script.sh"
echo " osalt -c my_scheduled_script.sh \"*-*-* 9-17:00:00\""
echo " osalt -v my_startup_script.sh"
echo " osalt -d my_shutdown_script.sh"
echo " osalt -l"
}
function list_scripts(){
# Parse the config.json file with jq
config_file="config.json"
# List items in the "startup" category
echo "Startup:"
jq -r '.startup[] | .name' "$config_file"
# List items in the "shutdown" category
echo "Shutdown:"
jq -r '.shutdown[] | .name' "$config_file"
# List items in the "scheduled" category
echo "Scheduled:"
jq -r '.scheduled[] | .name' "$config_file"
}
function delete_script() {
script_name="$1"
just_file_name="$(basename "$script_name" ".sh")"
config_file="config.json"
# if [ -f "config.json" ]; then
# script_category=""
# if [ "$(jq -r ".startup | index( \"$script_name\" )" "$config_file")" != null ]; then
# script_category="startup"
# elif [ "$(jq -r ".shutdown | index( \"$script_name\" )" "$config_file")" != null ]; then
# script_category="shutdown"
# elif [ "$(jq -r ".scheduled | index( \"$script_name\" )" "$config_file")" != null ]; then
# script_category="scheduled"
# else
# echo "Script \"$script_name\" not found in config.json"
# exit 1
# fi
script_category=$(jq -r '
if .startup[] | select(.name == "test.sh") then
"startup"
else
"startup not found"
end,
if .shutdown[] | select(.name == "test.sh") then
"shutdown"
else
"shutdown not found"
end,
if .scheduled[] | select(.name == "test.sh") then
"scheduled"
else
"scheduled not found"
end' config.json)
jq --arg script_name "$script_name" 'del(.startup[] | select(.name == $script_name)) | del(.shutdown[] | select(.name == $script_name)) | del(.scheduled[] | select(.name == $script_name))' "config.json" > temp.json
mv temp.json "$config_file"
if [ -f "$HOME/OSALT/scripts/$script_category/$script_name" ]; then
rm "$HOME/OSALT/scripts/$script_category/$script_name"
else
echo "Script file not found in \"$HOME/OSALT/scripts/$script_category/\""
fi
echo "Script \"$script_name\" has been deleted."
if [ "$script_category" = "scheduled" ]; then
systemctl --user stop "$just_file_name".timer
systemctl --user disable "$just_file_name".timer
echo "$HOME/.config/systemd/user/$just_file_name.timer"
rm "$HOME/.config/systemd/user/$just_file_name.timer"
echo "Systemd user service file removed."
fi
systemctl --user stop "$just_file_name".service
systemctl --user disable "$just_file_name".service
echo "$HOME/.config/systemd/user/$just_file_name.service"
rm "$HOME/.config/systemd/user/$just_file_name.service"
echo "Systemd user service file removed."
}
function add_script(){
new_script_path=$1
new_script_name=$(basename "$new_script_path")
option="$2"
schedule="$3"
if [ -f "config.json" ]; then
if grep -q "\"name\": \"$new_script_name\"" config.json; then
echo "Script with the same name already exists in config.json."
exit 1
else
jq ".$option += [{\"name\": \"$new_script_name\", \"path\": \"$new_script_path\"}]" config.json > temp.json
mv temp.json config.json
echo "New script added to config.json."
fi
else
echo '{"startup": [], "shutdown": [], "scheduled": []}' > config.json
jq ".$option += [{\"name\": \"$new_script_name\", \"path\": \"$new_script_path\"}]" config.json > temp.json
mv temp.json config.json
echo "New script added to config.json."
fi
if [ ! -d "$HOME/OSALT/scripts/$option" ]; then
mkdir -p "$HOME/OSALT/scripts/$option"
fi
cp "$new_script_path" "$HOME/OSALT/scripts/$option/"
chmod +x "$HOME/OSALT/scripts/$option/$new_script_name"
just_file_name="$(basename "$new_script_name" ".sh")"
if [ "$option" = "startup" ]; then
echo "
[Unit]
Description=$just_file_name
[Service]
Type=oneshot
ExecStartPre=/bin/sleep 10
ExecStart=$HOME/OSALT/scripts/$option/$new_script_name
[Install]
WantedBy=default.target
" >> "$HOME"/.config/systemd/user/"$just_file_name".service
systemctl --user enable "$just_file_name".service
elif [ "$option" = "shutdown" ]; then
echo "
[Unit]
Description=$just_file_name
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStart=$HOME/OSALT/scripts/$option/$new_script_name
[Install]
WantedBy=default.target
" >> "$HOME"/.config/systemd/user/"$just_file_name".service
systemctl --user enable "$just_file_name".service
elif [ "$option" = "scheduled" ]; then
echo "
[Unit]
Description=$just_file_name
[Service]
Type=oneshot
ExecStart=$HOME/OSALT/scripts/$option/$new_script_name
" >> "$HOME"/.config/systemd/user/"$just_file_name".service
echo "
[Unit]
Description=$just_file_name
[Timer]
OnCalendar=$schedule
Persistent=true
[Install]
WantedBy=timers.target
" >> "$HOME"/.config/systemd/user/"$just_file_name".timer
systemctl --user enable "$just_file_name".timer
fi
}
#systemd-analyze verify ~/.config/systemd/user/test.timer
#systemd-analyze verify ~/.config/systemd/user/test.service
if [ "$#" -lt 1 ]; then
show_help
exit 1
fi
while [ "$#" -gt 0 ]; do
case "$1" in
-s|--script)
file="$2"
option="startup"
add_script "$file" "$option"
shift 2
;;
-w|--shutdown)
file="$2"
option="shutdown"
add_script "$file" "$option"
shift 2
;;
-c|--scheduled)
file="$2"
option="scheduled"
schedule="$3"
add_script "$file" "$option" "$3"
shift 3
;;
-d|--delete)
delete_script "$2"
shift 2
;;
-l|--list)
list_scripts
exit 0
;;
-h|--help)
show_help
exit 1
;;
*)
echo "Please pass an argument"
show_help
exit 1
;;
esac
done