-
Notifications
You must be signed in to change notification settings - Fork 12
/
setup_service_raspbian.sh
executable file
·163 lines (137 loc) · 5.06 KB
/
setup_service_raspbian.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
#!/bin/bash
# This file is part of VisiCam. (https://github.com/t-oster/VisiCam)
#
# VisiCam is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# VisiCam is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with VisiCam. If not, see <http://www.gnu.org/licenses/>.
# ##################################################################
# ROOT
# ##################################################################
# Check if script is executed as root, if not, quit with error message
if [[ $EUID -ne 0 ]]
then
echo "[ERROR] Autostart VisiCam service script must be started as root, try sudo command"
exit 1
fi
# ##################################################################
# MAIN
# ##################################################################
# Change working directory to local directory
cd "$(dirname $0)"
# Check for dummy file
if ! [ -e "init-scripts/raspbian_template" ]
then
echo "[ERROR] Could not find file init-scripts/raspbian_template"
exit 1
fi
# Check for local jar path
if ! [ -e "dist/VisiCam.jar" ]
then
echo "[ERROR] Could not find file dist/VisiCam.jar"
exit 1
fi
# Check if dummy file contains lines which need to be replaced
if ! ( ( cat "init-scripts/raspbian_template" | grep '^VISICAM_JAR=TEMPLATE-LOCAL-JAR-PATH$' ) > "/dev/null" )
then
echo "[ERROR] Could not find replace line VISICAM_JAR in file init-scripts/raspbian_template"
exit 1
fi
if ! ( ( cat "init-scripts/raspbian_template" | grep '^VISICAM_ROOT=TEMPLATE-ABSOLUTE-ROOT-PATH$' ) > "/dev/null" )
then
echo "[ERROR] Could not find replace line VISICAM_ROOT in file init-scripts/raspbian_template"
exit 1
fi
if ! ( ( cat "init-scripts/raspbian_template" | grep '^PORT=TEMPLATE-PORT$' ) > "/dev/null" )
then
echo "[ERROR] Could not find replace line PORT in file init-scripts/raspbian_template"
exit 1
fi
if ! ( ( cat "init-scripts/raspbian_template" | grep '^DAEMONUSER=TEMPLATE-USER$' ) > "/dev/null" )
then
echo "[ERROR] Could not find replace line DAEMONUSER in file init-scripts/raspbian_template"
exit 1
fi
if ! ( ( cat "init-scripts/raspbian_template" | grep '^DAEMONGROUP=TEMPLATE-GROUP$' ) > "/dev/null" )
then
echo "[ERROR] Could not find replace line DAEMONGROUP in file init-scripts/raspbian_template"
exit 1
fi
# Get directory
DIRECTORY=$( pwd )
# Ask for port
echo ""
echo "[INFO] Please enter the web port at which your VisiCam installation should start."
echo "[INFO] (Default: 8080)"
read -p "Port: " PORT
echo ""
# Ask for user
echo "[INFO] Please enter the name of the user, who is used to automatically start the service."
echo "[INFO] Note: User must have write access to /var/run/ and /var/log/ !"
echo "[INFO] (Default: root)"
read -p "User: " USER
echo ""
# Copy dummy file to target
cp "init-scripts/raspbian_template" "/etc/init.d/visicam"
# Check if copy was successful
if [ $? -ne 0 ]
then
echo "[ERROR] Could not copy init-scripts/raspbian_template file to: /etc/init.d/visicam"
exit 1
fi
# Change permission settings for script
chmod +x "/etc/init.d/visicam"
# Check if chmod was successful
if [ $? -ne 0 ]
then
echo "[ERROR] Could not add execute permission to file /etc/init.d/visicam"
exit 1
fi
# Replace lines in target file
# Note: Need to change delimiter for sed from / to : since slashes might be part of directory path
sed -r -i "s:^VISICAM_JAR=TEMPLATE-LOCAL-JAR-PATH$:VISICAM_JAR=dist/VisiCam.jar:g" "/etc/init.d/visicam"
if [ $? -ne 0 ]
then
echo "[ERROR] Could not replace line VISICAM_JAR in file /etc/init.d/visicam"
exit 1
fi
sed -r -i "s:^VISICAM_ROOT=TEMPLATE-ABSOLUTE-ROOT-PATH$:VISICAM_ROOT=$DIRECTORY/:g" "/etc/init.d/visicam"
if [ $? -ne 0 ]
then
echo "[ERROR] Could not replace line VISICAM_ROOT in file /etc/init.d/visicam"
exit 1
fi
sed -r -i "s:^PORT=TEMPLATE-PORT$:PORT=$PORT:g" "/etc/init.d/visicam"
if [ $? -ne 0 ]
then
echo "[ERROR] Could not replace line PORT in file /etc/init.d/visicam"
exit 1
fi
sed -r -i "s:^DAEMONUSER=TEMPLATE-USER$:DAEMONUSER=$USER:g" "/etc/init.d/visicam"
if [ $? -ne 0 ]
then
echo "[ERROR] Could not replace line DAEMONUSER in file /etc/init.d/visicam"
exit 1
fi
sed -r -i "s:^DAEMONGROUP=TEMPLATE-GROUP$:DAEMONGROUP=$USER:g" "/etc/init.d/visicam"
if [ $? -ne 0 ]
then
echo "[ERROR] Could not replace line DAEMONGROUP in file /etc/init.d/visicam"
exit 1
fi
# Add auto start entry for system boot, if it does not exist yet
if ! ( ( ls -l "/etc/rc2.d" | grep visicam ) > "/dev/null" )
then
echo "[INFO] Creating auto start entry for visicam"
update-rc.d visicam defaults
fi
# Success message
echo "[INFO] Service visicam successfully configured for automatic system start"