forked from billw2/pikrellcam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-pikrellcam.sh
executable file
·327 lines (275 loc) · 7.3 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
#!/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
echo ""
echo "Enter a password if you want a password login for the web page."
echo "Enter a blank entry if you do not want the password login."
echo "The password will be configured in: $PWD/www/password.php"
echo -n "Enter password :"
read resp
if [ "$resp" == "" ]
then
PASSWORD="unset"
else
PASSWORD=$resp
fi
echo ""
echo "Starting PiKrellCam install..."
WHEEZY=7.8
DEB_VERSION=`cat /etc/debian_version`
# =============== apt install needed packages ===============
#
PACKAGE_LIST=""
for PACKAGE in gpac php5 php5-common php5-fpm nginx libav-tools bc sshpass mpack
do
if ! dpkg-query -l $PACKAGE &> /dev/null
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
echo "Installing packages: $PACKAGE_LIST"
sudo apt-get install -y $PACKAGE_LIST
else
echo "No packages need to be installed."
fi
IS_WHEEZY=`echo "$DEB_VERSION <= $WHEEZY" | bc`
if [ $IS_WHEEZY -gt 0 ]
then
if ! dpkg-query -l realpath &> /dev/null
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 pi -c '(sleep 5; \/home\/pi\/pikrellcam\/pikrellcam) \&'"
CMD="su pi -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
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
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
# =============== 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
NGINX_RESTART="yes"
fi
WHEEZY=7.8
DEB_VERSION=`cat /etc/debian_version`
IS_JESSIE=`echo "$DEB_VERSION > $WHEEZY" | bc`
if [ $IS_JESSIE -gt 0 ]
then
NGINX_SITE=etc/nginx-jessie-site-default
else
NGINX_SITE=etc/nginx-wheezy-site-default
fi
if ! grep -q $PORT /etc/nginx/sites-available/pikrellcam 2>/dev/null \
|| ! grep -q "root $PWD/www" /etc/nginx/sites-available/pikrellcam 2>/dev/null
then
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_RESTART="yes"
fi
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
NGINX_RESTART="yes"
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
NGINX_RESTART="yes"
fi
if [ "$NGINX_RESTART" == "yes" ]
then
echo "Restarting nginx"
sudo service nginx restart
fi
# =============== Setup FIFO ===============
#
fifo=$PWD/www/FIFO
if [ ! -p "$fifo" ]
then
rm -f $fifo
mkfifo $fifo
fi
sudo chown pi.www-data $fifo
sudo chmod 664 $fifo
# =============== Setup Password ===============
#
SESSION_PATH=$PWD/www/session
if [ ! -f www/password.php ]
then
WRITE_PASSWORD=yes
else
if ! grep -q $PASSWORD www/password.php 2>/dev/null \
|| ! grep -q $SESSION_PATH www/password.php 2>/dev/null
then
WRITE_PASSWORD=yes
fi
fi
if [ "$WRITE_PASSWORD" == "yes" ]
then
if [ "$PASSWORD" == "unset" ]
then
echo "Setting web page password to not require a password."
else
echo "Setting web page password file with password: $PASSWORD"
fi
cat << EOF > www/password.php
<?php
define("PASSWORD", "$PASSWORD");
define("SESSION_PATH", "$SESSION_PATH");
?>
EOF
fi
if [ ! -d $SESSION_PATH ]
then
mkdir $SESSION_PATH
fi
sudo chown pi.www-data $SESSION_PATH
sudo chmod 775 $SESSION_PATH
# =============== copy scripts-dist into scripts ===============
#
if [ ! -d scripts ]
then
mkdir scripts
fi
cd scripts-dist
for script in *
do
if [ ! -f ../scripts/$script ] && [ "$script" != "init" ]
then
cp $script ../scripts
fi
done