-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
build.sh
executable file
·1779 lines (1646 loc) · 47.4 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
#
#SPDX-License-Identifier: GPL-3.0-or-later
#myMPD (c) 2018-2024 Juergen Mang <mail@jcgames.de>
#https://github.com/jcorporation/mympd
#exit on error
set -e
#exit on undefined variable
set -u
#print out commands
[ -z "${DEBUG+x}" ] || set -x
#get action
if [ -z "${1+x}" ]
then
ACTION=""
else
ACTION="$1"
fi
#colorful warnings and errors
echo_error() {
printf "\e[0;31mERROR: "
#shellcheck disable=SC2068
echo $@
printf "\e[m"
}
echo_warn() {
printf "\e[1;33mWARN: "
#shellcheck disable=SC2068
echo $@
printf "\e[m"
}
#save script path and change to it
STARTPATH=$(dirname "$(realpath "$0")")
cd "$STARTPATH" || exit 1
#set umask
umask 0022
#get myMPD version
VERSION=$(grep " VERSION" CMakeLists.txt | sed 's/ VERSION //')
COPYRIGHT="myMPD ${VERSION} | (c) 2018-2024 Juergen Mang <mail@jcgames.de> | SPDX-License-Identifier: GPL-3.0-or-later | https://github.com/jcorporation/mympd"
# Minify JavaScript only for master branch
if [ -z "${MYMPD_MINIFY_JS+x}" ]
then
MYMPD_MINIFY_JS="1"
if [ -f .git/HEAD ] && ! grep -q "master" .git/HEAD
then
MYMPD_MINIFY_JS="0"
fi
fi
#check for command
check_cmd() {
for DEPENDENCY in "$@"
do
if ! check_cmd_silent "$@"
then
echo_error "${DEPENDENCY} not found"
return 1
fi
done
return 0
}
check_cmd_silent() {
for DEPENDENCY in "$@"
do
if ! command -v "${DEPENDENCY}" > /dev/null
then
return 1
fi
done
return 0
}
if [ "$ACTION" != "installdeps" ] && [ "$ACTION" != "" ]
then
check_cmd gzip perl
ZIP="gzip -n -f -v -9"
ZIPCAT="gzip -n -v -9 -c"
fi
setversion() {
TS=$(stat -c%Y CMakeLists.txt)
export LC_TIME="en_GB.UTF-8"
DATE_F1=$(date --date=@"${TS}" +"%a %b %d %Y")
DATE_F2=$(date --date=@"${TS}" +"%a, %d %b %Y %H:%m:%S %z")
DATE_F3=$(date --date=@"${TS}" +"%d %b %Y")
echo "Setting version to ${VERSION} and date to ${DATE_F2}"
for F in contrib/packaging/alpine/APKBUILD contrib/packaging/arch/PKGBUILD \
contrib/packaging/rpm/mympd.spec contrib/packaging/debian/changelog \
contrib/packaging/openwrt/Makefile contrib/man/mympd.1 contrib/man/mympd-config.1 \
contrib/man/mympd-script.1 contrib/packaging/freebsd/multimedia/mympd/Makefile
do
echo "$F"
sed -e "s/__VERSION__/${VERSION}/g" -e "s/__DATE_F1__/$DATE_F1/g" -e "s/__DATE_F2__/$DATE_F2/g" \
-e "s/__DATE_F3__/$DATE_F3/g" "$F.in" > "$F"
done
#gentoo ebuild must be moved only
if [ ! -f "contrib/packaging/gentoo/media-sound/mympd/mympd-${VERSION}.ebuild" ]
then
mv -f contrib/packaging/gentoo/media-sound/mympd/mympd-*.ebuild \
"contrib/packaging/gentoo/media-sound/mympd/mympd-${VERSION}.ebuild"
fi
printf "%s" "${VERSION}" > docs/_includes/version
BUILD=$(git log --format="%H" -n 1)
echo "const myMPDversion = '${VERSION}';" > htdocs/js/version.js
echo "const myMPDbuild = '${BUILD}';" >> htdocs/js/version.js
}
minify() {
TYPE="$1"
SRC="$2"
DST="$3"
#We remove only line-breaks, comments, blank lines and trim whitespaces
echo "Minifying $SRC"
if [ "$TYPE" = "html" ]
then
#shellcheck disable=SC2016
if ! perl -pe 's/^<!--debug-->.*\n//gm; s/<!--release\s+(.+)-->/$1/g; s/<!--(.+)-->//g; s/^\s*//gm; s/\s*$//gm' "$SRC" > "${DST}.tmp"
then
rm -f "${DST}.tmp"
echo_error "Error minifying $SRC"
exit 1
fi
elif [ "$TYPE" = "js" ]
then
#shellcheck disable=SC2016
if [ "$MYMPD_MINIFY_JS" = "0" ]
then
cp "$SRC" "${DST}.tmp"
else
if ! perl -pe 's/^\s*//gm; s/^\s*\/?\*.*$//g; s/^\/\/.+$//g; s/^logDebug\(.*$//g; s/\/\*debug\*\/.*$//g; s/\s*$//gm;' "$SRC" > "${DST}.tmp"
then
rm -f "${DST}.tmp"
echo_error "Error minifying $SRC"
exit 1
fi
fi
elif [ "$TYPE" = "json" ]
then
#shellcheck disable=SC2016
if ! jq -r tostring "$SRC" | tr -d '\n' > "${DST}.tmp"
then
rm -f "${DST}.tmp"
echo_error "Error minifying $SRC"
exit 1
fi
elif [ "$TYPE" = "css" ]
then
#shellcheck disable=SC2016
if ! perl -pe 's/^\s*//gm; s/\s*$//gm; s/: /:/g;' "$SRC" | perl -pe 's/\/\*[^*]+\*\///g;' > "${DST}.tmp"
then
rm -f "${DST}.tmp"
echo_error "Error minifying $SRC"
exit 1
fi
fi
#successfull minified file
echo "" >> "${DST}.tmp"
mv "${DST}.tmp" "$DST"
return 0
}
createassets() {
check_cmd jq
[ -z "${MYMPD_BUILDDIR+x}" ] && MYMPD_BUILDDIR="release"
echo "Creating assets in $MYMPD_BUILDDIR"
#Recreate asset directories
rm -fr "$MYMPD_BUILDDIR/htdocs"
install -d "$MYMPD_BUILDDIR/htdocs/js"
install -d "$MYMPD_BUILDDIR/htdocs/css"
install -d "$MYMPD_BUILDDIR/htdocs/assets/i18n"
#Create translation phrases file
check_phrases
createi18n 2>/dev/null
minify js "$MYMPD_BUILDDIR/htdocs/js/i18n.js" "$MYMPD_BUILDDIR/htdocs/js/i18n.min.js"
#Create defines
create_js_defines
echo "Minifying javascript"
JSSRCFILES=""
#shellcheck disable=SC2013
for F in $(grep -E '<!--debug-->\s+<script' htdocs/index.html | cut -d\" -f2)
do
[ "$F" = "js/bootstrap-native.js" ] && continue
[ "$F" = "js/i18n.js" ] && continue
[ "$F" = "js/long-press-event.js" ] && continue
JSSRCFILES="$JSSRCFILES htdocs/$F"
if tail -1 "htdocs/$F" | perl -npe 'exit 1 if m/\n/; exit 0'
then
echo_error "$F don't end with newline character"
exit 1
fi
done
echo "Creating mympd.js"
#shellcheck disable=SC2086
#shellcheck disable=SC2002
cat $JSSRCFILES | grep -v "\"use strict\";" > "$MYMPD_BUILDDIR/htdocs/js/mympd.js"
minify js htdocs/sw.js "$MYMPD_BUILDDIR/htdocs/sw.min.js"
minify js "$MYMPD_BUILDDIR/htdocs/js/mympd.js" "$MYMPD_BUILDDIR/htdocs/js/mympd.min.js"
echo "Combining and compressing javascript"
echo "//${COPYRIGHT}" > "$MYMPD_BUILDDIR/htdocs/js/copyright.min.js"
if [ "$MYMPD_MINIFY_JS" = "0" ]
then
JSFILES="dist/bootstrap-native/bootstrap-native.js dist/long-press-event/long-press-event.js"
else
JSFILES="dist/bootstrap-native/bootstrap-native.min.js dist/long-press-event/long-press-event.min.js"
fi
JSFILES="$JSFILES $MYMPD_BUILDDIR/htdocs/js/*.min.js"
for F in $JSFILES
do
if tail -1 "$F" | perl -npe 'exit 1 if m/\n/; exit 0'
then
echo_error "$F don't end with newline character"
exit 1
fi
done
echo "\"use strict\";" > "$MYMPD_BUILDDIR/htdocs/js/combined.js"
#shellcheck disable=SC2086
#shellcheck disable=SC2002
cat $JSFILES >> "$MYMPD_BUILDDIR/htdocs/js/combined.js"
$ZIP "$MYMPD_BUILDDIR/htdocs/js/combined.js"
#serviceworker
$ZIPCAT "$MYMPD_BUILDDIR/htdocs/sw.min.js" > "$MYMPD_BUILDDIR/htdocs/sw.js.gz"
echo "Minifying stylesheets"
for F in htdocs/css/*.css
do
[ "$F" = "htdocs/css/bootstrap.css" ] && continue
DST=$(basename "$F" .css)
minify css "$F" "$MYMPD_BUILDDIR/htdocs/css/${DST}.min.css"
done
echo "Combining and compressing stylesheets"
echo "/* ${COPYRIGHT} */" > "$MYMPD_BUILDDIR/htdocs/css/copyright.min.css"
CSSFILES="dist/bootstrap/compiled/custom.css $MYMPD_BUILDDIR/htdocs/css/*.min.css"
#shellcheck disable=SC2086
cat $CSSFILES > "$MYMPD_BUILDDIR/htdocs/css/combined.css"
$ZIP "$MYMPD_BUILDDIR/htdocs/css/combined.css"
echo "Compressing i18n json"
jq -r "select(.missingPhrases < 100) | keys[]" "$STARTPATH/src/i18n/json/i18n.json" | grep -v "default" | \
while read -r CODE
do
minify json "$STARTPATH/src/i18n/json/${CODE}.json" "$MYMPD_BUILDDIR/htdocs/assets/i18n/${CODE}.min.json"
$ZIPCAT "$MYMPD_BUILDDIR/htdocs/assets/i18n/${CODE}.min.json" > "$MYMPD_BUILDDIR/htdocs/assets/i18n/${CODE}.json.gz"
done
echo "Minifying and compressing html"
minify html htdocs/index.html "$MYMPD_BUILDDIR/htdocs/index.html"
$ZIPCAT "$MYMPD_BUILDDIR/htdocs/index.html" > "$MYMPD_BUILDDIR/htdocs/index.html.gz"
echo "Creating other compressed assets"
ASSETS="htdocs/mympd.webmanifest htdocs/assets/*.svg"
for ASSET in $ASSETS
do
$ZIPCAT "$ASSET" > "$MYMPD_BUILDDIR/${ASSET}.gz"
done
echo "Copy images"
cp -v htdocs/assets/*.png "$MYMPD_BUILDDIR/htdocs/assets/"
echo "Copy webfont"
cp -v dist/material-icons/MaterialIcons-Regular.woff2 "$MYMPD_BUILDDIR/htdocs/assets/"
$ZIPCAT dist/material-icons/ligatures.json > "$MYMPD_BUILDDIR/htdocs/assets/ligatures.json.gz"
[ -z "${MYMPD_ENABLE_LUA+x}" ] && MYMPD_ENABLE_LUA="ON"
if [ "${MYMPD_ENABLE_LUA}" = "on" ] || [ "${MYMPD_ENABLE_LUA}" = "ON" ]
then
lualibs
else
echo "Skip creation of lua libraries"
fi
return 0
}
lualibs() {
[ -z "${MYMPD_ENABLE_MYGPIOD+x}" ] && MYMPD_ENABLE_MYGPIOD="OFF"
[ -z "${MYMPD_BUILDDIR+x}" ] && MYMPD_BUILDDIR="release"
echo "Copy integrated lua libraries"
mkdir -p "$MYMPD_BUILDDIR/contrib/lualibs"
cp -v contrib/lualibs/json.lua "$MYMPD_BUILDDIR/contrib/lualibs/"
cp -v contrib/lualibs/mympd/00-start.lua "$MYMPD_BUILDDIR/contrib/lualibs/mympd.lua"
cat contrib/lualibs/mympd/10-mympd.lua >> "$MYMPD_BUILDDIR/contrib/lualibs/mympd.lua"
cat contrib/lualibs/mympd/20-http.lua >> "$MYMPD_BUILDDIR/contrib/lualibs/mympd.lua"
cat contrib/lualibs/mympd/30-execute.lua >> "$MYMPD_BUILDDIR/contrib/lualibs/mympd.lua"
[ "$MYMPD_ENABLE_MYGPIOD" = "ON" ] && cat contrib/lualibs/mympd/40-mygpiod.lua >> "$MYMPD_BUILDDIR/contrib/lualibs/mympd.lua"
cat contrib/lualibs/mympd/50-util.lua >> "$MYMPD_BUILDDIR/contrib/lualibs/mympd.lua"
cat contrib/lualibs/mympd/60-caches.lua >> "$MYMPD_BUILDDIR/contrib/lualibs/mympd.lua"
cat contrib/lualibs/mympd/70-string.lua >> "$MYMPD_BUILDDIR/contrib/lualibs/mympd.lua"
cat contrib/lualibs/mympd/99-end.lua >> "$MYMPD_BUILDDIR/contrib/lualibs/mympd.lua"
echo "Compiling lua libraries"
LUAC=$(command -v luac5.4 2> /dev/null || command -v luac5.3 2> /dev/null || command -v luac 2> /dev/null || true)
if [ -z "$LUAC" ]
then
echo_error "luac not found"
exit 1
fi
if ! $LUAC -s -o "$MYMPD_BUILDDIR/contrib/lualibs/mympd.luac" "$MYMPD_BUILDDIR/contrib/lualibs/mympd.lua"
then
exit 1
fi
if ! $LUAC -s -o "$MYMPD_BUILDDIR/contrib/lualibs/json.luac" "$MYMPD_BUILDDIR/contrib/lualibs/json.lua"
then
exit 1
fi
}
buildrelease() {
BUILD_TYPE=$1
echo "Compiling myMPD v${VERSION}"
cmake -B release \
-DCMAKE_INSTALL_PREFIX:PATH=/usr \
-DCMAKE_BUILD_TYPE="$BUILD_TYPE" \
.
make -j4 -C release
}
addmympduser() {
echo "Checking status of mympd system user and group"
if ! getent group mympd > /dev/null
then
if check_cmd_silent groupadd
then
groupadd -r mympd
elif check_cmd_silent addgroup
then
#alpine
addgroup -S mympd
else
echo_error "Can not add group mympd"
return 1
fi
fi
if ! getent passwd mympd > /dev/null
then
if check_cmd_silent useradd
then
useradd -r -g mympd -s /bin/false -d /var/lib/mympd mympd
elif check_cmd_silent adduser
then
#alpine
adduser -S -D -H -h /var/lib/mympd -s /sbin/nologin -G mympd -g myMPD mympd
else
echo_error "Can not add user mympd"
return 1
fi
fi
return 0
}
installrelease() {
echo "Installing myMPD"
cd release || exit 1
[ -z "${DESTDIR+x}" ] && DESTDIR=""
make install DESTDIR="$DESTDIR"
if [ ! -f /usr/lib/systemd/system/mympd.service ] &&
[ ! -f /usr/systemd/system/mympd.service ]
then
addmympduser
else
echo "Systemd found skipping mympd user creation"
fi
echo "myMPD installed"
}
copyassets() {
echo "Copy dist assets"
[ -z "${MYMPD_BUILDDIR+x}" ] && MYMPD_BUILDDIR="debug"
cp -v "$STARTPATH/dist/bootstrap/compiled/custom.css" "$STARTPATH/htdocs/css/bootstrap.css"
cp -v "$STARTPATH/dist/bootstrap-native/bootstrap-native.js" "$STARTPATH/htdocs/js/bootstrap-native.js"
cp -v "$STARTPATH/dist/long-press-event/long-press-event.js" "$STARTPATH/htdocs/js/long-press-event.js"
cp -v "$STARTPATH/dist/material-icons/MaterialIcons-Regular.woff2" "$STARTPATH/htdocs/assets/MaterialIcons-Regular.woff2"
cp -v "$STARTPATH/dist/material-icons/ligatures.json" "$STARTPATH/htdocs/assets/ligatures.json"
lualibs
#Create defines
create_js_defines
#translation files
check_phrases
createi18n
cp -v "$MYMPD_BUILDDIR/htdocs/js/i18n.js" "$STARTPATH/htdocs/js/i18n.js"
rm -fr "$STARTPATH/htdocs/assets/i18n/"
install -d "$STARTPATH/htdocs/assets/i18n"
jq -r "select(.missingPhrases < 100) | keys[]" "$STARTPATH/src/i18n/json/i18n.json" | grep -v "default" | \
while read -r CODE
do
minify json "$STARTPATH/src/i18n/json/${CODE}.json" "$STARTPATH/htdocs/assets/i18n/${CODE}.json"
done
}
builddebug() {
echo "Compiling myMPD v${VERSION}"
CMAKE_SANITIZER_OPTIONS=""
case "$ACTION" in
asan) CMAKE_SANITIZER_OPTIONS="-DMYMPD_ENABLE_ASAN=ON" ;;
tsan) CMAKE_SANITIZER_OPTIONS="-DMYMPD_ENABLE_TSAN=ON" ;;
ubsan) CMAKE_SANITIZER_OPTIONS="-DMYMPD_ENABLE_UBSAN=ON" ;;
esac
cmake -B debug \
-DCMAKE_INSTALL_PREFIX:PATH=/usr \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
$CMAKE_SANITIZER_OPTIONS \
.
make -j4 -C debug VERBOSE=1
echo "Linking compilation database"
sed -e 's/\\t/ /g' -e 's/-Wformat-truncation//g' -e 's/-Wformat-overflow=2//g' -e 's/-fsanitize=bounds-strict//g' \
-e 's/-Wno-stringop-overread//g' -e 's/-fstack-clash-protection//g' \
debug/compile_commands.json > src/compile_commands.json
}
buildtest() {
echo "Compiling and running unit tests"
cmake -B debug \
-DCMAKE_INSTALL_PREFIX:PATH=/usr \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DMYMPD_ENABLE_ASAN=ON \
-DMYMPD_BUILD_TESTING=ON \
.
make -j4 -C debug
make -j4 -C debug test
echo "Linking compilation database"
sed -e 's/\\t/ /g' -e 's/-Wformat-truncation//g' -e 's/-Wformat-overflow=2//g' -e 's/-fsanitize=bounds-strict//g' \
-e 's/-Wno-stringop-overread//g' -e 's/-fstack-clash-protection//g' \
debug/compile_commands.json > test/compile_commands.json
}
cleanup() {
[ -z "${MYMPD_BUILDDIR+x}" ] && MYMPD_BUILDDIR="release"
#build directories
rm -rf "$MYMPD_BUILDDIR"
rm -rf release
rm -rf debug
rm -rf package
rm -rf test/build
#htdocs
rm -f htdocs/sw.js
rm -f htdocs/js/bootstrap-native.js
rm -f htdocs/js/long-press-event.js
rm -f htdocs/js/i18n.js
rm -f htdocs/css/bootstrap.css
rm -f htdocs/assets/MaterialIcons-Regular.woff2
rm -f htdocs/assets/ligatures.json
rm -rf htdocs/assets/i18n
#generated documentation
rm -rf _site
rm -rf docs/doxygen
rm -rf docs/jsdoc
#compilation database
rm -f src/compile_commands.json
#caches
rm -fr src/.cache
rm -fr .cache
#node modules
rm -fr dist/bootstrap/node_modules
#clang tidy
rm -f clang-tidy.out
}
cleanuposc() {
rm -rf osc
}
check_docs() {
rc=0
METHODS=$(grep -v '//' src/lib/api.h | grep 'X(MYMPD' | cut -d\( -f2 | cut -d\) -f1)
for METHOD in $METHODS
do
if ! grep -q "$METHOD" htdocs/js/apidoc.js
then
echo_warn "API $METHOD not documented"
rc=1
fi
done
return "$rc"
}
check_includes() {
rc=0
FILES=$(find src/ -name \*.c)
for FILE in $FILES
do
if ! grep -m1 "#include" "$FILE" | grep -q "compile_time.h"
then
echo_warn "First include is not compile_time.h: $FILE"
rc=1
fi
INCLUDES=$(grep "#include \"" "$FILE" | grep -v "compile_time.h" | cut -d\" -f2)
for INCLUDE in $INCLUDES
do
if ! realpath "$INCLUDE" > /dev/null 2>&1
then
echo_error "Wrong include path in $FILE for $INCLUDE"
rc=1
fi
done
return "$rc"
done
}
check_file() {
FILE=$1
if check_cmd cppcheck
then
echo "Running cppcheck"
[ -z "${CPPCHECKOPTS+z}" ] && CPPCHECKOPTS="-q --force --enable=warning"
#shellcheck disable=SC2086
cppcheck $CPPCHECKOPTS "$FILE"
else
echo_warn "cppcheck not found"
fi
if check_cmd flawfinder
then
echo "Running flawfinder"
[ -z "${FLAWFINDEROPTS+z}" ] && FLAWFINDEROPTS="-m3 --quiet --dataonly"
#shellcheck disable=SC2086
flawfinder $FLAWFINDEROPTS "$FILE"
else
echo_warn "flawfinder not found"
fi
if [ ! -f src/compile_commands.json ]
then
echo "src/compile_commands.json not found"
echo "run: ./build.sh debug"
exit 1
fi
if check_cmd clang-tidy
then
echo "Running clang-tidy"
rm -f clang-tidy.out
clang-tidy --config-file="$STARTPATH/.clang-tidy" "$FILE" > ../clang-tidy.out 2>/dev/null
grep -v -E "(/usr/include/|memset|memcpy|_XOPEN_SOURCE|\^)" ../clang-tidy.out
else
echo_warn "clang-tidy not found"
fi
}
check() {
check_phrases
if ! check_docs
then
return 1
fi
if ! check_includes
then
return 1
fi
if [ ! -f src/compile_commands.json ]
then
echo "src/compile_commands.json not found"
echo "run: ./build.sh debug"
return 1
fi
if check_cmd clang-tidy
then
echo "Running clang-tidy"
rm -f clang-tidy.out
cd src || exit 1
find ./ -name '*.c' -exec clang-tidy \
--config-file="$STARTPATH/.clang-tidy" {} \; >> ../clang-tidy.out 2>/dev/null
ERRORS=$(grep -v -E "(/usr/include/|memset|memcpy|_XOPEN_SOURCE|\^)" ../clang-tidy.out)
if [ -n "$ERRORS" ]
then
echo "$ERRORS"
return 1
fi
cd .. || return 1
else
echo_warn "clang-tidy not found"
return 1
fi
return 0
}
prepare() {
cleanup
SRC=$(ls -d "$STARTPATH"/* -1)
mkdir -p package/build
cd package/build || exit 1
for F in $SRC
do
[ "$F" = "$STARTPATH/osc" ] && continue
[ "$F" = "$STARTPATH/builder" ] && continue
cp -a "$F" .
rm -fr src/.cache
done
}
pkgdebian() {
check_cmd dpkg-buildpackage
prepare
cp -a contrib/packaging/debian .
export LC_TIME="en_GB.UTF-8"
tar -czf "../mympd_${VERSION}.orig.tar.gz" -- *
SIGNOPT="--no-sign"
if [ -n "${SIGN+x}" ] && [ "$SIGN" = "TRUE" ]
then
SIGNOPT="--sign-key=$GPGKEYID"
else
echo_warn "Package would not be signed"
fi
#shellcheck disable=SC2086
dpkg-buildpackage -rfakeroot $SIGNOPT
#get created package name
PACKAGE=$(ls ../mympd_"${VERSION}"-1_*.deb)
if [ "$PACKAGE" = "" ]
then
echo_error "Can't find package"
fi
if check_cmd lintian
then
echo "Checking package with lintian"
lintian "$PACKAGE"
else
echo_warn "lintian not found, can't check package"
fi
}
pkgdocker() {
check_cmd docker
[ -z "${DOCKERFILE+x}" ] && DOCKERFILE="Dockerfile.alpine"
prepare
docker build --rm -t mympd -f "contrib/packaging/docker/$DOCKERFILE" .
}
pkgbuildx() {
check_cmd docker
if [ ! -x ~/.docker/cli-plugins/docker-buildx ]
then
echo "Docker buildx not found"
echo "Quick start:"
echo " 1. Download plugin: https://github.com/docker/buildx/releases/latest"
echo " 2. Save it to file: ~/.docker/cli-plugins/docker-buildx"
echo " 3. Make it executeable: chmod +x ~/.docker/cli-plugins/docker-buildx"
echo ""
echo "More info: https://www.docker.com/blog/getting-started-with-docker-for-arm-on-linux/"
exit 1
fi
[ -z "${DOCKERFILE+x}" ] && DOCKERFILE="Dockerfile.alpine"
[ -z "${PLATFORMS+x}" ] && PLATFORMS="linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6"
prepare
cp contrib/packaging/docker/"$DOCKERFILE" Dockerfile
docker run --rm --privileged docker/binfmt:820fdd95a9972a5308930a2bdfb8573dd4447ad3
if ! docker buildx ls | grep -q mympdbuilder
then
docker buildx create --name mympdbuilder
fi
docker buildx use mympdbuilder
docker buildx inspect --bootstrap
docker buildx build -t mympd --platform "$PLATFORMS" .
}
pkgalpine() {
if [ -z "${1+x}" ]
then
TARONLY=""
else
TARONLY=$1
fi
check_cmd abuild
prepare
tar -czf "mympd_${VERSION}.orig.tar.gz" -- *
[ "$TARONLY" = "taronly" ] && return 0
cp contrib/packaging/alpine/* .
abuild checksum
abuild -r
}
pkgrpm() {
if [ -z "${1+x}" ]
then
TARONLY=""
else
TARONLY=$1
fi
check_cmd rpmbuild
prepare
SRC=$(ls)
mkdir "mympd-${VERSION}"
for F in $SRC
do
mv "$F" "mympd-${VERSION}"
done
tar -czf "mympd-${VERSION}.tar.gz" "mympd-${VERSION}"
[ "$TARONLY" = "taronly" ] && return 0
install -d "$HOME/rpmbuild/SOURCES"
mv "mympd-${VERSION}.tar.gz" ~/rpmbuild/SOURCES/
cp ../../contrib/packaging/rpm/mympd.spec .
rpmbuild -ba mympd.spec
if check_cmd rpmlint
then
echo "Checking package with rpmlint"
ARCH=$(uname -p)
rpmlint "$HOME/rpmbuild/RPMS/${ARCH}/mympd-${VERSION}-0.${ARCH}.rpm"
else
echo_warn "rpmlint not found, can't check package"
fi
}
pkgarch() {
check_cmd makepkg
prepare
tar -czf "mympd_${VERSION}.orig.tar.gz" -- *
cp contrib/packaging/arch/* .
makepkg
if [ -n "${SIGN+x}" ] && [ "$SIGN" = "TRUE" ]
then
KEYARG=""
[ -z "${GPGKEYID+x}" ] || KEYARG="--key $GPGKEYID"
#shellcheck disable=SC2086
makepkg --sign $KEYARG mympd-*.pkg.tar.xz
fi
if check_cmd namcap
then
echo "Checking package with namcap"
namcap PKGBUILD
namcap "mympd-${VERSION}"*.pkg.tar.*
else
echo_warn "namcap not found, can't check package"
fi
}
pkgosc() {
[ -z "${OSC_BIN+x}" ] && OSC_BIN="$HOME/python-venv/bin/osc"
if [ ! -x "$OSC_BIN" ]
then
echo_error "Command osc not found: $HOME/python-venv/bin/osc"
exit 1
fi
cleanup
cleanuposc
if [ -z "${OSC_REPO+x}" ]
then
if [ -f .git/HEAD ] && grep -q "master" .git/HEAD
then
OSC_REPO="home:jcorporation/myMPD"
else
OSC_REPO="home:jcorporation/myMPD-devel"
fi
fi
mkdir osc
cd osc || exit 1
$OSC_BIN checkout "$OSC_REPO"
rm -f "$OSC_REPO"/*
cd "$STARTPATH" || exit 1
pkgrpm taronly
cd "$STARTPATH" || exit 1
cp "package/build/mympd-${VERSION}.tar.gz" "osc/$OSC_REPO/"
if [ -f /etc/debian_version ]
then
pkgdebian
else
pkgalpine taronly
rm -f "$OSC_REPO"/debian.*
fi
cd "$STARTPATH/osc" || exit 1
cp "../package/mympd_${VERSION}.orig.tar.gz" "$OSC_REPO/"
if [ -f /etc/debian_version ]
then
cp "../package/mympd_${VERSION}-1.dsc" "$OSC_REPO/"
cp "../package/mympd_${VERSION}-1.debian.tar.xz" "$OSC_REPO/"
fi
cp ../contrib/packaging/rpm/mympd.spec "$OSC_REPO/"
cp ../contrib/packaging/arch/PKGBUILD "$OSC_REPO/"
cd "$OSC_REPO" || exit 1
$OSC_BIN addremove
$OSC_BIN st
$OSC_BIN vc -m "Update"
$OSC_BIN commit -m "Update"
}
installdeps() {
echo "Platform: $(uname -m)"
if [ -f /etc/debian_version ]
then
apt-get update
if ! apt-get install -y --no-install-recommends liblua5.4-dev lua5.4
then
#fallback to lua 5.3 for older debian versions
apt-get install -y --no-install-recommends liblua5.3-dev lua5.3
fi
apt-get install -y --no-install-recommends \
gcc cmake perl libssl-dev libid3tag0-dev libflac-dev \
build-essential pkg-config libpcre2-dev gzip jq whiptail
elif [ -f /etc/arch-release ]
then
#arch
pacman -Sy gcc base-devel cmake perl openssl libid3tag flac lua pkgconf pcre2 gzip jq libnewt
elif [ -f /etc/alpine-release ]
then
#alpine
apk add cmake perl openssl-dev libid3tag-dev flac-dev lua5.4-dev lua5.4 \
alpine-sdk linux-headers pkgconf pcre2-dev gzip jq newt
elif [ -f /etc/SuSE-release ]
then
#suse
zypper install gcc cmake pkgconfig perl openssl-devel libid3tag-devel flac-devel \
lua-devel unzip pcre2-devel gzip jq whiptail
elif [ -f /etc/redhat-release ]
then
#fedora
yum install gcc cmake pkgconfig perl openssl-devel libid3tag-devel flac-devel \
lua-devel unzip pcre2-devel gzip jq whiptail
else
echo_warn "Unsupported distribution detected."
echo "You should manually install:"
echo " - gcc or clang"
echo " - cmake"
echo " - perl"
echo " - gzip"
echo " - jq"
echo " - whiptail"
echo " - openssl (devel)"
echo " - flac (devel)"
echo " - libid3tag (devel)"
echo " - liblua5.4 or liblua5.3 (devel)"
echo " - libpcre2 (devel)"
fi
}
updatelibmympdclient() {
check_cmd git meson
cd dist/libmympdclient || exit 1
TMPDIR=$(mktemp -d)
cd "$TMPDIR" || exit 1
git clone --depth=1 -b libmympdclient https://github.com/jcorporation/libmympdclient.git
cd libmympdclient || exit 1
meson setup . output -Dbuffer_size=8192
cd "$STARTPATH/dist/libmympdclient" || exit 1
install -d src
install -d include/mpd
install -d LICENSES
rsync -av --delete "$TMPDIR/libmympdclient/src/" ./src/
rsync -av --delete "$TMPDIR/libmympdclient/include/mpd/" ./include/mpd/
rsync -av "$TMPDIR/libmympdclient/output/include/mpd/version.h" include/mpd/version.h
rsync -av "$TMPDIR/libmympdclient/output/config.h" include/config.h
rsync -av "$TMPDIR/libmympdclient/LICENSES/" LICENSES/
rm -rf "$TMPDIR"
}
updatebootstrapnative() {
check_cmd git npm
#clone repository
TMPDIR=$(mktemp -d)
cd "$TMPDIR" || exit 1
git clone --depth=1 git@github.com:thednp/bootstrap.native.git
cd bootstrap.native
npm install vite
#copy custom config
cp "$STARTPATH/dist/bootstrap-native/mympd-config.ts" src/index.ts
cp "$STARTPATH/dist/bootstrap-native/mympd-init.ts" src/util/
#minified build
npm run build-vite
grep -v "^//" dist/bootstrap-native.js > "$STARTPATH/dist/bootstrap-native/bootstrap-native.min.js"
#normal build
sed -i 's/sourcemap: true,/sourcemap: true,\nminify: false/' vite.config.mts
npm run build-vite
grep -v "^//" dist/bootstrap-native.js > "$STARTPATH/dist/bootstrap-native/bootstrap-native.js"
#cleanup
cd "$STARTPATH" || exit 1
rm -rf "$TMPDIR"
#update debug build
if [ -d debug ]
then
cp dist/bootstrap-native/bootstrap-native.js htdocs/js/
fi
}
updatebootstrap() {
check_cmd npm
cd "$STARTPATH/dist/bootstrap" || exit 1
[ -z "${BOOTSTRAP_VERSION+x}" ] && BOOTSTRAP_VERSION=""
npm install "$BOOTSTRAP_VERSION"
npm run build
sed -i '$ d' compiled/custom.css
rm compiled/custom.css.map
if [ -d "$STARTPATH/debug" ]
then
cp -v compiled/custom.css "$STARTPATH/htdocs/css/bootstrap.css"
fi
rm -fr dist/bootstrap/node_modules
}
#Also deletes stale installations in other locations.
#
uninstall() {
#cmake does not provide an uninstall target, instead its manifest is of use at least for
#the binaries
if [ -f "$STARTPATH/release/install_manifest.txt" ]
then
xargs rm -f < "$STARTPATH/release/install_manifest.txt"
fi
[ -z "${DESTDIR+x}" ] && DESTDIR=""
#CMAKE_INSTALL_PREFIX="/usr"
rm -f "$DESTDIR/usr/bin/mympd"
rm -f "$DESTDIR/usr/bin/mympd-script"
rm -rf "$DESTDIR/usr/share/doc/mympd"
rm -f "$DESTDIR/usr/share/man/man1/mympd.1.gz"
rm -f "$DESTDIR/usr/share/man/man1/mympd-script.1.gz"
#CMAKE_INSTALL_PREFIX="/usr/local"
rm -f "$DESTDIR/usr/local/bin/mympd"
rm -f "$DESTDIR/usr/local/bin/mympd-script"
rm -rf "$DESTDIR/usr/local/share/doc/mympd"
rm -f "$DESTDIR/usr/local/share/man/man1/mympd.1.gz"
rm -f "$DESTDIR/usr/local/share/man/man1/mympd-script.1.gz"
#CMAKE_INSTALL_PREFIX="/opt/mympd/"
rm -rf "$DESTDIR/opt/mympd"
#systemd
rm -f "$DESTDIR/usr/lib/systemd/system/mympd.service"
rm -f "$DESTDIR/lib/systemd/system/mympd.service"
#sysVinit, open-rc
if [ -z "$DESTDIR" ] && [ -f "/etc/init.d/mympd" ]
then
echo "SysVinit / OpenRC-script /etc/init.d/mympd found."
echo "Make sure it isn't part of any runlevel and delete by yourself"
echo "or invoke with purge instead of uninstall."
fi
}
purge() {
[ -z "${DESTDIR+x}" ] && DESTDIR=""
#CMAKE_INSTALL_PREFIX="/usr"
rm -rf "$DESTDIR/var/lib/mympd"
rm -rf "$DESTDIR/var/lib/private/mympd"
rm -rf "$DESTDIR/var/cache/mympd"
rm -rf "$DESTDIR/var/cache/private/mympd"
rm -f "$DESTDIR/etc/init.d/mympd"
rm -rf "$DESTDIR/usr/share/doc/mympd"
rm -f "$DESTDIR/usr/share/man/man1/mympd.1.gz"
rm -f "$DESTDIR/usr/share/man/man1/mympd-script.1.gz"
#CMAKE_INSTALL_PREFIX="/opt/mympd/"
rm -rf "$DESTDIR/var/opt/mympd"
#remove user
if getent passwd mympd > /dev/null
then
if check_cmd_silent userdel
then
userdel mympd
elif check_cmd_silent deluser
then
#alpine