-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-run
150 lines (129 loc) · 3.77 KB
/
pre-run
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
#!/bin/sh -u
# -u: Treat unset variables as an error when performing parameter expansion.
# If expansion is attempted on an unset interactive, exits with a non-zero status.
#
# Process executed before application manager execution
#
# Author: Loan Lassalle <https://github.com/lassalleloan>
# Current version of StevenBlack/hosts source files
CURRENT_VERSION="$(sed -En 's/^CURRENT_VERSION=([0-9]+([.][0-9]+)*)(( |#).*)?$/\1/p' .conf)"
# Default verbosity level
VERBOSITY="$(sed -En 's/^VERBOSITY=([0-9]{1})(( |#).*)?$/\1/p' .conf)"
# Display a banner
banner() {
printf "Pre-run script\n"
}
# Usage of the script
usage() {
printf "Usage: pre-run [--verbosity (0 | 1 | 2)]\n"
printf "\n"
printf "Process executed before run-app execution\n"
printf "\n"
printf "Version: 1.0.0, build deadbeef\n"
printf "\n"
printf "Author:\n"
printf " Loan Lassalle - <https://github.com/lassalleloan>\n"
printf "\n"
printf "Options:\n"
printf " -v, --verbosity (0 | 1 | 2) Level of verbosity: no ouput, step information (default), all information\n"
printf " -h, --help Help on how to use this script\n"
}
# Check for updates
check_updates() {
local current_version="$1"
local verbosity="$2"
# Retrieve the latest version number of StevenBlack/hosts source files
local latest_version="$(curl --silent https://raw.githubusercontent.com/StevenBlack/hosts/master/package.json | \
sed -En 's/^[ ]*"version": "([0-9]+([.][0-9]+)*)",$/\1/p')"
# Check if the latest version number has been retrieved
if [ -z "$latest_version" ]; then
latest_version="0.0.0"
fi
if [ "$verbosity" -ne 0 ]; then
printf "Checking for updates\n"
fi
if [ "$verbosity" -eq 2 ]; then
printf "Current version: %*s\n" 12 "$current_version"
printf "Latest version: %*s\n" 13 "$latest_version"
fi
# Check if the latest version is greater than the current version
if [ ! -f src/etc/hosts ] || \
[ ! -s src/etc/hosts ] || \
[ "$(printf '%s\n' "$current_version" "$latest_version" | \
sort -rV | \
head -n 1)" != "$current_version" ]; then
# Update the lastest version in .conf file
sed -i '' -E "/^LATEST_VERSION=$current_version(( |#).*)?$/ s/$current_version/$latest_version/" \
.conf
# Audit system
if [ "$verbosity" -eq 0 ]; then
printf "%s\tinfo\tupgrade\t%s\t%s\t\"New version available\"\n" \
"$(date -u +%FT%TZ)" "$current_version" "$latest_version" >> .log
fi
else
# Audit system
if [ "$verbosity" -eq 0 ]; then
printf "%s\tinfo\tupdate\t" "$(date -u +%FT%TZ)" >> .log
if [ "$latest_version" = "0.0.0" ]; then
printf "%s\t%s\t\"Lastest version number not retrieved\"\n" \
"$current_version" "$latest_version" >> .log
else
printf "%s\t%s\t\"No new version available\"\n" "$current_version" "$latest_version" >> .log
fi
fi
# Stop all future process
exit 1
fi
}
# Check if it is an integer
exit_if_not_integer() {
local variable="$1"
if ! echo "$variable" | grep -Eq "^[0-9]{1}$"; then
printf "%s: not a integer\n" "$variable"
usage
exit 1
fi
}
# Main
for arg in "$@"; do
shift
case "$arg" in
"--verbosity")
set -- "$@" "-v"
;;
"--help")
set -- "$@" "-h"
;;
"--"*)
set -- "$@" "-?"
;;
*)
set -- "$@" "$arg"
esac
done
while getopts ":v:h" option; do
case $option in
v)
VERBOSITY=$OPTARG
exit_if_not_integer "$VERBOSITY"
;;
h)
usage
exit
;;
:)
printf "%s: required argument\n" "$OPTARG"
exit 1
;;
\?)
printf "%s: invalid option\n" "$OPTARG"
usage
exit 1
;;
esac
done
if [ "$VERBOSITY" -ne 0 ]; then
banner
fi
# Check for updates
check_updates "$CURRENT_VERSION" "$VERBOSITY"