-
Notifications
You must be signed in to change notification settings - Fork 1
/
libs.sh
307 lines (268 loc) · 6.74 KB
/
libs.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/bin/bash
## dependency: libapt.sh
## dependency: libfiles.sh
## dependency: libgsettings.sh
## dependency: libgit.sh
## dependency: libmount.sh
## dependency: libnet.sh
## dependency: libexec.sh
## dependency: libatom.sh
## dependency: liblxc.sh
## dependency: libinstall.sh
#!/bin/bash
[[ $0 != $BASH_SOURCE ]] || echo "Script is not intended to be run, but rather sourced"
adamlibpath=$(dirname $BASH_SOURCE)
source ${adamlibpath}/libapt.sh
source ${adamlibpath}/libfiles.sh
source ${adamlibpath}/libgsettings.sh
source ${adamlibpath}/libgit.sh
source ${adamlibpath}/libmount.sh
source ${adamlibpath}/libnet.sh
source ${adamlibpath}/libexec.sh
source ${adamlibpath}/libatom.sh
source ${adamlibpath}/liblxc.sh
source ${adamlibpath}/libinstall.sh
#Gets ubuntu version in format e.g. 1804 or 1604
function get_ubuntu_version {
local infix="$1"
local tmp=$(lsb_release -a 2>/dev/null | grep Release)
local pattern='^Release:\s*([0-9]+)\.([0-9]+)$'
if [[ "$tmp" =~ $pattern ]]; then
echo ${BASH_REMATCH[1]}${infix}${BASH_REMATCH[2]}
return 0
fi
local ubuntu_codename=$(get_ubuntu_codename)
if [[ "$ubuntu_codename" == "bionic" ]]; then
echo 18${infix}04
return 0
fi
return 1
}
function get_ubuntu_codename {
local tmp=$(lsb_release --codename 2>/dev/null | grep Codename)
local pattern='^Codename:\s*([^ ]+)$'
if [[ ! "$tmp" =~ $pattern ]]; then
return 1
fi
local codename=${BASH_REMATCH[1]}
if [[ "${codename}" == "tina" ]]; then
codename=bionic
fi
if [[ "${codename}" == "tara" ]]; then
codename=bionic
fi
echo "${codename}"
return 0
}
function get_distribution {
local tmp=$(lsb_release --id 2>/dev/null)
local pattern='^Distributor ID:\s*([^ ]+)$'
if [[ "$tmp" =~ $pattern ]]; then
echo ${BASH_REMATCH[1]}
return 0
fi
return 1
}
function simple_systemd_service {
local name=$1
local description="$2"
local program=$3
shift;shift;shift;shift
local args="$@"
local is_active
if [ ! -f "$program" ]; then
if which $program >/dev/null; then
program=$(which ${program})
fi
fi
logexec sudo chmod +x ${program}
cat > /tmp/tmp_service <<EOT
[Unit]
Description=$description
[Service]
ExecStart="$program" $args
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOT
if ! cmp --silent /tmp/${name}.service /lib/systemd/system/${name}.service; then
logheredoc EOT
sudo tee /lib/systemd/system/${name}.service>/dev/null <<EOT
[Unit]
Description=$description
[Service]
ExecStart="$program" $args
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOT
logexec sudo systemctl daemon-reload
is_active=$(systemctl is-active $name)
if [ ! "$is_active" == "active" ]; then
logexec sudo systemctl enable $name
fi
fi
if ! systemctl status $name | grep -q running; then
logexec sudo service $name start
return 0
fi
return 1
}
function custom_systemd_service {
local name=$1
local contents="$2"
local is_active
textfile /lib/systemd/system/${name}.service "${contents}" root
logexec sudo systemctl daemon-reload
is_active=$(systemctl is-active $name)
if [ ! "$is_active" == "active" ]; then
logexec sudo systemctl enable $name
fi
if ! systemctl status $name | grep -q running; then
logexec sudo service $name start
return 0
fi
return 1
}
function check_for_root {
if which sudo >/dev/null; then
if ! sudo -n true 2>/dev/null; then
errcho "User $USER doesn't have admin rights"
return 1
fi
else
if [[ "$UID" != 0 ]]; then
errcho "No sudo present and user $USER is not root!"
return 1
else
logexec apt install sudo --yes
fi
fi
return 0
}
function get_home_dir {
if [ -n "$1" ]; then
local USER=$1
fi
echo $( getent passwd "$USER" | cut -d: -f6 )
}
function get_special_dir {
local dirtype=$1
local user=$2
local ans=""
local HOME
local pattern
local folder
HOME=$(get_home_dir $user)
if [ -f "${HOME}/.config/user-dirs.dirs" ]; then
source "${HOME}/.config/user-dirs.dirs"
varname="XDG_${dirtype}_DIR"
echo "${!varname}"
return
# line=$(grep "^[^#].*${dirtype}" "${HOME}/.config/user-dirs.dirs")
# pattern="^.*${dirtype}.*=\"?([^\"]+)\"?$"
# if [[ "$line" =~ $pattern ]]; then
# folder=${BASH_REMATCH[1]}
# ans=$(echo $folder | envsubst )
# if [ "$ans" == "$HOME" ]; then
# ans=""
# fi
# fi
fi
echo ""
}
function make_sure_dir_is_in_a_path {
local newpath="$1"
if ! echo "$PATH" | tr ':' '\n' | grep '^\\one\\two$' >/dev/null; then
export PATH=${PATH}:${newpath}
fi
}
function get_ui_context {
local user=$1
if [ -z "$user" ]; then
user=$USER
fi
if [ -z "${DBUS_SESSION_BUS_ADDRESS}" ]; then
compatiblePrograms=( nemo unity nautilus kdeinit kded4 pulseaudio trackerd )
for index in ${compatiblePrograms[@]}; do
PIDS=$(pidof -s ${index})
if [[ "${PIDS}" != "" ]]; then
for PID in ${PIDS[@]}; do
uid=$(awk '/^Uid:/{print $2}' /proc/${PID}/status)
piduser=$(getent passwd "$uid" | awk -F: '{print $1}')
if [[ "$piduser" == ${user} ]]; then
break;
fi
done
fi
done
if [[ "${PID}" == "" ]]; then
ercho "Could not detect active login session"
return 1
fi
if [ -r /proc/${PID}/environ ]; then
QUERY_ENVIRON="$(cat /proc/${PID}/environ | tr '\0' '\n' | grep "DBUS_SESSION_BUS_ADDRESS" | cut -d "=" -f 2-)"
else
QUERY_ENVIRON="$(sudo cat /proc/${PID}/environ | tr '\0' '\n' | grep "DBUS_SESSION_BUS_ADDRESS" | cut -d "=" -f 2-)"
fi
if [[ "${QUERY_ENVIRON}" != "" ]]; then
export DBUS_SESSION_BUS_ADDRESS="${QUERY_ENVIRON}"
echo "Connected to session:"
echo "DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS}"
else
echo "Could not find dbus session ID in user environment."
return 1
fi
fi
export DISPLAY=:0
return 0
}
function make_desktop_non_script {
local execpath="$1"
local title="$2"
local description="$3"
local icon="$4"
if [ -f "$icon" ]; then
icon_dir="/usr/share/icons/myicon"
cp_file "$icon" "${icon_dir}/" root
icon="$(basename "$icon")"
fi
contents="
[Desktop Entry]
Version=1.0
Name=${title}
Comment=${description}
Exec=${execpath}
Icon=${icon}
Terminal=false
Type=Application
Categories=Application;"
filename=$(echo "$execpath" | awk '{print $1;}')
textfile "/usr/share/applications/${filename}.desktop" "$contents"
}
function make_service_user {
local username=$1
local homedir=$2
if [ -n "$homedir" ]; then
homedir=" --home-dir $homedir"
fi
if ! id -u $username 2>/dev/null; then
logexec sudo useradd -r $homedir --shell /bin/false $username
fi
}
function add_group {
local groupname=$1
if ! grep -q "^$groupname:" /etc/group; then
logexec sudo groupadd $groupname
fi
}
function add_usergroup {
local username=$1
local groupname=$1
if ! groups $username | grep -q "\b${groupname}\b" ;then
logexec sudo usermod -a -G ${groupname} ${username}
fi
}
function get_total_mem_MB {
echo $(grep MemTotal /proc/meminfo | awk '{print $2}')/1024 | bc
}