-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·2027 lines (1575 loc) · 43.2 KB
/
run.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
#!/usr/bin/env bash
#
# Copyright (c) 2016 - 2021 Roland Kujundzic <[email protected]>
#
# shellcheck disable=SC1091,SC1001,SC2009,SC2012,SC2016,SC2024,SC2028,SC2033,SC2034,SC2044,SC2046,SC2048,SC2068,SC2086,SC2119,SC2120,SC2153,SC2183,SC2206
#
test -z "$RKBASH_DIR" && RKBASH_DIR="$HOME/.rkbash/$$"
if declare -A __hash=([key]=value) 2>/dev/null; then
test "${__hash[key]}" = 'value' || { echo -e "\nERROR: declare -A\n"; exit 1; }
unset __hash
else
echo -e "\nERROR: declare -A\n"
exit 1
fi
if test "${@: -1}" = 'help' 2>/dev/null; then
for a in ps tr xargs head grep awk find sed sudo cd chown chmod mkdir rm ls; do
command -v $a >/dev/null || { echo -e "\nERROR: missing $a\n"; exit 1; }
done
fi
function _abort {
local msg line rf brf nf
rf="\033[0;31m"
brf="\033[1;31m"
nf="\033[0m"
msg="$1"
if test -n "$2"; then
msg="$2"
line="[$1]"
fi
if test "$NO_ABORT" = 1; then
ABORT=1
echo -e "${rf}WARNING${line}: ${msg}${nf}"
return 1
fi
msg="${rf}${msg}${nf}"
local frame trace
if type -t caller >/dev/null 2>/dev/null; then
frame=0
trace=$(while caller $frame; do ((frame++)); done)
msg="$msg\n\n$trace"
fi
if [[ -n "$LOG_LAST" && -s "$LOG_LAST" ]]; then
msg="$msg\n\n$(tail -n+5 "$LOG_LAST")"
fi
test -n "$ABORT_MSG" && msg="$msg\n\n$ABORT_MSG"
echo -e "\n${brf}ABORT${line}:${nf} $msg\n" 1>&2
local other_pid=
if test -n "$APP_PID"; then
for a in $APP_PID; do
other_pid=$(ps aux | grep -E "^.+\\s+$a\\s+" | awk '{print $2}')
test -z "$other_pid" || kill "$other_pid" 2>/dev/null 1>&2
done
fi
if test -n "$APP"; then
other_pid=$(ps aux | grep "$APP" | awk '{print $2}')
test -z "$other_pid" || kill "$other_pid" 2>/dev/null 1>&2
fi
exit 1
}
function _add_abort_linenum {
local lines changes tmp_file fix_line
type -t caller >/dev/null 2>/dev/null && return
_mkdir "$RKBASH_DIR/add_abort_linenum"
tmp_file="$RKBASH_DIR/add_abort_linenum/"$(basename "$1")
test -f "$tmp_file" && _abort "$tmp_file already exists"
echo -n "add line number to _abort in $1"
changes=0
readarray -t lines < "$1"
for ((i = 0; i < ${#lines[@]}; i++)); do
fix_line=$(echo "${lines[$i]}" | grep -E -e '(;| \|\|| &&) _abort ["'"']" -e '^\s*_abort ["'"']" | grep -vE -e '^\s*#' -e '^\s*function ')
if test -z "$fix_line"; then
echo "${lines[$i]}" >> "$tmp_file"
else
changes=$((changes+1))
echo "${lines[$i]}" | sed -E 's/^(.*)_abort (.+)$/\1_abort '$((i+1))' \2/g' >> "$tmp_file"
fi
done
echo " ($changes)"
_cp "$tmp_file" "$1" >/dev/null
}
function _apigen_doc {
local doc_dir prj bin src_dir
doc_dir=./docs/apigen
prj="docs/.apigen"
bin="$prj/vendor/apigen/apigen/bin/apigen"
src_dir=./src
_mkdir "$doc_dir"
_mkdir "$prj"
_require_program composer
if ! test -f "$prj/composer.json"; then
_cd "$prj"
_composer_json "rklib/rkphplib_doc_apigen"
composer require "apigen/apigen:dev-master"
composer require "roave/better-reflection:dev-master#c87d856"
_cd "$CURR"
fi
if ! test -s "$bin"; then
_cd "$prj"
composer update
_cd "$CURR"
fi
test -n "$1" && src_dir="$1"
test -n "$2" && doc_dir="$2"
_require_dir "$src_dir"
if test -d "$doc_dir"; then
_confirm "Remove existing documentation directory [$doc_dir] ?" 1
if test "$CONFIRM" = "y"; then
_rm "$doc_dir"
fi
fi
echo "Create apigen documentation"
echo "$bin generate '$src_dir' --destination '$doc_dir'"
$bin generate "$src_dir" --destination "$doc_dir"
}
function _apt_install {
local curr_lne
curr_lne=$LOG_NO_ECHO
LOG_NO_ECHO=1
_require_program apt
_run_as_root 1
_rkbash_dir
for a in $*; do
if test -d "$RKBASH_DIR/apt/$a"; then
_msg "already installed, skip: apt -y install $a"
else
sudo apt -y install "$a" || _abort "apt -y install $a"
_log "apt -y install $a" "apt/$a"
fi
done
_rkbash_dir reset
LOG_NO_ECHO=$curr_lne
}
function _apt_update {
_require_program apt
local lu now
_rkbash_dir apt
lu="$RKBASH_DIR/last_update"
now=$(date +%s)
if [[ -f "$lu" && $(cat "$lu") -gt $((now - 3600 * 24 * 7)) ]]; then
:
else
echo "$now" > "$lu"
_run_as_root 1
_msg "apt -y update &>$RKBASH_DIR/update.log ... " -n
sudo apt -y update &>"$RKBASH_DIR/update.log" || _abort 'sudo apt -y update'
_msg "done"
if test "$1" = 1; then
_msg "apt -y upgrade &>$RKBASH_DIR/upgrade.log ... " -n
sudo apt -y upgrade &>"$RKBASH_DIR/upgrade.log" || _abort 'sudo apt -y upgrade'
_msg "done"
fi
fi
_rkbash_dir reset
}
function _ask {
local allow default msg label recursion
if test -z "$2"; then
:
elif [[ "${2:0:1}" == "<" && "${2: -1}" == ">" ]]; then
label="$2 "
allow="|${2:1: -1}|"
if test -n "$ASK_DEFAULT"; then
default="$ASK_DEFAULT"
label="$label [$default] "
ASK_DEFAULT=
fi
else
label="[$2] "
default="$2"
fi
if [[ "$AUTOCONFIRM" = "default" && -n "$default" ]]; then
ANSWER="$default"
AUTOCONFIRM=
return
fi
msg="\033[0;35m$1\033[0m"
if test -z "$ASK_DESC"; then
echo -en "$msg $label"
else
echo -en "$msg\n\n$ASK_DESC\n\n$label"
fi
ASK_DESC=
read -r
if test "$REPLY" = " "; then
ANSWER=
elif [[ -z "$REPLY" && -n "$default" ]]; then
ANSWER="$default"
elif test -n "$allow"; then
[[ "$allow" == *"|$REPLY|"* ]] && ANSWER="$REPLY" || ANSWER=
else
ANSWER="$REPLY"
fi
recursion="${4:-0}"
if test -z "$ANSWER" && test "$recursion" -lt 3; then
test "$recursion" -ge 2 && _abort "you failed to answer the question 3 times"
recursion=$((recursion + 1))
_ask "$1" "$2" "$3" "$recursion"
fi
[[ -z "$ANSWER" && "$1" = '1' ]] && _abort "you failed to answer the question"
}
function _cd {
local has_realpath curr_dir goto_dir
has_realpath=$(command -v realpath)
if [[ -n "$has_realpath" && -n "$1" ]]; then
curr_dir=$(realpath "$PWD")
goto_dir=$(realpath "$1")
if test "$curr_dir" = "$goto_dir"; then
return
fi
fi
test -z "$2" && _msg "cd '$1'"
if test -z "$1"; then
if test -n "$LAST_DIR"; then
_cd "$LAST_DIR"
return
else
_abort "empty directory path"
fi
fi
if ! test -d "$1"; then
_abort "no such directory [$1]"
fi
LAST_DIR="$PWD"
cd "$1" || _abort "cd '$1' failed"
}
function _chmod {
local tmp cmd i priv
test -z "$1" && _abort "empty privileges parameter"
test -z "$2" && _abort "empty path"
tmp=$(echo "$1" | sed -E 's/[012345678]*//')
test -z "$tmp" || _abort "invalid octal privileges '$1'"
cmd="chmod -R"
if test -n "$CHMOD"; then
cmd="$CHMOD"
CHMOD=
fi
if test -z "$2"; then
for ((i = 0; i < ${#FOUND[@]}; i++)); do
priv=
if test -f "${FOUND[$i]}" || test -d "${FOUND[$i]}"; then
priv=$(stat -c "%a" "${FOUND[$i]}")
fi
if test "$1" != "$priv" && test "$1" != "0$priv"; then
_sudo "$cmd $1 '${FOUND[$i]}'" 1
fi
done
elif test -f "$2"; then
priv=$(stat -c "%a" "$2")
if [[ "$1" != "$priv" && "$1" != "0$priv" ]]; then
_sudo "$cmd $1 '$2'" 1
fi
elif test -d "$2"; then
_sudo "$cmd $1 '$2'" 1
fi
}
function _composer {
local action cmd
action="$1"
if [[ ! -f 'composer.phar' && ! -f '/usr/local/bin/composer' ]]; then
_composer_install
test "$action" = 'q' && return
fi
cmd=$(command -v composer)
test -z "$cmd" && cmd='php composer.phar'
if test -z "$action"; then
_composer_ask
test "$action" = 'q' && return
fi
$cmd validate --no-check-publish 2>/dev/null || \
_abort "$cmd validate --no-check-publish"
if test -f composer.json; then
if test "$action" = 'i'; then
$cmd install
elif test "$action" = 'u'; then
$cmd update
elif test "$action" = 'a'; then
$cmd dump-autoload -o
fi
fi
}
function _composer_install {
ASK_DESC="[g] = Global installation as /usr/local/bin/composer\n[l] = Local installation as ./composer.phar"
_ask 'Install composer' '<g|l>'
if test "$ANSWER" = 'g'; then
echo 'install composer as /usr/local/bin/composer - Enter root password if asked'
_composer_phar /usr/local/bin/composer
elif test "$ANSWER" = 'l'; then
echo 'install composer as ./composer.phar'
_composer_phar
else
action='q'
fi
}
function _composer_ask {
if ! test -f 'composer.json'; then
action='q'
return
fi
ask='<i'
ASK_DESC="[i] = install packages from composer.json"
ASK_DEFAULT='i'
if test -d 'vendor'; then
ASK_DESC="$ASK_DESC\n[u] = update packages from composer.json\n[a] = update vendor/composer/autoload*"
ASK_DEFAULT='u'
ask="$ask|u|a"
fi
if test -f 'composer.phar'; then
ask="$ask|r"
ASK_DESC="$ASK_DESC\n[r] = remove local composer.phar"
fi
ASK_DESC="$ASK_DESC\n[q] = quit"
_ask 'Composer action?' "$ask|q>" 1
action=$ANSWER
if test "$action" = "r"; then
echo "remove composer"
_rm "composer.phar vendor composer.lock ~/.composer"
action='q'
fi
}
function _composer_json {
if test -z "$1"; then
_abort "empty project name use e.g. rklib/NAME"
fi
if test -f "composer.json"; then
_confirm "Overwrite existing composer.json"
if test "$CONFIRM" = "y"; then
_rm "composer.json"
else
return
fi
fi
_license "gpl-3.0"
local CLASSMAP=
if test -d "src"; then
CLASSMAP='"src/"'
fi
echo "create composer.json ($1, $LICENSE)"
1>"composer.json" cat <<EOL
{
"name": "$1",
"type": "",
"description": "",
"authors": [
{ "name": "Roland Kujundzic", "email": "[email protected]" }
],
"minimum-stability" : "dev",
"prefer-stable" : true,
"require": {
"php": ">=7.2.0",
"ext-mbstring": "*"
},
"autoload": {
"classmap": [$CLASSMAP],
"files": []
},
"license": "GPL-3.0-or-later"
}
EOL
}
function _composer_phar {
local expected_sig actual_sig install_as sudo result
expected_sig="$(_wget "https://composer.github.io/installer.sig" -)"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
actual_sig="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
if test "$expected_sig" != "$actual_sig"; then
_rm composer-setup.php
_abort 'Invalid installer signature'
fi
install_as="$1"
sudo='sudo'
if test -z "$install_as"; then
install_as="./composer.phar"
sudo=
fi
$sudo php composer-setup.php --quiet --install-dir=$(dirname "$install_as") --filename=$(basename "$install_as")
result=$?
if ! test "$result" = "0" || ! test -s "$install_as"; then
_abort "composer installation failed"
fi
_rm composer-setup.php
}
function _confirm {
local msg
msg="\033[0;35m$1\033[0m"
CONFIRM=
if test -n "$AUTOCONFIRM"; then
CONFIRM="${AUTOCONFIRM:0:1}"
echo -e "$msg <$CONFIRM>"
AUTOCONFIRM="${AUTOCONFIRM:1}"
return
fi
if test -z "$CONFIRM_COUNT"; then
CONFIRM_COUNT=1
else
CONFIRM_COUNT=$((CONFIRM_COUNT + 1))
fi
local flag cckey default
flag=$(($2 + 0))
if test $((flag & 2)) = 2; then
if test $((flag & 1)) = 1; then
CONFIRM=n
else
CONFIRM=y
fi
return
fi
while read -r -d $'\0'
do
cckey="--q$CONFIRM_COUNT"
if test "$REPLY" = "$cckey=y"; then
echo "found $cckey=y, accept: $1"
CONFIRM=y
elif test "$REPLY" = "$cckey=n"; then
echo "found $cckey=n, reject: $1"
CONFIRM=n
fi
done < /proc/$$/cmdline
if test -n "$CONFIRM"; then
CONFIRM_TEXT="$CONFIRM"
return
fi
if test $((flag & 1)) -ne 1; then
default=n
echo -n -e "$msg y [n] "
read -r -n1 -t 10 CONFIRM
echo
else
default=y
echo -n -e "$msg \033[0;35m[y]\033[0m n "
read -r -n1 -t 3 CONFIRM
echo
fi
if test -z "$CONFIRM"; then
CONFIRM="$default"
fi
CONFIRM_TEXT="$CONFIRM"
if test "$CONFIRM" != "y"; then
CONFIRM=n
fi
}
function _cp {
local curr_lno target_dir md1 md2 pdir
curr_lno="$LOG_NO_ECHO"
LOG_NO_ECHO=1
CP_FIRST=
CP_KEEP=
test -z "$2" && _abort "empty target"
target_dir=$(dirname "$2")
test -d "$target_dir" || _abort "no such directory [$target_dir]"
if test "$3" != 'md5'; then
:
elif ! test -f "$2"; then
CP_FIRST=1
elif test -f "$1"; then
md1=$(_md5 "$1")
md2=$(_md5 "$2")
if test "$md1" = "$md2"; then
_msg "_cp: keep $2 (same as $1)"
CP_KEEP=1
else
_msg "Copy file $1 to $2 (update)"
_sudo "cp '$1' '$2'" 1
fi
return
fi
if test -f "$1"; then
_msg "Copy file $1 to $2"
_sudo "cp '$1' '$2'" 1
elif test -d "$1"; then
if test -d "$2"; then
pdir="$2"
_confirm "Remove existing target directory '$2'?"
if test "$CONFIRM" = "y"; then
_rm "$pdir"
_msg "Copy directory $1 to $2"
_sudo "cp -r '$1' '$2'" 1
else
_msg "Copy directory $1 to $2 (use rsync)"
_rsync "$1/" "$2"
fi
else
_msg "Copy directory $1 to $2"
_sudo "cp -r '$1' '$2'" 1
fi
else
_abort "No such file or directory [$1]"
fi
LOG_NO_ECHO="$curr_lno"
}
function _find {
FOUND=()
local a
_require_program find
_require_dir "$1"
while read -r a; do
FOUND+=("$a")
done < <(eval "find '$1' $2" || _abort "find '$1' $2")
}
function _git_status {
local a change files
_require_program 'rks-filter'
for a in $1; do
files="$files $(git status | grep "$a/" | sed -E 's#^.+src/#src/#')"
done
for a in $files; do
change=$(git diff --color=always "$a" | rks-filter diff | \
sed -E -e 's#diff .+##' -e 's#index .+##' -e 's#\-\-\- .+##' -e 's#\+\+\+ .+##' | xargs | \
sed -E -e 's/[^a-z0-9]//gi' -e 's/1m//g')
if test -z "$change"; then
_ok "$a"
else
git diff --color=always "$a" | rks-filter diff
fi
done
}
function _install_mariadb {
_apt_update
_apt_install 'mariadb-server mariadb-client php-mysql'
}
function _install_nginx {
_apt_update
_apt_install 'nginx php-fpm'
}
function _install_php {
_apt_update
_apt_install 'php-cli php-curl php-mbstring php-gd php-xml php-tcpdf php-json'
_apt_install 'php-dev php-imap php-intl php-xdebug php-pear php-zip php-pclzip'
}
function _install_sqlite3 {
_apt_update
_apt_install 'sqlite3 php-sqlite3'
}
function _is_running {
_os_type linux
local rx out res
res=0
if test "$1" = 'apache'; then
rx='[a]pache2.*k start'
elif test "$1" = 'nginx'; then
rx='[n]ginx.*master process'
elif test "${1:0:7}" = 'docker:'; then
rx="[d]ocker-proxy.* -host-port ${1:7}"
elif test "${1:0:5}" = 'port:'; then
out=$(netstat -tulpn 2>/dev/null | grep -E ":${1:5} .+:* .+LISTEN.*")
else
_abort "invalid [$1] use apache|nginx|docker:PORT|port:N|rx:[n]ame"
fi
test -z "$rx" || out=$(ps aux 2>/dev/null | grep -E "$rx")
test -z "$out" && res=1
return $res
}
function _license {
if [[ -n "$1" && "$1" != 'gpl-3.0' ]]; then
_abort "unknown license [$1] use [gpl-3.0]"
fi
LICENSE=$1
if test -z "$LICENSE"; then
LICENSE="gpl-3.0"
fi
local lfile is_gpl3
lfile="./LICENSE"
if test -s "$lfile"; then
is_gpl3=$(head -n 2 "$lfile" | tr '\n' ' ' | sed -E 's/\s+/ /g' | grep 'GNU GENERAL PUBLIC LICENSE Version 3')
if test -n "$is_gpl3"; then
echo "keep existing gpl-3.0 LICENSE ($lfile)"
return
fi
_confirm "overwrite existing $lfile file with $LICENSE"
if test "$CONFIRM" != "y"; then
echo "keep existing $lfile file"
return
fi
fi
_wget "http://www.gnu.org/licenses/gpl-3.0.txt" "$lfile"
}
declare -Ai LOG_COUNT # define hash (associative array) of integer
declare -A LOG_FILE # define hash
declare -A LOG_CMD # define hash
LOG_NO_ECHO=
function _log {
test -z "$LOG_NO_ECHO" && _msg "$1" -n
if test -z "$2"; then
test -z "$LOG_NO_ECHO" && echo
return
fi
LOG_COUNT[$2]=$((LOG_COUNT[$2] + 1))
LOG_FILE[$2]="$RKBASH_DIR/$2/${LOG_COUNT[$2]}.nfo"
LOG_CMD[$2]=">>'${LOG_FILE[$2]}' 2>&1"
LOG_LAST=
if ! test -d "$RKBASH_DIR/$2"; then
mkdir -p "$RKBASH_DIR/$2"
if test -n "$SUDO_USER"; then
chown -R $SUDO_USER.$SUDO_USER "$RKBASH_DIR" || _abort "chown -R $SUDO_USER.$SUDO_USER '$RKBASH_DIR'"
elif test "$UID" = "0"; then
chmod -R 777 "$RKBASH_DIR" || _abort "chmod -R 777 '$RKBASH_DIR'"
fi
fi
local now
now=$(date +'%d.%m.%Y %H:%M:%S')
echo -e "# _$2: $now\n# $PWD\n# $1 ${LOG_CMD[$2]}\n" > "${LOG_FILE[$2]}"
if test -n "$SUDO_USER"; then
chown $SUDO_USER.$SUDO_USER "${LOG_FILE[$2]}" || _abort "chown $SUDO_USER.$SUDO_USER '${LOG_FILE[$2]}'"
elif test "$UID" = "0"; then
chmod 666 "${LOG_FILE[$2]}" || _abort "chmod 666 '${LOG_FILE[$2]}'"
fi
test -z "$LOG_NO_ECHO" && _msg " ${LOG_CMD[$2]}"
test -s "${LOG_FILE[$2]}" && LOG_LAST="${LOG_FILE[$2]}"
}
function _mb_check {
_require_dir src
local a mb_func
echo -e "\nSearch all *.php files in src/ - output filename if string function\nmight need to be replaced with mb_* version.\n"
echo -e "Type any key to continue or wait 5 sec.\n"
read -r -n1 -t 5 ignore_keypress
mb_func="parse_str split stripos stristr strlen strpos strrchr strrichr
strripos strrpos strstr strtolower strtoupper strwidth substr_count substr"
for a in $mb_func; do
grep -d skip -r --include=*.php "$a(" src | grep -v "mb_$a("
done
}
function _md5 {
_require_program md5sum
if test -z "$1"; then
_abort "Empty parameter"
elif test -f "$1"; then
md5sum "$1" | awk '{print $1}'
elif test "$2" = "1"; then
echo -n "$1" | md5sum | awk '{print $1}'
else
_abort "No such file [$1]"
fi
}
function _merge_sh {
local a my_app mb_app sh_dir rkbash_inc tmp_app md5_new md5_old inc_sh scheck
my_app="${1:-$APP}"
sh_dir="${my_app}_"
if test -n "$2"; then
my_app="$2"
sh_dir="$1"
else
_require_file "$my_app"
mb_app=$(basename "$my_app")
test -d "$sh_dir" || { test -d "$mb_app" && sh_dir="$mb_app"; }
fi
test "${ARG[static]}" = "1" && rkbash_inc=$(_merge_static "$sh_dir")
_require_dir "$sh_dir"
tmp_app="$sh_dir"'_'
test -s "$my_app" && md5_old=$(_md5 "$my_app")
_msg "merge $sh_dir into $my_app ... " -n
inc_sh=$(find "$sh_dir" -name '*.inc.sh' 2>/dev/null | sort)
scheck=$(grep -E '^# shellcheck disable=' $inc_sh | sed -E 's/.+ disable=(.+)$/\1/g' | tr ',' ' ' | xargs -n1 | sort -u | xargs | tr ' ' ',')
test -z "$scheck" || RKS_HEADER_SCHECK="shellcheck disable=SC1091,$scheck"
if test -z "$rkbash_inc"; then
_rks_header "$tmp_app" 1
else
_rks_header "$tmp_app"
echo "$rkbash_inc" >> "$tmp_app"
fi
for a in $inc_sh; do
tail -n+2 "$a" | grep -E -v '^# shellcheck disable=' >> "$tmp_app"
done
_add_abort_linenum "$tmp_app"
md5_new=$(_md5 "$tmp_app")
if test "$md5_old" = "$md5_new"; then
_msg "no change"
_rm "$tmp_app" >/dev/null
else
_msg "update"
_mv "$tmp_app" "$my_app"
_chmod 755 "$my_app"
fi
test -z "$2" && exit 0
}
function _merge_static {
local a rks_inc inc_sh
inc_sh=$(find "$1" -name '*.inc.sh' 2>/dev/null | sort)
for a in $inc_sh; do
_rkbash_inc "$a"
rks_inc="$rks_inc $RKBASH_INC"
done
for a in $(_sort $rks_inc); do
tail -n +2 "$RKBASH_SRC/${a:1}.sh" | grep -E -v '^\s*#'
done
}
function _mkdir {
local flag
flag=$(($2 + 0))
test -z "$1" && _abort "Empty directory path"
if test -d "$1"; then
test $((flag & 1)) = 1 && _abort "directory $1 already exists"
test $((flag & 4)) = 4 && _msg "directory $1 already exists"
else
_msg "mkdir -p $1"
$SUDO mkdir -p "$1" || _abort "mkdir -p '$1'"
fi
test $((flag & 2)) = 2 && _chmod 777 "$1"
}
function _msg {
if test "$2" == '-n'; then
echo -n -e "\033[0;2m$1\033[0m"
else
echo -e "\033[0;2m$1\033[0m"
fi
}
function _mv {
if test -z "$1"; then
_abort "Empty source path"
fi
if test -z "$2"; then
_abort "Empty target path"
fi
local pdir
pdir=$(dirname "$2")
if ! test -d "$pdir"; then
_abort "No such directory [$pdir]"
fi
local AFTER_LAST_SLASH=${1##*/}
if test "$AFTER_LAST_SLASH" = "*"
then
_msg "mv $1 $2"
mv "$1" "$2" || _abort "mv $1 $2 failed"
else
_msg "mv '$1' '$2'"
mv "$1" "$2" || _abort "mv '$1' '$2' failed"
fi
}
function _nginx_php_fpm {
local site php_fpm
site=/etc/nginx/sites-available/default
_install_nginx
if [[ -f "$site" && ! -f "${site}.orig" ]]; then
_orig "$site"
echo "changing $site"
echo 'server {
listen 80 default_server; root /var/www/html; index index.html index.htm index.php; server_name localhost;
location / { try_files $uri $uri/ =404; }
location ~ \.php$ { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; }
}' > "$site"
fi
php_fpm=php$(ls /etc/php/*/fpm/php.ini | sed -E 's#/etc/php/(.+)/fpm/php.ini#\1#')-fpm
_service "$php_fpm" restart
_service nginx restart
}
function _ok {
echo -e "\033[0;32m$1\033[0m" 1>&2
}
function _orig {
if ! test -f "$1" && ! test -d "$1"; then
test -z "$2" && _abort "missing $1"
return 1
fi
local RET=0
if test -f "$1.orig"; then
_msg "$1.orig already exists"
RET=1
else
_msg "create backup $1.orig"
_cp "$1" "$1.orig"
fi
return $RET
}
function _os_type {
local os me
_require_program uname
me=$(uname -s)
if [ "$(uname)" = "Darwin" ]; then
os="macos"
elif [ "$OSTYPE" = "linux-gnu" ]; then
os="linux"
elif [ "${me:0:5}" = "Linux" ]; then
os="linux"
elif [ "${me:0:5}" = "MINGW" ]; then
os="cygwin"
fi
if test -z "$1"; then
echo $os
elif test "$1" != "$os"; then
_abort "$1 required (this is $os)"
fi
return 0