-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils
executable file
·189 lines (178 loc) · 5.55 KB
/
utils
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
#!/bin/bash
#===============================================================================
# HEADER (this header was shamelessly stolen from Michel VONGILAY @ uxora.com)
#===============================================================================
#% DESCRIPTION
#% Collection of functions and variables that are useful to other moosh
#% scripts. This script is not intended to be used as a standalone script.
#===============================================================================
#- IMPLEMENTATION
#- version ${SCRIPT_NAME} 1.0.0
#- author Grupo Plataformas Abertas (jpgrego@ua.pt,
#- pedro.lobo@ua.pt)
#- organization Universidade de Aveiro
#===============================================================================
# HISTORY
# 2017/08/02 : jpgrego: Script creation
#===============================================================================
# END_OF_HEADER
#===============================================================================
readonly SCRIPT_HEADSIZE=$(awk '/^# END_OF_HEADER/ {print NR}' "$0")
readonly SCRIPT_NAME=$(basename "$0")
readonly URL_REGEX='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
#readonly MOODLE_PATH='/var/www/html/moodle/webroot/elearning-2017-18'
#readonly MOODLE_PATH='/var/www/html/moodle/webroot'
readonly MOOSH_BIN=$(which moosh)
################################################################################
# Show how to use the script
#
# Globals:
# SCRIPT_NAME
# Arguments:
# None
# Returns:
# None
################################################################################
function utils::usage() {
local temp
temp=$(awk '/^#\+/ {gsub("^#\\+", ""); print "Usage: "; print $0 }' "$0")
temp="${temp//\$\{SCRIPT_NAME\}/${SCRIPT_NAME}}"
echo "${temp}"
}
################################################################################
# Show full information concerning the script, including how to use, author,
# description, etc.
#
# Globals:
# SCRIPT_NAME
# Arguments:
# None
# Returns:
# None
################################################################################
function utils::usagefull() {
local temp
temp=$(awk '/^#[%+-]/ {gsub("^#[%+-]", ""); print }' "$0")
temp="${temp//\$\{SCRIPT_NAME\}/${SCRIPT_NAME}}"
echo "${temp}"
}
################################################################################
# Show author info of the script
#
# Globals:
# SCRIPT_NAME
# Arguments:
# None
# Returns:
# None
################################################################################
function utils::scriptinfo() {
local temp
temp=$(awk '/^#-/ {gsub("^#-", ""); print }' "$0")
temp="${temp//\$\{SCRIPT_NAME\}/${SCRIPT_NAME}}"
echo "${temp}"
}
################################################################################
# Remove leading and trailing whitespace from a given variable
#
# Globals:
# None
# Arguments:
# $1 string that is to be trimmed
# Returns:
# trimmed string
################################################################################
function utils::trim_string() {
local temp
temp=$(echo "$1" | sed -e 's/^\s*//' -e 's/\s*$//')
echo "${temp}"
}
################################################################################
# Escape all special characters on a given string. Useful when the string is to
# be used in the middle of a regex search.
#
# Globals:
# None
# Arguments:
# $1 string to be escaped
# Returns:
# escaped string
################################################################################
function utils::escape_special_chars() {
local temp
temp="$1"
# escape '\'
temp="${temp//\\/\\\\}"
# escape '|'
temp="${temp//|/\|}"
# escape '('
temp="${temp//(/\(}"
# escape ')'
temp="${temp//)/\)}"
# escape '$'
temp="${temp//\$/\\$}"
# escape '^'
temp="${temp//^/\^}"
# escape '['
temp="${temp//[/\[}"
# escape ']'
temp="${temp//]/\]}"
# escape '*'
temp="${temp//\*/\*}"
# escape '.'
temp="${temp//./\.}"
# escape '+'
temp="${temp//+/\+}"
# escape '?'
temp="${temp//\?/\?}"
# escape '{'
temp="${temp//{/\{}"
# escape '}'
temp="${temp//\}/\}}"
echo "${temp}"
}
################################################################################
# write text to the file given by the LOGS_FILE environment variable
#
# Globals:
# LOGS_FILE
# Arguments:
# $1 string to be written to the file
# Returns:
# None
################################################################################
function utils::write_to_log() {
if [[ -n "${LOGS_FILE:-}" ]]; then
echo -e "[ $(date -Ins) ]\t$1" >> "${LOGS_FILE}"
else
echo -e "[ $(date -Ins) ]\t$1"
fi
}
################################################################################
# Make initial verifications, like checking if moosh is in PATH, and if the
# logs folder exists
#
# Globals:
# None
# Arguments:
# None
# Returns:
# 0 if everything seems OK
# 1 otherwise
################################################################################
function utils::generic_initial_verification() {
if [[ "${EUID}" == 0 ]]; then
echo "Please run this script as a user other than root." >&2
return 1
fi
if [[ -z "${MOOSH_BIN}" ]]; then
echo "No moosh binary was found. Please make sure moosh is installed." >&2
return 1
fi
return 0
}
function main() {
if [[ "${SCRIPT_NAME}" == "utils.sh" ]]; then
utils::usagefull
fi
}