forked from billw2/pikrellcam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-pikrellcam.sh
executable file
·338 lines (283 loc) · 7.55 KB
/
install-pikrellcam.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
#!/bin/bash
PGM=`basename $0`
if [ `id -u` == 0 ]
then
echo -e "$PGM should not be run as root.\n"
exit 1
fi
bad_install()
{
echo "Cannot find $1 in $PWD"
echo "Are you running $PGM in the install directory?"
exit 1
}
if [ ! -x $PWD/pikrellcam ]
then
bad_install "program pikrellcam"
fi
if [ ! -d $PWD/www ]
then
bad_install "directory www"
fi
echo ""
echo "Set the port for the nginx web server."
echo "If you already have a web server configuration using the default"
echo "port 80, you should enter an alternate port for PiKrellCam."
echo "Otherwise you can use the default port 80 or an alternate as you wish."
echo "The port number will be set in: /etc/nginx.sites-available/pikrellcam."
echo -n "Enter web server port: "
read resp
if [ "$resp" == "" ]
then
PORT=80
else
PORT=$resp
fi
echo ""
echo "For auto starting at boot, a PiKrellCam start command must be in rc.local."
echo "If you don't start at boot, PiKrellCam can always be started and stopped"
echo "from the web page."
echo -n "Do you want PiKrellCam to be auto started at boot? (yes/no): "
read resp
if [ "$resp" == "y" ] || [ "$resp" == "yes" ]
then
AUTOSTART=yes
else
AUTOSTART=no
fi
HTPASSWD=www/.htpasswd
PASSWORD=""
echo ""
if [ -f $HTPASSWD ]
then
echo "A web password is already set."
echo -n "Do you want to change the password (yes/no)? "
read resp
if [ "$resp" == "y" ] || [ "$resp" == "yes" ]
then
SET_PASSWORD=yes
rm -f $HTPASSWD
else
SET_PASSWORD=no
fi
else
SET_PASSWORD=yes
fi
if [ "$SET_PASSWORD" == "yes" ]
then
echo "Enter a password for a web page login for user: $USER"
echo "Enter a blank entry if you do not want the password login."
echo -n "Enter password: "
read PASSWORD
fi
echo ""
echo "Starting PiKrellCam install..."
# =============== apt install needed packages ===============
#
PACKAGE_LIST=""
for PACKAGE in gpac php5 php5-common php5-fpm nginx libav-tools bc sshpass mpack imagemagick apache2-utils
do
if ! dpkg -s $PACKAGE 2>/dev/null | grep Status | grep -q installed
then
PACKAGE_LIST="$PACKAGE_LIST $PACKAGE"
fi
done
if [ "$PACKAGE_LIST" != "" ]
then
echo "Installing packages: $PACKAGE_LIST"
echo "Running: apt-get update"
sudo apt-get update
sudo apt-get install -y $PACKAGE_LIST
else
echo "No packages need to be installed."
fi
JESSIE=8.0
DEB_VERSION=`cat /etc/debian_version`
IS_WHEEZY=`echo "$DEB_VERSION < $JESSIE" | bc`
if [ $IS_WHEEZY -gt 0 ]
then
if ! dpkg -s realpath 2>/dev/null | grep Status | grep -q installed
then
echo "Installing package: realpath"
sudo apt-get install -y realpath
fi
fi
if [ ! -h /usr/local/bin/pikrellcam ]
then
echo "Making /usr/local/bin/pikrellcam link."
sudo rm -f /usr/local/bin/pikrellcam
sudo ln -s $PWD/pikrellcam /usr/local/bin/pikrellcam
else
CURRENT_BIN=`realpath /usr/local/bin/pikrellcam`
if [ "$CURRENT_BIN" != "$PWD/pikrellcam" ]
then
echo "Replacing /usr/local/bin/pikrellcam link"
sudo rm /usr/local/bin/pikrellcam
sudo ln -s $PWD/pikrellcam /usr/local/bin/pikrellcam
fi
fi
# =============== create initial ~/.pikrellcam configs ===============
#
./pikrellcam -quit
# =============== set install_dir in pikrellcam.conf ===============
#
PIKRELLCAM_CONF=$HOME/.pikrellcam/pikrellcam.conf
if [ ! -f $PIKRELLCAM_CONF ]
then
echo "Unexpected failure to create config file $HOME/.pikrellcam/pikrellcam.conf"
exit 1
fi
if ! grep -q "install_dir $PWD" $PIKRELLCAM_CONF
then
echo "Setting install_dir config line in $PIKRELLCAM_CONF:"
echo "install_dir $PWD"
sed -i "/install_dir/c\install_dir $PWD" $PIKRELLCAM_CONF
fi
# =============== pikrellcam autostart to rc.local ===============
#
#CMD="su $USER -c '(sleep 5; \/home\/pi\/pikrellcam\/pikrellcam) \&'"
CMD="su $USER -c '(sleep 5; $PWD/pikrellcam) \&'"
if [ "$AUTOSTART" == "yes" ]
then
if ! fgrep -q "$CMD" /etc/rc.local
then
if grep -q pikrellcam /etc/rc.local
then
sudo sed -i "/pikrellcam/d" /etc/rc.local
fi
echo "Adding a pikrellcam autostart command to /etc/rc.local:"
sudo sed -i "s|^exit.*|$CMD\n&|" /etc/rc.local
if ! [ -x /etc/rc.local ]
then
echo "Added execute permission to /etc/rc.local"
sudo chmod a+x /etc/rc.local
fi
grep pikrellcam /etc/rc.local
fi
else
if grep -q pikrellcam /etc/rc.local
then
echo "Removing pikrellcam autostart line from /etc/rc.local."
sudo sed -i "/pikrellcam/d" /etc/rc.local
fi
fi
# ===== sudoers permission for www-data to run pikrellcam as pi ======
#
CMD=$PWD/pikrellcam
if ! grep -q "$CMD" /etc/sudoers.d/pikrellcam 2>/dev/null
then
echo "Adding to /etc/sudoers.d: www-data permission to run pikrellcam as user pi:"
cp etc/pikrellcam.sudoers /tmp/pikrellcam.sudoers.tmp
sed -i "s|pikrellcam|$CMD|" /tmp/pikrellcam.sudoers.tmp
sed -i "s/USER/$USER/" /tmp/pikrellcam.sudoers.tmp
sudo chown root.root /tmp/pikrellcam.sudoers.tmp
sudo chmod 440 /tmp/pikrellcam.sudoers.tmp
sudo mv /tmp/pikrellcam.sudoers.tmp /etc/sudoers.d/pikrellcam
# sudo cat /etc/sudoers.d/pikrellcam
fi
# =============== Setup Password ===============
#
OLD_SESSION_PATH=www/session
if [ -d $OLD_SESSION_PATH ]
then
sudo rm -rf $OLD_SESSION_PATH
fi
OLD_PASSWORD=www/password.php
if [ -f $OLD_PASSWORD ]
then
rm -f $OLD_PASSWORD
fi
if [ "$PASSWORD" != "" ]
then
htpasswd -bc $HTPASSWD $USER $PASSWORD
sudo chown $USER.www-data $HTPASSWD
fi
# =============== nginx install ===============
#
# Logging can eat many tens of megabytes of SD card space per day
# with the mjpeg.jpg streaming
#
if ! grep -q "access_log off" /etc/nginx/nginx.conf
then
echo "Turning off nginx access_log."
sudo sed -i '/access_log/c\ access_log off;' /etc/nginx/nginx.conf
fi
if [ $IS_WHEEZY -gt 0 ]
then
NGINX_SITE=etc/nginx-wheezy-site-default
else
NGINX_SITE=etc/nginx-jessie-site-default
fi
echo "Installing /etc/nginx/sites-available/pikrellcam"
echo " nginx web server port: $PORT"
echo " nginx web server root: $PWD/www"
sudo cp $NGINX_SITE /etc/nginx/sites-available/pikrellcam
sudo sed -i "s|PIKRELLCAM_WWW|$PWD/www|; \
s/PORT/$PORT/" \
/etc/nginx/sites-available/pikrellcam
NGINX_SITE=/etc/nginx/sites-available/pikrellcam
if [ "$PORT" == "80" ]
then
NGINX_LINK=/etc/nginx/sites-enabled/default
CURRENT_SITE=`realpath $NGINX_LINK`
if [ "$CURRENT_SITE" != "$NGINX_SITE" ]
then
echo "Changing $NGINX_LINK link to pikrellcam"
sudo rm -f $NGINX_LINK
sudo ln -s $NGINX_SITE $NGINX_LINK
fi
else
NGINX_LINK=/etc/nginx/sites-enabled/pikrellcam
fi
if [ ! -h $NGINX_LINK 2>/dev/null ]
then
echo "Adding $NGINX_LINK link to sites-available/pikrellcam."
sudo ln -s $NGINX_SITE $NGINX_LINK
fi
if [ ! -f $HTPASSWD ]
then
echo "A password for the web page is not set."
sudo sed -i 's/auth_basic/\# auth_basic/' $NGINX_SITE
fi
sudo service nginx restart
# =============== Setup FIFO ===============
#
fifo=$PWD/www/FIFO
if [ ! -p "$fifo" ]
then
rm -f $fifo
mkfifo $fifo
fi
sudo chown $USER.www-data $fifo
sudo chmod 664 $fifo
# =============== copy scripts-dist into scripts ===============
#
if [ ! -d scripts ]
then
mkdir scripts
fi
cd scripts-dist
for script in *
do
if [ ! -f ../scripts/$script ] && [ "${script:0:1}" != "_" ]
then
cp $script ../scripts
fi
done
echo ""
echo "Install finished."
echo "This install script does not automatically start pikrellcam."
echo "To start pikrellcam, open a browser page to:"
if [ "$PORT" == "80" ]
then
echo " http://your_pi"
else
echo " http://your_pi:$PORT"
fi
echo "and click on the \"System\" panel and then the \"Start PiKrellCam\" button."
echo "PiKrellCam can also be run from a Pi terminal for testing purposes."
if [ "$AUTOSTART" == "yes" ]
then
echo "Automatic pikrellcam starting at boot is enabled."
fi
echo ""