-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathbuild.sh
executable file
·258 lines (231 loc) · 6.59 KB
/
build.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
#!/usr/bin/env bash
#### crowsnest - A webcam Service for multiple Cams and Stream Services.
####
#### Written by Stephan Wendel aka KwadFan <[email protected]>
#### Copyright 2021 - 2024
#### Co-authored by Patrick Gehrsitz aka mryel00 <[email protected]>
#### Copyright 2024 - till today
#### https://github.com/mainsail-crew/crowsnest
####
#### This File is distributed under GPLv3
####
# shellcheck enable=require-variable-braces
### Disable SC2317 due Trap usage
# shellcheck disable=SC2317
# Exit on errors
set -Ee
# Debug
# set -x
# Global vars
# Base Path
BASE_CN_BIN_PATH="$(dirname "$(readlink -f "${0}")")"
# Clone Flags
CLONE_FLAGS=(--depth=1 --single-branch)
# Ustreamer repo
USTREAMER_PATH="ustreamer"
if [[ -z "${CROWSNEST_USTREAMER_REPO_SHIP}" ]]; then
CROWSNEST_USTREAMER_REPO_SHIP="https://github.com/pikvm/ustreamer.git"
fi
if [[ -z "${CROWSNEST_USTREAMER_REPO_BRANCH}" ]]; then
CROWSNEST_USTREAMER_REPO_BRANCH="v6.10"
fi
# Camera-streamer repo
CSTREAMER_PATH="camera-streamer"
if [[ -z "${CROWSNEST_CAMERA_STREAMER_REPO_SHIP}" ]]; then
CROWSNEST_CAMERA_STREAMER_REPO_SHIP="https://github.com/ayufan/camera-streamer.git"
fi
if [[ -z "${CROWSNEST_CAMERA_STREAMER_REPO_BRANCH}" ]]; then
CROWSNEST_CAMERA_STREAMER_REPO_BRANCH="master"
fi
# Paths of repos
ALL_PATHS=(
"${BASE_CN_BIN_PATH}"/"${USTREAMER_PATH}"
"${BASE_CN_BIN_PATH}"/"${CSTREAMER_PATH}"
)
# Helper messages
show_help() {
printf "Usage %s [options]\n" "$(basename "${0}")"
printf "\t-h or --help\t\tShows this help message\n"
printf "\t-b or --build\t\tBuild Apps\n"
printf "\t-c or --clean\t\tClean Apps\n"
printf "\t-d or --delete\t\tDelete cloned Apps\n"
printf "\t-r or --reclone\t\tClone Apps again\n\n"
}
## Helper funcs
### Check if device is Raspberry Pi
is_raspberry_pi() {
if [[ -f /proc/device-tree/model ]] &&
grep -q "Raspberry" /proc/device-tree/model; then
echo "1"
else
echo "0"
fi
}
is_bookworm() {
if [[ -f /etc/os-release ]]; then
grep -cq "bookworm" /etc/os-release &> /dev/null && echo "1" || echo "0"
fi
}
is_pi5() {
if [[ -f /proc/device-tree/model ]] &&
grep -q "Raspberry Pi 5" /proc/device-tree/model; then
echo "1"
else
echo "0"
fi
}
is_ubuntu_arm() {
if [[ "$(is_raspberry_pi)" = "1" ]] &&
grep -q "ubuntu" /etc/os-release; then
echo "1"
else
echo "0"
fi
}
### Get avail mem
get_avail_mem() {
grep "MemTotal" /proc/meminfo | awk '{print $2}'
}
## MAIN funcs
### Delete repo folder
delete_apps() {
for path in "${ALL_PATHS[@]}"; do
if [[ ! -d "${path}" ]]; then
printf "'%s' does not exist! Delete ... [SKIPPED]\n" "${path}"
fi
if [[ -d "${path}" ]]; then
printf "Deleting '%s' ... [DONE]\n" "${path}"
rm -rf "${path}"
fi
done
}
### Clone ustreamer
clone_ustreamer() {
if [[ -d "${BASE_CN_BIN_PATH}"/"${USTREAMER_PATH}" ]]; then
printf "%s already exist ... [SKIPPED]\n" "${USTREAMER_PATH}"
return
fi
git clone "${CROWSNEST_USTREAMER_REPO_SHIP}" \
-b "${CROWSNEST_USTREAMER_REPO_BRANCH}" \
"${BASE_CN_BIN_PATH}"/"${USTREAMER_PATH}" \
"${CLONE_FLAGS[@]}"
}
### Clone camera-streamer
clone_cstreamer() {
## Special handling because only supported on Raspberry Pi
[[ -n "${CROWSNEST_UNATTENDED}" ]] || CROWSNEST_UNATTENDED="0"
if { [[ "$(is_raspberry_pi)" = "0" ]] ||
[[ "$(is_pi5)" = "1" ]] ||
[[ "$(is_ubuntu_arm)" = "1" ]]; } &&
[[ "${CROWSNEST_UNATTENDED}" = "0" ]]; then
printf "Device is not supported! Cloning camera-streamer ... [SKIPPED]\n"
return
fi
if [[ -d "${BASE_CN_BIN_PATH}"/"${CSTREAMER_PATH}" ]]; then
printf "%s already exist ... [SKIPPED]\n" "${CSTREAMER_PATH}"
return
fi
if [[ "$(is_bookworm)" = "1" ]]; then
printf "\nBookworm detected!\n"
printf "Using main branch of camera-streamer for Bookworm ...\n\n"
CROWSNEST_CAMERA_STREAMER_REPO_BRANCH="main"
fi
git clone "${CROWSNEST_CAMERA_STREAMER_REPO_SHIP}" \
-b "${CROWSNEST_CAMERA_STREAMER_REPO_BRANCH}" \
"${BASE_CN_BIN_PATH}"/"${CSTREAMER_PATH}" \
"${CLONE_FLAGS[@]}" --recurse-submodules --shallow-submodules
}
### Clone Apps
clone_apps() {
local apps
apps="ustreamer cstreamer"
for app in ${apps}; do
clone_"${app}"
done
}
### Run 'make clean' in cloned folders
clean_apps() {
for app in "${ALL_PATHS[@]}"; do
if [[ ! -d "${app}" ]]; then
printf "'%s' does not exist! Clean ... [SKIPPED]\n" "${app}"
else
printf "\nRunning 'make clean' in %s ... \n" "${app}"
pushd "${app}" &> /dev/null || exit 1
make clean
popd &> /dev/null || exit 1
fi
done
printf "\nRunning 'make clean' ... [DONE]\n"
}
build_apps() {
## Determine Ramsize and export MAKEFLAG
if [[ "$(get_avail_mem)" -le 524288 ]]; then
USE_PROCS=-j1
elif [[ "$(get_avail_mem)" -le 1048576 ]]; then
USE_PROCS=-j2
else
USE_PROCS=-j4
fi
for path in "${ALL_PATHS[@]}"; do
if [[ ! -d "${path}" ]]; then
printf "'%s' does not exist! Build skipped ... [WARN]\n" "${path}"
fi
if [[ -d "${path}" ]]; then
printf "Build '%s' using ${USE_PROCS##-j} Cores ... \n" "${path##*/}"
pushd "${path}" &> /dev/null || exit 1
make "${USE_PROCS}"
popd &> /dev/null || exit 1
printf "Build '%s' ... [SUCCESS]\n" "${path##*/}"
fi
done
}
## MAIN FUNC
main() {
## Error exit if no args given, show help
if [[ $# -eq "0" ]]; then
printf "ERROR: No options given ...\n"
show_help
exit 1
fi
## Error exit if too many args given
if [[ $# -gt "1" ]]; then
printf "ERROR: Too many options given ...\n"
show_help
exit 1
fi
## Get opts
while true; do
case "${1}" in
-b|--build)
build_apps
break
;;
-c|--clean)
clean_apps
break
;;
-d|--delete)
delete_apps
break
;;
-h|--help)
show_help
break
;;
-r|--reclone)
delete_apps
clone_apps
break
;;
*)
printf "Unknown option: %s" "${1}"
show_help
break
;;
esac
done
}
#### MAIN
main "${@}"
exit 0
#### EOF