This repository has been archived by the owner on Jan 2, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate-rc.sh
executable file
·231 lines (194 loc) · 5.72 KB
/
generate-rc.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
#!/bin/bash
trim() {
# http://www.cyberciti.biz/faq/bash-remove-whitespace-from-string/
local OUTPUT="$1"
### trim leading whitespaces
OUTPUT="${OUTPUT##*([[:blank:]])}"
### trim trailing whitespaces
echo "${OUTPUT%%*([[:blank:]])}"
}
error() {
echo "$(tput sgr0)$(tput bold)$(tput setaf 7)$(tput setab 1)[hosting-check]$(tput sgr0) $*" >&2
}
msg() {
echo "$(tput sgr0)$(tput dim)$(tput setaf 0)$(tput setab 2)[hosting-check]$(tput sgr0) $*"
}
title() {
local TITLE="$1"
echo
echo "$TITLE"
for (( i=0; i < ${#TITLE}; i++ )); do
echo -n "="
done
echo
}
strip_hash() {
local STRING="$1"
sed -r 's/^(\/\/)?#\s+//' <<< "$STRING"
}
reset_file() {
local FILE="$1"
echo -n > "$FILE"
}
dnsquery() {
## error 1: empty host
## error 2: invalid answer
## error 3: invalid query type
## error 4: not found
local TYPE="$1"
local HOST="$2"
local ANSWER
local IP
# empty host
[ -z "$HOST" ] && return 1
# last record only
IP="$(LC_ALL=C host -t "$TYPE" "$HOST" 2> /dev/null | tail -n 1)"
if ! [ -z "$IP" ] && [ "$IP" = "${IP/ not found:/}" ] && [ "$IP" = "${IP/ has no /}" ]; then
case "$TYPE" in
A)
ANSWER="${IP#* has address }"
if grep -q "^\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\$" <<< "$ANSWER"; then
echo "$ANSWER"
else
# invalid IP
return 2
fi
;;
MX)
ANSWER="${IP#* mail is handled by *[0-9] }"
if grep -q "^[a-z0-9A-Z.-]\+\$" <<< "$ANSWER"; then
echo "$ANSWER"
else
# invalid hostname
return 2
fi
;;
PTR)
ANSWER="${IP#* domain name pointer }"
if grep -q "^[a-z0-9A-Z.-]\+\$" <<< "$ANSWER"; then
echo "$ANSWER"
else
# invalid hostname
return 2
fi
;;
TXT)
ANSWER="${IP#* domain name pointer }"
if grep -q "^[a-z0-9A-Z.-]\+\$" <<< "$ANSWER"; then
echo "$ANSWER"
else
# invalid hostname
return 2
fi
;;
*)
# invalid type
return 3
;;
esac
return 0
else
# not found
return 4
fi
}
rev_hostname() {
local HC_HOST
local HC_IP
local REV_HOSTNAME
[ -z "$HC_SITE" ] && echo ""
HC_HOST="$(sed -r 's|^(([a-z]+:)?//)?([a-z0-9.-]+)/.*$|\3|' <<< "$HC_SITE")"
HC_IP="$(dnsquery A "$HC_HOST")"
if ! [ $? = 0 ]; then
echo "${HC_HOST}"
return 1
fi
REV_HOSTNAME="$(dnsquery PTR "$HC_IP")"
if [ $? = 0 ]; then
# remove trailing dot for certificate vaildation
echo "${REV_HOSTNAME%.}"
else
echo "${HC_HOST}"
return 1
fi
}
process_template() {
local TEMPLATE="$1"
local CONFIG_FILE="$2"
local PROMPT_COLOR="$3"
local VALUE_PROMPT="$(tput sgr0)$(tput dim)$(tput setaf 0)$(tput setab ${PROMPT_COLOR})%s$(tput sgr0): "
local Variable
local Default
local Name
local Description
local Validator
local Output
local VALUE
[ -r "$TEMPALTE" ] && return 1
reset_file "$CONFIG_FILE"
while read -r LINE <&3; do
LINE="$(strip_hash "$LINE")"
case "${LINE%%=*}" in
Variable|Default|Name|Description|Validator|Output)
eval "$LINE"
;;
*)
continue
;;
esac
# got Output, start rendering
if ! [ -z "$Variable" ] \
&& ! [ -z "$Validator" ] \
&& ! [ -z "$Output" ]; then
echo "$Description"
unset VALUE
while : ; do
read -p "$(printf "$VALUE_PROMPT" "$Name")" -e -i "$Default" VALUE
VALUE="$(trim "$VALUE")"
if grep -q "$Validator" <<< "$VALUE"; then
break
else
error "Validation error ($VALUE)"
fi
done
# set global variable
declare "${Variable}"="${VALUE}"
# append to config file
printf "$Output\n" "${Variable}" "${VALUE}" >> "$CONFIG_FILE"
unset Variable
unset Validator
unset Output
fi
done 3< "$TEMPLATE"
return 0
}
continue_db() {
local QUESTION="$1"
while : ; do
read -p "$(tput sgr0)$(tput dim)$(tput setaf 0)$(tput setab 6)${QUESTION}$(tput sgr0) " \
-e -i "y" ANSWER
if [[ "$ANSWER" = [yYnN] ]]; then
break
else
error "Validation error ($ANSWER)"
fi
done
[[ "$ANSWER" = [nN] ]] && exit 0
}
## example template entry
## "$VALUE" contains the previously entered value
# Variable="HC_FTP_HOST"
# Default="$(sed -r 's|^(([a-z]+:)?//)?([a-z0-9.-]+)/.*$|\3|' <<< "$VALUE")"
# Name='Site URL'
# Description='Host name of the FTP server'
# Validator='^[a-zA-Z0-9-.]\{13,50\}$'
# Output='%s="%s"'
#
title 'Please enter FTP and other SETTINGS'
process_template "templates/.hcrc" "./.hcrc" 3
msg "Configuration file (.hcrc) generated OK."
continue_db "Continue with Database credentials? [y/n]"
title 'Please enter MySQL database credentials http://codex.wordpress.org/Editing_wp-config.php'
process_template "templates/wp-config.php" "./wp-config.php" 6
msg "Database configuration file (wp-config.php) generated OK."
msg "Generated wp-config.php will be uploaded to the webroot directory."