-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathphad-simple-install.sh
executable file
·366 lines (315 loc) · 13.6 KB
/
phad-simple-install.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env bash
# Some of this code copied shamelessly from the pi-hole basic-install.sh script
# found at https://install.pi-hole.net
# Parse the output of udevadm, looking for the device associated with a touchscreen
function get_touchscreen_dev()
{
local IFS=$'\n'
local NEWDEV=".BLOCK"
for i in $(udevadm info --export-db | sed -e "s/^$/${NEWDEV}/") ; do
[[ $i == *"ID_INPUT_TOUCHSCREEN"* ]] && _DEVID=${i}
[[ $i == *DEVNAME=* ]] && _DEVNAME=${i#*=}
if [[ "$i" == "$NEWDEV" ]] ; then
if [[ "$_DEVNAME" != "" && "$_DEVID" != "" ]] ; then
echo $_DEVNAME
return
fi
unset _DEVNAME
unset _DEVID
fi
done
}
# This is a file used for the colorized output
coltable=/opt/pihole/COL_TABLE
# If the color table file exists,
if [[ -f "${coltable}" ]]; then
# source it
source ${coltable}
# Otherwise,
else
# Set these values so the installer can still run in color
COL_NC='\e[0m' # No Color
COL_LIGHT_GREEN='\e[1;32m'
COL_LIGHT_RED='\e[1;31m'
TICK="[${COL_LIGHT_GREEN}✓${COL_NC}]"
CROSS="[${COL_LIGHT_RED}✗${COL_NC}]"
INFO="[i]"
# shellcheck disable=SC2034
DONE="${COL_LIGHT_GREEN} done!${COL_NC}"
OVER="\\r\\033[K"
fi
# Check if we are running on a real terminal and find the rows and columns
# If there is no real terminal, we will default to 80x24
if [ -t 0 ] ; then
screen_size=$(stty size)
else
screen_size="24 80"
fi
# Set rows variable to contain first number
printf -v rows '%d' "${screen_size%% *}"
# Set columns variable to contain second number
printf -v columns '%d' "${screen_size##* }"
# Divide by two so the dialogs take up half of the screen, which looks nice.
r=$(( rows / 2 ))
c=$(( columns / 2 ))
# Unless the screen is tiny
r=$(( r < 20 ? 20 : r ))
c=$(( c < 70 ? 70 : c ))
# interface_filename;off_value;on_value
TOUCHSCREEN_BACKLIGHT_INTERFACES=$(cat << EOM
/sys/class/backlight/rpi_backlight/bl_power;1;0
/sys/class/backlight/soc:backlight/bl_power;1;0
EOM
)
TOUCHSCREEN_INPUT_DEVICES=$(cat << EOM
/dev/input/touchscreen
/dev/input/event0
EOM
)
PIHOLE_FILES=$(cat <<EOM
/etc/pihole/setupVars.conf
/usr/local/bin/pihole
/opt/pihole/version.sh
EOM
)
PHAD_FILES=$(cat <<EOM
phad
phad.conf
templates/main.j2
EOM
)
REQUIREMENTS="jinja2>==2.0 requests>==2.0"
PYTHON3=$(/usr/bin/env python3 -c 'import sys; print(sys.version_info[0])')
if [[ "$PYTHON3" != "3" ]] ; then
printf " %b %s\\n" "${CROSS}" "Python 3 not found\\n"
printf " %b %bPhad requires version 3 of Python to run.%b\\n" "${INFO}" "${COL_LIGHT_RED}" "${COL_NC}"
printf " Please ensure Python 3 is installed and that the following command\\n"
printf " launches the Python 3 interpreter:\\n"
printf "\\n %b/usr/bin/env python3%b\\n" "${COL_LIGHT_GREEN}" "${COL_NC}"
printf "\\n"
exit 1
fi
for i in ${PIHOLE_FILES} ; do
if [ ! -f $i ] ; then
printf " %b %s\\n" "${CROSS}" "File $i not found\\n"
printf " %b %bPi-hole's basic installer installs this file as part of its setup.%b\\n" "${INFO}" "${COL_LIGHT_RED}" "${COL_NC}"
printf " This file does not exist, which means Pi-hole is not installed or has\\n"
printf " been installed in a custom location. This phad installer script assumes\\n"
printf " that Pi-hole was installed with default settings. Please see the phad\\n"
printf " README file for instructions on installing phad manually.\\n"
exit 1
fi
done
printf " %b %bChecking Python 3 for pip%b\\n" "${INFO}" "${COL_LIGHT_RED}" "${COL_NC}"
/usr/bin/env python3 -m pip --version > /dev/null 2>&1
if [[ $? -ne 0 ]] ; then
printf " %b %bInstalling python3-pip package%b\\n" "${INFO}" "${COL_LIGHT_RED}" "${COL_NC}"
sudo apt-get install python3-pip > /dev/null 2>&1
if [[ $? -ne 0 ]] ; then
printf " %b %s\\n" "${CROSS}" "Unable to install python3-pip\\n"
printf " %b %bThe python3-pip package is required to allow python to install other packages.%b\\n" "${INFO}" "${COL_LIGHT_RED}" "${COL_NC}"
printf " Please ensure the Python3 pip module is installed then re-run this installer.\\n"
exit 1
fi
fi
if [[ "$1" == "--upgrade" ]] ; then
printf " %b %bInstalling Python 3 dependencies%b\\n" "${INFO}" "${COL_LIGHT_RED}" "${COL_NC}"
EVDEV=$(/usr/bin/env python3 -m pip list | grep evdev)
if [[ "$EVDEV" != "" ]] ; then
REQUIREMENTS="$REQUIREMENTS evdev>==1.0.0"
fi
/usr/bin/env python3 -m pip install -q ${REQUIREMENTS}
DL_URL=$(curl --silent "https://api.github.com/repos/bpennypacker/phad/releases/latest" | grep tarball_url | sed -e 's/^.*\: "//' -e 's/".*$//')
printf "\\n"
DL_FILE=$(curl --silent -L $DL_URL | tar tz --strip-components=1 | grep -e '/phad$')
printf " %b %bDownloading phad from %s%b\\n" "${INFO}" "${COL_LIGHT_RED}" "${DL_URL}" "${COL_NC}"
curl --silent -L $DL_URL | tar xz $DL_FILE --strip-components=1
printf " %b %s\\n" "${TICK}" "Successfully downloaded the phad program. No changes have been made to phad.conf or ${HOME}/.bashrc"
exit 0
fi
grep -q -i phad ${HOME}/.bashrc
if [[ $? -eq 0 ]] ; then
printf " %b %s\\n" "${CROSS}" "phad already configured to run"
printf " Referenes to phad exist in ${HOME}/.bashrc.\\n"
printf " For this reason the installer refuses to run. Please see the\\n"
printf " README file for instructions on installing phad manually.\\n"
printf "\\n"
printf " If you would like to replace just the phad program without\\n"
printf " making any changes to your existing configuration then run\\n"
printf " the following commands:\\n\\n"
printf " wget -c https://raw.githubusercontent.com/bpennypacker/phad/master/phad-simple-install.sh\\n"
printf " bash phad-simple-install.sh --upgrade\\n"
exit 1
fi
# -e option instructs bash to immediately exit if any command [1] has a non-zero exit status
# We do not want users to end up with a partially working install, so we exit the script
# instead of continuing the installation with something broken
set -e
PHAD_DIR="${HOME}/phad"
mkdir -p $PHAD_DIR
if [ ! -d $PHAD_DIR ] ; then
printf " %b %s\\n" "${CROSS}" "Directory $PHAD_DIR not found"
printf " %b %bphad installs into this directory but this installer was unable to create it. Aborting.%b\\n" "${INFO}" "${COL_LIGHT_RED}" "${COL_NC}"
exit 1
fi
cd $PHAD_DIR
for i in ${PHAD_FILES} ; do
if [ -f "$i" ] ; then
printf " %b %s\\n" "${CROSS}" "file ${PHAD_DIR}/${i} exists"
printf " %b %bphad appears to already be installed in $PHAD_DIR. Aborting.%b\\n" "${INFO}" "${COL_LIGHT_RED}" "${COL_NC}"
exit 1
fi
done
printf " %b %bChecking for touchscreen backlight interface%b\\n" "${INFO}" "${COL_LIGHT_RED}" "${COL_NC}"
for i in ${TOUCHSCREEN_BACKLIGHT_INTERFACES}; do
TS_FILE="$(cut -d ';' -f1 <<< "${i}")"
TS_OFF="$(cut -d ';' -f2 <<< "${i}")"
TS_ON="$(cut -d ';' -f3 <<< "${i}")"
if [[ -f "${TS_FILE}" && "${BACKLIGHT_FILE}" == "" ]] ; then
if whiptail --title "Touchscreen backlight interface" --yesno "A potential interface for your touchscreen backlight has been found at ${TS_FILE}. Would you like to test this in order to enable touchscreen blanking? If you choose 'yes' then this installer will attempt to make the LCD display go blank for 5 seconds and then turn it on again. Would you like to do this?" ${r} ${c}; then
[ -w ${TS_FILE} ] || REQUIRE_SUDO=1
printf "Attempting to blank LCD display in "
for i in $(seq 5 -1 1) ; do printf "${i}... " ; sleep 1 ; done
printf "\\n"
if [ "$REQUIRE_SUDO" == "1" ] ; then
sudo sh -c "echo '${TS_OFF}' > ${TS_FILE}"
else
echo "${TS_OFF}" > ${TS_FILE}
fi
for i in $(seq 5 -1 1) ; do printf "${i}... " ; sleep 1 ; done
printf "\\n"
if [ "$REQUIRE_SUDO" == "1" ] ; then
sudo sh -c "echo '${TS_ON}' > ${TS_FILE}"
else
echo "${TS_ON}" > ${TS_FILE}
fi
if whiptail --defaultno --title "Touchscreen backlight interface" --yesno "Did your touchscreen go blank?" ${r} ${c}; then
BACKLIGHT_FILE=${TS_FILE}
BACKLIGHT_CMD_OFF=${TS_OFF}
BACKLIGHT_CMD_ON=${TS_ON}
printf "\\n"
printf " %b %s\\n" "${TICK}" "Found touchscreen backlight interface: ${BACKLIGHT_FILE}"
printf "\\n"
fi
fi
fi
done
ts_dev=$(get_touchscreen_dev)
RE='^[0-9]*$'
T="x"
if [ "$ts_dev" != "" ] ; then
if whiptail --title "Touchscreen interface" --yesno "A touch interfface was found at ${ts_dev}. Would you like phad to wake up when you touch the touchscreen?" ${r} ${c}; then
TOUCHSCREEN_DEV=${ts_dev}
REQUIREMENTS="$REQUIREMENTS evdev>==1.0.0"
while ! [[ $T =~ $RE ]] ; do
T=$(whiptail --title "Touchscreen timeout" --inputbox "How many seconds after tapping on the display should phad blank the screen again?" ${r} ${c} 10 3>&1 1>&2 2>&3)
done
[ "$T" != "" ] && MAIN_TIMEOUT=$T
fi
fi
if [[ "$TOUCHSCREEN_DEV" == "" ]] ; then
T="x"
while ! [[ $T =~ $RE ]] ; do
T=$(whiptail --title "Display cycle time" --inputbox "No touchscreen interface was found, so touch support will be disabled. Instead, phad will be configured to cycle between displays automatically. How many seconds shold phad wait between switching its display?" ${r} ${c} 20 3>&1 1>&2 2>&3)
done
[ "$T" != "" ] && TEMPLATE_TIMEOUT="-s $T"
fi
declare -a TEMPLATE_LIST=()
if [[ "$TOUCHSCREEN_DEV" != "" && "$BACKLIGHT_FILE" == "" ]] ; then
TEMPLATE_LIST+=("blank.j2" "A blank screen to simulate turning off the display" ON)
fi
TEMPLATE_LIST+=("main.j2" "The main phad summary screen" ON)
TEMPLATE_LIST+=("top_ads.j2" "A list of the top ads blocked by your Pi-Hole" ON)
TEMPLATE_LIST+=("top_clients.j2" "A list of the top clients using your Pi-Hole" ON)
TEMPLATE_LIST+=("top_domains.j2" "A list of the top domains resolved by your Pi-Hole" ON)
TEMPLATE_LIST+=("network.j2" "A summary of your Pi-Hole's network settings" ON)
L=${#TEMPLATE_LIST[@]}
N=$(( L / 3 ))
while [[ "$TL" == "" ]] ; do
TL=$(whiptail --title "Select pages phad should cycle between" --checklist "Select the templates that phad should cycle between" ${r} ${c} $N "${TEMPLATE_LIST[@]}" 3>&1 1>&2 2>&3)
done
TEMPLATES=$(echo $TL | sed -e 's/"//g' -e 's/ /,/'g)
printf " %b %bInstalling python dependencies:%b\\n" "${INFO}" "${COL_LIGHT_RED}" "${COL_NC}"
for i in ${REQUIREMENTS} ; do
printf " - %s\\n" "${i}"
done
/usr/bin/env python3 -m pip install -q ${REQUIREMENTS}
printf " %b %s\\n" "${TICK}" "Installed python dependencies"
DL_URL=$(curl --silent "https://api.github.com/repos/bpennypacker/phad/releases/latest" | grep tarball_url | sed -e 's/^.*\: "//' -e 's/".*$//')
printf "\\n"
printf " %b %bDownloading phad from %s%b\\n" "${INFO}" "${COL_LIGHT_RED}" "${DL_URL}" "${COL_NC}"
curl --silent -L $DL_URL | tar xz --strip-components=1
printf " %b %s\\n" "${TICK}" "Successfully downloaded phad"
for i in ${PHAD_FILES} ; do
if [ ! -f "$i" ] ; then
printf " %b %s\\n" "${CROSS}" "phad file $i not found"
printf " %b %bphad appears to have failed to download properly.%b\\n" "${INFO}" "${COL_LIGHT_RED}" "${COL_NC}"
printf " Check that $DL_URL is valid.\\n"
printf " If that URL is valid then remove the current phad directory and.\\n"
printf " try running this installer again.\\n"
exit 1
fi
done
CMD=" "
if [[ "$BACKLIGHT_FILE" != "" ]] ; then
F=$(echo $BACKLIGHT_FILE | sed -e s'/\//\\\//g')
CMD="$CMD -e s/^file_location=.*$/file_location=$F/"
CMD="$CMD -e s/^on_value=.*$/on_value=$BACKLIGHT_CMD_ON/"
CMD="$CMD -e s/^off_value=.*$/off_value=$BACKLIGHT_CMD_OFF/"
CMD="$CMD -e s/^enabled=.*$/enabled=True/"
if [[ "$REQUIRE_SUDO" == "" ]] ; then
CMD="$CMD -e s/^use_sudo=.*$/use_sudo=False/"
else
CMD="$CMD -e s/^use_sudo=.*$/use_sudo=True/"
fi
else
CMD="$CMD -e s/^enabled=.*$/enabled=False/"
fi
if [[ "$TOUCHSCREEN_DEV" != "" ]] ; then
F=$(echo $TOUCHSCREEN_DEV | sed -e s'/\//\\\//g')
CMD="$CMD -e s/^input_device=.*$/input_device=$F/"
else
CMD="$CMD -e s/^input_device=/#input_device=/"
fi
CMD="$CMD -e s/^templates=.*$/templates=$TEMPLATES/"
if [[ "$MAIN_TIMEOUT" != "" ]] ; then
CMD="$CMD -e s/^main_timeout=.*$/main_timeout=$MAIN_TIMEOUT/"
else
CMD="$CMD -e s/^enable_main_timeout=.*$/enable_main_timeout=False/"
fi
mv phad.conf phad.conf.original
cat phad.conf.original | sed $CMD > phad.conf
printf " %b %s\\n" "${TICK}" "Customized phad.conf"
BASH_TXT=$(cat << EOM
if [ "\$TERM" == "linux" ] ; then
cd ${PHAD_DIR}
while :
do
./phad ${TEMPLATE_TIMEOUT} 2>/dev/null
sleep 10
done
fi
EOM
)
BASHRC=${HOME}/.bashrc
IFS=$'\n'
if whiptail --defaultno --title "Start phad automatically" --yesno "Would you like phad to start up automatically by adding it to the pi users .bashrc file?" ${r} ${c}; then
echo "" >> $BASHRC
echo "# Start phad" >> $BASHRC
for i in ${BASH_TXT} ; do
echo ${i} >> $BASHRC
done
if whiptail --defaultno --title "Reboot?" --yesno "Your Raspberry Pi needs to be rebooted for phad to start. Reboot now?" ${r} ${c}; then
sudo reboot
else
printf " %b %s\\n" "${TICK}" "phad installation complete"
fi
else
printf " %b %s\\n" "${TICK}" "phad installation complete"
printf " %b %bphad has not been configured to start automatically.%b\\n" "${INFO}" "${COL_LIGHT_RED}" "${COL_NC}"
printf " %b %bTo start phad add something like this to your .bashrc file:%b\\n" "${INFO}" "${COL_LIGHT_RED}" "${COL_NC}"
printf "\\n"
for i in ${BASH_TXT} ; do
printf " %s\\n" "$i"
done
fi