forked from silvanmelchior/RPi_Cam_Web_Interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
352 lines (307 loc) · 13.3 KB
/
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
#!/bin/bash
# Copyright (c) 2015, Bob Tidey
# All rights reserved.
# Redistribution and use, with or without modification, are permitted provided
# that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Description
# This script installs a browser-interface to control the RPi Cam. It can be run
# on any Raspberry Pi with a newly installed raspbian and enabled camera-support.
# RPI_Cam_Web_Interface installer by Silvan Melchior
# Edited by jfarcher to work with github
# Edited by slabua to support custom installation folder
# Additions by btidey, miraaz, gigpi
# Rewritten and split up by Bob Tidey
#Debug enable next 3 lines
exec 5> install.txt
BASH_XTRACEFD="5"
set -x
cd $(dirname $(readlink -f $0))
if [ $(dpkg-query -W -f='${Status}' "dialog" 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
sudo apt-get install -y dialog
fi
# Terminal colors
color_red="tput setaf 1"
color_green="tput setaf 2"
color_reset="tput sgr0"
# Version stuff moved out functions as we need it more when one time.
versionfile="./www/config.php"
version=$(cat $versionfile | grep "'APP_VERSION'" | cut -d "'" -f4)
backtitle="Copyright (c) 2015, Bob Tidey. RPi Cam $version"
# Config options located in ./config.txt. In first run script makes that file for you.
if [ ! -e ./config.txt ]; then
sudo echo "#This is config file for main installer. Put any extra options in here." > ./config.txt
sudo echo "rpicamdir=\"html\"" >> ./config.txt
sudo echo "webserver=\"apache\"" >> ./config.txt
sudo echo "webport=\"80\"" >> ./config.txt
sudo echo "user=\"\"" >> ./config.txt
sudo echo "webpasswd=\"\"" >> ./config.txt
sudo echo "autostart=\"yes\"" >> ./config.txt
sudo echo "" >> ./config.txt
sudo chmod 664 ./config.txt
fi
source ./config.txt
rpicamdirold=$rpicamdir
if [ ! "${rpicamold:0:1}" == "" ]; then
rpicamdirold=/$rpicamdirold
fi
#Allow for a quiet install
if [ $# -eq 0 ] || [ "$1" != "q" ]; then
exec 3>&1
dialog \
--separate-widget $'\n' \
--title "Configuration Options" \
--backtitle "$backtitle" \
--form "" \
0 0 0 \
"Cam subfolder:" 1 1 "$rpicamdir" 1 32 15 0 \
"Autostart:(yes/no)" 2 1 "$autostart" 2 32 15 0 \
"Server:(apache/nginx)" 3 1 "$webserver" 3 32 15 0 \
"Webport:" 4 1 "$webport" 4 32 15 0 \
"User:(blank=nologin)" 5 1 "$user" 5 32 15 0 \
"Password:" 6 1 "$webpasswd" 6 32 15 0 \
2>&1 1>&3 | {
read -r rpicamdir
read -r autostart
read -r webserver
read -r webport
read -r user
read -r webpasswd
if [ -n "$webport" ]; then
sudo echo "#This is edited config file for main installer. Put any extra options in here." > ./config.txt
sudo echo "rpicamdir=\"$rpicamdir\"" >> ./config.txt
sudo echo "webserver=\"$webserver\"" >> ./config.txt
sudo echo "webport=\"$webport\"" >> ./config.txt
sudo echo "user=\"$user\"" >> ./config.txt
sudo echo "webpasswd=\"$webpasswd\"" >> ./config.txt
sudo echo "autostart=\"$autostart\"" >> ./config.txt
sudo echo "" >> ./config.txt
fi
}
returncode=$?
exec 3>&-
if [ ! "$returncode" == "0" ]; then
exit
fi
source ./config.txt
fi
if [ ! "${rpicamdir:0:1}" == "" ]; then
rpicamdirEsc="\\/$rpicamdir"
rpicamdir=/$rpicamdir
else
rpicamdirEsc = ""
fi
fn_stop ()
{ # This is function stop
sudo killall raspimjpeg
sudo killall php
sudo killall motion
}
fn_reboot ()
{ # This is function reboot system
dialog --title "You must reboot your system!" --backtitle "$backtitle" --yesno "Do you want to reboot now?" 5 33
response=$?
case $response in
0) sudo reboot;;
1) dialog --title 'Reboot message' --colors --infobox "\Zb\Z1"'Pending system changes that require a reboot!' 4 28 ; sleep 2;;
255) dialog --title 'Reboot message' --colors --infobox "\Zb\Z1"'Pending system changes that require a reboot!' 4 28 ; sleep 2;;
esac
}
fn_apache ()
{
aconf="etc/apache2/sites-available/raspicam.conf"
cp $aconf.1 $aconf
if [ -e "\/$aconf" ]; then
sudo rm "\/$aconf"
fi
if [ -e /etc/apache2/conf-available/other-vhosts-access-log.conf ]; then
aotherlog="/etc/apache2/conf-available/other-vhosts-access-log.conf"
else
aotherlog="/etc/apache2/conf.d/other-vhosts-access-log"
fi
tmpfile=$(mktemp)
sudo awk '/NameVirtualHost \*:/{c+=1}{if(c==1){sub("NameVirtualHost \*:.*","NameVirtualHost *:'$webport'",$0)};print}' /etc/apache2/ports.conf > "$tmpfile" && sudo mv "$tmpfile" /etc/apache2/ports.conf
sudo awk '/Listen/{c+=1}{if(c==1){sub("Listen.*","Listen '$webport'",$0)};print}' /etc/apache2/ports.conf > "$tmpfile" && sudo mv "$tmpfile" /etc/apache2/ports.conf
awk '/<VirtualHost \*:/{c+=1}{if(c==1){sub("<VirtualHost \*:.*","<VirtualHost *:'$webport'>",$0)};print}' $aconf > "$tmpfile" && sudo mv "$tmpfile" $aconf
sudo sed -i "s/<Directory\ \/var\/www\/.*/<Directory\ \/var\/www$rpicamdirEsc>/g" $aconf
if [ "$user" == "" ]; then
sudo awk '/AllowOverride/{c+=1}{if(c==2){sub("AllowOverride.*","AllowOverride None",$0)};print}' $aconf > "$tmpfile" && sudo mv "$tmpfile" $aconf
else
sudo awk '/AllowOverride/{c+=1}{if(c==2){sub("AllowOverride.*","AllowOverride All",$0)};print}' $aconf > "$tmpfile" && sudo mv "$tmpfile" $aconf
sudo htpasswd -b -c /usr/local/.htpasswd $user $webpasswd
if [ ! -e /var/www$rpicamdir/.htaccess ]; then
sudo bash -c "cat > /var/www$rpicamdir/.htaccess" << EOF
AuthName "RPi Cam Web Interface Restricted Area"
AuthType Basic
AuthUserFile /usr/local/.htpasswd
AuthGroupFile /dev/null
Require valid-user
EOF
sudo chown -R www-data:www-data /var/www$rpicamdir/.htaccess
fi
fi
sudo mv $aconf /$aconf
sudo sed -i 's/^CustomLog/#CustomLog/g' $aotherlog
sudo service apache2 restart
}
fn_nginx ()
{
sudo sed -e "s:root /var/www;:root /var/www$rpicamdirEsc;:g" etc/nginx/sites-available/rpicam.1 > etc/nginx/sites-available/rpicam
sudo cp -r etc/nginx/sites-available/rpicam /etc/nginx/sites-available/rpicam
sudo chmod 644 /etc/nginx/sites-available/rpicam
if [ ! -e /etc/nginx/sites-enabled/rpicam ]; then
sudo ln -s /etc/nginx/sites-available/rpicam /etc/nginx/sites-enabled/rpicam
fi
# Update nginx main config file
sudo sed -i "s/worker_processes 4;/worker_processes 2;/g" /etc/nginx/nginx.conf
sudo sed -i "s/worker_connections 768;/worker_connections 128;/g" /etc/nginx/nginx.conf
sudo sed -i "s/gzip on;/gzip off;/g" /etc/nginx/nginx.conf
if ["$NGINX_DISABLE_LOGGING"]; then
sudo sed -i "s:access_log /var/log/nginx/nginx/access.log;:access_log /dev/null;:g" /etc/nginx/nginx.conf
fi
# Configure php-apc
sudo sh -c "echo \"cgi.fix_pathinfo = 0;\" >> /etc/php5/fpm/php.ini"
sudo cp etc/php5/apc.ini /etc/php5/conf.d/20-apc.ini
sudo chmod 644 /etc/php5/conf.d/20-apc.ini
}
fn_motion ()
{
sudo sed -i "s/^; netcam_url.*/netcam_url/g" /etc/motion/motion.conf
sudo sed -i "s/^netcam_url.*/netcam_url http:\/\/localhost$rpicamdirEsc\/cam_pic.php/g" /etc/motion/motion.conf
if [ "$user" == "" ]; then
sudo sed -i "s/^netcam_userpass.*/; netcam_userpass value/g" /etc/motion/motion.conf
else
sudo sed -i "s/^; netcam_userpass.*/netcam_userpass/g" /etc/motion/motion.conf
sudo sed -i "s/^netcam_userpass.*/netcam_userpass $user:$webpasswd/g" /etc/motion/motion.conf
fi
sudo sed -i "s/^; on_event_start.*/on_event_start/g" /etc/motion/motion.conf
sudo sed -i "s/^on_event_start.*/on_event_start echo -n \'1\' >\/var\/www\/FIFO1/g" /etc/motion/motion.conf
sudo sed -i "s/^; on_event_end.*/on_event_end/g" /etc/motion/motion.conf
sudo sed -i "s/^on_event_end.*/on_event_end echo -n \'0\' >\/var\/www\/FIFO1/g" /etc/motion/motion.conf
sudo sed -i "s/control_port.*/control_port 6642/g" /etc/motion/motion.conf
sudo sed -i "s/control_html_output.*/control_html_output off/g" /etc/motion/motion.conf
sudo sed -i "s/^output_pictures.*/output_pictures off/g" /etc/motion/motion.conf
sudo sed -i "s/^ffmpeg_output_movies on/ffmpeg_output_movies off/g" /etc/motion/motion.conf
sudo sed -i "s/^ffmpeg_cap_new on/ffmpeg_cap_new off/g" /etc/motion/motion.conf
sudo sed -i "s/^stream_port.*/stream_port 0/g" /etc/motion/motion.conf
sudo sed -i "s/^webcam_port.*/webcam_port 0/g" /etc/motion/motion.conf
sudo sed -i "s/^process_id_file/; process_id_file/g" /etc/motion/motion.conf
sudo sed -i "s/^videodevice/; videodevice/g" /etc/motion/motion.conf
sudo sed -i "s/^event_gap 60/event_gap 3/g" /etc/motion/motion.conf
sudo sed -i "s/www/www$rpicamdirEsc/" /etc/motion/motion.conf
sudo chown motion:www-data /etc/motion/motion.conf
sudo chmod 664 /etc/motion/motion.conf
}
fn_autostart ()
{
tmpfile=$(mktemp)
sudo sed '/#START/,/#END/d' /etc/rc.local > "$tmpfile" && sudo mv "$tmpfile" /etc/rc.local
# Remove to growing plank lines.
sudo awk '!NF {if (++n <= 1) print; next}; {n=0;print}' /etc/rc.local > "$tmpfile" && sudo mv "$tmpfile" /etc/rc.local
if [ "$autostart" == "yes" ]; then
if ! grep -Fq '#START RASPIMJPEG SECTION' /etc/rc.local; then
sudo sed -i '/exit 0/d' /etc/rc.local
sudo bash -c "cat >> /etc/rc.local" << EOF
#START RASPIMJPEG SECTION
mkdir -p /dev/shm/mjpeg
chown www-data:www-data /dev/shm/mjpeg
chmod 777 /dev/shm/mjpeg
sleep 4;su -c 'raspimjpeg > /dev/null 2>&1 &' www-data
if [ -e /etc/debian_version ]; then
sleep 4;su -c 'php /var/www$rpicamdir/schedule.php > /dev/null 2>&1 &' www-data
else
sleep 4;su -s '/bin/bash' -c 'php /var/www$rpicamdir/schedule.php > /dev/null 2>&1 &' www-data
fi
#END RASPIMJPEG SECTION
exit 0
EOF
fi
fi
sudo chown root:root /etc/rc.local
sudo chmod 755 /etc/rc.local
}
#Main install)
fn_stop
sudo mkdir -p /var/www$rpicamdir/media
#move old material if changing from a different install folder
if [ ! "$rpicamdir" == "$rpicamdirold" ]; then
if [ -e /var/www$rpicamdirold/index.php ]; then
sudo mv /var/www$rpicamdirold/* /var/www$rpicamdir
fi
fi
sudo cp -r www/* /var/www$rpicamdir/
if [ -e /var/www$rpicamdir/index.html ]; then
sudo rm /var/www$rpicamdir/index.html
fi
sudo chown -R www-data:www-data /var/www$rpicamdir
if [ "$webserver" == "apache" ]; then
sudo apt-get install -y apache2 php5 php5-cli libapache2-mod-php5 gpac motion zip libav-tools
fn_apache
else
sudo apt-get install -y nginx php5-fpm php5-cli php5-common php-apc gpac motion zip libav-tools
fn_nginx
fi
#Make sure user www-data has bash shell
sudo sed -i "s/^www-data:x.*/www-data:x:33:33:www-data:\/var\/www:\/bin\/bash/g" /etc/passwd
if [ ! -e /var/www$rpicamdir/FIFO ]; then
sudo mknod /var/www$rpicamdir/FIFO p
fi
sudo chmod 666 /var/www$rpicamdir/FIFO
if [ ! -e /var/www$rpicamdir/FIFO1 ]; then
sudo mknod /var/www$rpicamdir/FIFO1 p
fi
sudo chmod 666 /var/www$rpicamdir/FIFO1
sudo chmod 755 /var/www$rpicamdir/raspizip.sh
if [ ! -e /var/www$rpicamdir/cam.jpg ]; then
sudo ln -sf /run/shm/mjpeg/cam.jpg /var/www$rpicamdir/cam.jpg
fi
sudo cp etc/sudoers.d/RPI_Cam_Web_Interface /etc/sudoers.d/
sudo chmod 440 /etc/sudoers.d/RPI_Cam_Web_Interface
sudo cp -r bin/raspimjpeg /opt/vc/bin/
sudo chmod 755 /opt/vc/bin/raspimjpeg
if [ ! -e /usr/bin/raspimjpeg ]; then
sudo ln -s /opt/vc/bin/raspimjpeg /usr/bin/raspimjpeg
fi
sed -e "s/www/www$rpicamdirEsc/" etc/raspimjpeg/raspimjpeg.1 > etc/raspimjpeg/raspimjpeg
if [ `cat /proc/cmdline |awk -v RS=' ' -F= '/boardrev/ { print $2 }'` == "0x11" ]; then
sed -i 's/^camera_num 0/camera_num 1/g' etc/raspimjpeg/raspimjpeg
fi
if [ -e /etc/raspimjpeg ]; then
$color_green; echo "Your custom raspimjpg backed up at /etc/raspimjpeg.bak"; $color_reset
sudo cp -r /etc/raspimjpeg /etc/raspimjpeg.bak
fi
sudo cp -r etc/raspimjpeg/raspimjpeg /etc/
sudo chmod 644 /etc/raspimjpeg
if [ ! -e /var/www$rpicamdir/raspimjpeg ]; then
sudo ln -s /etc/raspimjpeg /var/www$rpicamdir/raspimjpeg
fi
sudo usermod -a -G video www-data
if [ -e /var/www$rpicamdir/uconfig ]; then
sudo chown www-data:www-data /var/www$rpicamdir/uconfig
fi
fn_motion
fn_autostart
if [ -e /var/www$rpicamdir/uconfig ]; then
sudo chown www-data:www-data /var/www$rpicamdir/uconfig
fi
if [ -e /var/www$rpicamdir/schedule.php ]; then
sudo rm /var/www$rpicamdir/schedule.php
fi
sudo sed -e "s/www/www$rpicamdirEsc/g" www/schedule.php > www/schedule.php.1
sudo mv www/schedule.php.1 /var/www$rpicamdir/schedule.php
sudo chown www-data:www-data /var/www$rpicamdir/schedule.php
fn_reboot