-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmakeBuild.sh
executable file
·1495 lines (1444 loc) · 45.6 KB
/
makeBuild.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/bash
# Script was made by Ido Ben-Hur (@idoybh) due to pure bordom and to save time building diff roms
#######################
## Functions ##
#######################
# resets adb server
adb_reset()
{
echo -e "${GREEN}Restarting ADB server${NC}"
adb kill-server
adb start-server
}
# waits for a recognizeable device in given state
# $1: delay between scans in seconds
# $2: device states array
adb_wait()
{
delay=$1
shift
states=("$@")
if [[ ${#states[@]} == 1 ]]; then
state=${states[$i]}
if [[ $state != 'device' ]]; then
echo -e "${GREEN}Waiting for device in ${BLUE}${state}${NC}"
else
echo -e "${GREEN}Waiting for device${NC}"
fi
else
outp="${GREEN}Waiting for device in"
for i in "${!states[@]}"; do
[[ $i -gt 0 ]] && outp="${outp} or"
if [[ ${states[$i]} == 'device' ]]; then
outp="${outp} ${BLUE}on${GREEN}"
continue
fi
outp="${outp} ${BLUE}${states[$i]}${GREEN}"
done
echo -e "${outp} state${NC}"
fi
waitCount=0
waitSent=0
isDet=1 # reversed logic
while [[ $isDet != 0 ]]; do # wait until detected
for i in "${!states[@]}"; do
if [[ $waitCount -gt 0 ]] && [[ $waitSent == 0 ]]; then
tg_send "Waiting for device"
waitSent=1
fi
waitCount=$(( waitCount + 1 ))
if [[ ${states[$i]} == 'bootloader' ]] || [[ ${states[$i]} == 'fastboot' ]]; then
if [[ $(fastboot devices) ]]; then
isDet=0
break
fi
continue
fi
adb kill-server &> /dev/null
adb start-server &> /dev/null
adb devices | grep -w "${states[$i]}" &> /dev/null
isDet=$?
[[ $isDet == 0 ]] && break
done
[[ $isDet != 0 ]] && sleep $delay
done
}
# waits until device is unlocked
# $1: (optional) delay between scans in seconds (defaults to 1)
# $2: (optional) path to check (defaults to /sdcard/)
adb_wait_unlocked()
{
delay=$1
[[ $delay == "" ]] && delay=1
cPath=$2
[[ $cPath == "" ]] && cPath="/sdcard/"
echo -e "${GREEN}Waiting for an unlocked device${NC}"
isUnlocked=1 # reversed logic
while [[ $isUnlocked != 0 ]]; do
adb shell "ls ${cPath}" &> /dev/null
isUnlocked=$?
sleep $delay
done
}
# sends a msg in telegram if not silent.
# $1: the msg / file to send
isEdit=0 # global var so we don't edit the 1st msg
tmpDir="$(mktemp -d)/" # global var for the tmp file dir
currMsg="" # a var that stores the current message for edits
tg_send()
{
local tgmsg=$1
if [[ $isSilent == 0 ]]; then
if [[ $TG_SEND_PRIOR_CMD != '' ]]; then
eval "$TG_SEND_PRIOR_CMD"
fi
tgcmd="--tmp ${tmpDir}"
if [[ $TG_SEND_CFG_FILE != '' ]]; then
tgcmd="${tgcmd} --config ${TG_SEND_CFG_FILE}"
fi
if [[ $isEdit == 1 ]] && [[ ! -f "${tgmsg}" ]]; then
tgcmd="${tgcmd} --edit"
elif [[ ! -f "${tgmsg}" ]]; then
trap tg_clean SIGINT
tgcmd="${tgcmd} --pin"
isEdit=1
fi
if [[ -f "${tgmsg}" ]]; then
# always unpin when we send a file (error)
currMsg=""
isEdit=0
./telegramSend.sh $tgcmd --unpin " "
./telegramSend.sh $tgcmd --file --cite "${tgmsg}"
trap - SIGINT
else
[[ $currMsg != "" ]] && currMsg="${currMsg}\n"
currMsg="${currMsg}${tgmsg}"
./telegramSend.sh $tgcmd --disable-preview "${currMsg}"
fi
fi
}
# shellcheck disable=SC2317
function tg_clean()
{
[[ $currMsg != "" ]] && currMsg="${currMsg}\n"
currMsg="${currMsg}Script was stopped / canceled externally"
local tgcmd="--tmp ${tmpDir} --edit --disable-preview"
local upintgcmd="--tmp ${tmpDir} --unpin"
if [[ $TG_SEND_CFG_FILE != '' ]]; then
tgcmd="${tgcmd} --config ${TG_SEND_CFG_FILE}"
upintgcmd="${upintgcmd} --config ${TG_SEND_CFG_FILE}"
fi
./telegramSend.sh $tgcmd "${currMsg}"
./telegramSend.sh $upintgcmd " "
exit 1
}
# returns a progress bar for a given % in $1
# $1 percentage in decimal (out of 100)
get_bar()
{
per=$1
len=25
cLen=$(bc -l <<< "${len} * (${per} / 100)" | cut -d "." -f 1)
bar="["
for ((i = 0; i < len; i++)); do
if [[ $i -lt $cLen ]]; then
bar="${bar}#"
continue
fi
bar="${bar}-"
done
bar="${bar}]"
echo "$bar"
}
# returns min max and avg cpu core speeds
get_cpu_speeds()
{
outp=$(cat /proc/cpuinfo | grep MHz | tr -dc '[. [:digit:]]' | xargs | tr ' ' '\n')
max=0
min=0
count=0
sum=0
while read -r num; do
num=$(echo $num | cut -d '.' -f 1)
if [[ $count == 0 ]]; then
max=$num
min=$num
sum=$(( $sum + $num ))
(( count++ ))
continue
fi
[[ $num -gt $max ]] && max=$num
[[ $num -lt $min ]] && min=$num
sum=$(( $sum + $num ))
(( count++ ))
done <<< "$outp"
avg=$(( $sum / $count ))
avg=$(echo "scale=2; ${avg} / 1000" | bc -l)
min=$(echo "scale=2; ${min} / 1000" | bc -l)
max=$(echo "scale=2; ${max} / 1000" | bc -l)
if [[ $min == "" ]] || [[ $max == "" ]] || [[ $avg == "" ]]; then
echo ""
else
echo "GHz: ↑${max} ↓${min} ⨏${avg}"
fi
}
# returns some cpu & ccache details
pccFiles=-1
pccFlag=0
get_stats()
{
bar=""
sOut=$(sensors)
cUsage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}')
cTemp=$(echo "$sOut" | grep "Tctl" | tr -d -c "0-9.")
ramInf=$(free -g -t | grep Total | cut -d ":" -f 2 | tr -s ' ' | sed "s/ //" | sed "s/ /:/g")
cCurr=$(echo "$sOut" | grep -i "CPU VRM Output Current" | cut -d ":" -f 2 | sed 's/ //g' | sed 's/A//')
cVolt=$(echo "$sOut" | grep -m 1 -i "CPU Core Voltage" | cut -d ":" -f 2 | sed 's/ //g' | sed 's/V//')
cWatt=$(echo "${cCurr} * ${cVolt}" | bc)
cSpeeds=$(get_cpu_speeds)
[[ $cUsage != "" ]] && bar="${bar}CPU: ${cUsage}"
if [[ $cTemp != "" ]]; then
if ! echo "$bar" | grep -q "CPU"; then
bar="${bar}CPU:"
fi
bar="${bar} ${cTemp}°C"
fi
if [[ $cWatt != "" ]]; then
if ! echo "$bar" | grep -q "CPU"; then
bar="${bar}CPU:"
fi
bar="${bar} ${cWatt}W"
fi
if echo "$bar" | grep -q "CPU"; then
cPer="${cUsage//%/}"
pBar="$(get_bar ${cPer})"
bar="${bar}\n${pBar}"
fi
if [[ $cSpeeds != "" ]]; then
if ! echo "$bar" | grep -q "CPU"; then
bar="${bar}CPU:"
fi
bar="${bar}\n${cSpeeds}"
fi
if echo "$bar" | grep -q "CPU"; then
bar="${bar}\n"
fi
if [[ $ramInf != "" ]]; then
ramT=$(cut -d ":" -f 1 <<< "$ramInf")
ramU=$(cut -d ":" -f 2 <<< "$ramInf")
ramP=$(bc -l <<< "${ramU} * 100 / ${ramT}" | cut -d "." -f 1)
pBar="$(get_bar ${ramP})"
bar="${bar}\nRAM: ${ramU}/${ramT} GiB (${ramP}%)\n${pBar}\n"
fi
if [[ $USE_CCACHE == 1 ]]; then
sOut=$(ccache -s | grep size | cut -d ":" -f 2)
ccSize=$(echo "$sOut" | cut -d "/" -f 1 | tr -d -c "0-9.")
ccMax=$(echo "$sOut" | cut -d "/" -f 2 | cut -d "(" -f 1 | tr -d -c "0-9.")
ccFiles=$(ccache -s -v | grep Files | cut -d ":" -f 2 | tr -d -c "0-9")
if [[ $ccSize != "" ]]; then
bar="${bar}\nccache: ${ccSize}"
[[ $ccMax != "" ]] && bar="${bar}/${ccMax}"
bar="${bar} GB"
if [[ $ccFiles != "" ]]; then
bar="${bar}\n${ccFiles}"
if [[ $pccFiles != -1 ]] && [[ $ccFiles -gt $pccFiles ]]; then
bar="${bar}↑"
pccFlag=1
elif [[ $pccFlag == 1 ]]; then
bar="${bar}⤒"
fi
bar="${bar} files"
pccFiles=$ccFiles
fi
fi
fi
echo "$bar"
}
# tracks the build progress (requires a soong change) and sends it
# meant to be started on a "thread"
prog_send()
{
initMsg="${currMsg}\n"
alt=":"
while true; do
sleep 5
trap "" SIGTERM
[ ! -f "build_status.txt" ] && continue
statusTxt=$(cat "build_status.txt")
[[ $statusTxt == "" ]] && continue
! grep -q "/" <<< "$statusTxt" && continue
! grep -q "," <<< "$statusTxt" && continue
if [[ $alt == ":" ]]; then
alt=";"
else
alt=":"
fi
get_time
targets=$(cut -d "," -f 1 <<< "$statusTxt") || continue
percent=$(cut -d "," -f 2 <<< "$statusTxt") || continue
progMsg="${initMsg}\n<code>[${targets}] targets ${alt} ${percent}%</code>" || continue
progMsg="${progMsg}\n<code>$(get_bar "$percent")</code>" || continue
progMsg="${progMsg}\nTime running: <code>${buildTime}</code>" || continue
progMsg="${progMsg}\n\n<code>$(get_stats)</code>" || continue
./telegramSend.sh --tmp "${tmpDir}" --config "${TG_SEND_CFG_FILE}" --edit --disable-preview "${progMsg}" || continue
trap - SIGTERM
done
}
# tracks the upload progress and sends it
# meant to be started on a "thread"
# modify to match your utility if changed
upload_prog_send()
{
initMsg="${currMsg}\n"
alt=":"
while true; do
sleep 5
trap "" SIGTERM
statusTxt=$(rclone rc core/stats | jq -r ".transferring?.[0]")
[[ $? != 0 ]] && continue
[[ $statusTxt == "" ]] && continue
[[ $statusTxt == "null" ]] && continue
! grep -q "percentage" <<< "$statusTxt" && continue
! grep -q "speed" <<< "$statusTxt" && continue
! grep -q "eta" <<< "$statusTxt" && continue
percent=$(jq -r ".percentage" <<< "$statusTxt") || continue
speed=$(jq -r ".speed" <<< "$statusTxt" | cut -d "." -f 1) || continue
etaTime=$(jq -r ".eta" <<< "$statusTxt") || continue
[[ $percent == "" ]] && continue
[[ $speed == "" ]] && continue
[[ $etaTime == "" ]] && continue
if [[ $alt == ":" ]]; then
alt=";"
else
alt=":"
fi
speed=$(bc -l <<< "${speed} / 1000000")
speed=$(printf "%.2f" "${speed}")
etaTime="$(( etaTime / 60 ))m$(( etaTime % 60 ))s"
progMsg="${initMsg}\n<code>${speed} MiB/s ${alt} ETA ${etaTime} ${alt} ${percent}%</code>" || continue
progMsg="${progMsg}\n<code>$(get_bar "$percent")</code>" || continue
./telegramSend.sh --tmp "${tmpDir}" --config "${TG_SEND_CFG_FILE}" --edit --disable-preview "${progMsg}" || continue
trap - SIGTERM
done
}
# prints the help msg
print_help()
{
echo -e "${GREEN}Flags available:${NC}"
echo -e "${BLUE}-h${NC} to show this dialog and exit"
echo -e "${BLUE}-i${NC} for setup"
echo -e "${BLUE}-u${NC} for upload"
echo -e "${BLUE}-p${NC} for ADB push (and recovery flash)"
echo -e "${BLUE}-f${NC} for fastboot flash"
echo -e "${BLUE}-c${NC} for a clean build"
echo -e "${BLUE}-s${NC} to disable sending telegram messages"
echo -e "${BLUE}-d${NC} to perform a dry run (no build)"
echo -e "${BLUE}--keep-file | -k${NC} to skip removal of original build file"
echo -e "${BLUE}--power [ARG]${NC} to power off / reboot when done"
echo -e " ${BLUE}Supported ARG(s): ${NC} off, reboot"
echo -e "${BLUE}--choose [CMD]${NC} to change target choose command"
echo -e "${BLUE}--type [CMD]${NC} to change build type command"
echo -e "${BLUE}--product [ARG]${NC} to change target product name"
echo -e "${BLUE}--config [FILE]${NC} to select a different config file"
echo -e "${BLUE}--tg-config [FILE]${NC} to select a different telegram config file"
echo -e "${BLUE}--installclean | --i-c${NC} to run ${BLUE}make installclean${NC} before the build"
echo -e "${GREEN}Default configuration file: ${BLUE}build.conf${NC}"
echo -en "${GREEN}For more help visit: "
echo -e "${BLUE}https://github.com/idoybh/makeBuild/blob/master/README.md${NC}"
}
# returns a property value from the config file
# $1: property name to read
# $2: config file name
config_read()
{
rProp=$1
cFile=$2
lineNO=$(awk "/${rProp}/{ print NR; exit }" $cFile)
res="$(sed "${lineNO}q;d" $cFile | cut -d '=' -f 2-)"
res="$(echo $res | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
eval "echo \"${res}\""
}
# sets a property vlaue to the config file
# $1: property name
# $2: value
# $3: config file name
config_write()
{
wProp=$1
wVal=$2
cFile=$3
lineNO=$(awk "/${wProp}/{ print NR; exit }" $cFile)
sed -i "${lineNO}s,=.*,=${wVal}," $cFile
}
# rewrites given config file
# $1: config file path
rewrite_config()
{
confPath=$1
config_write "WAS_INIT" 1 $confPath
config_write "CLEAN_CMD" "${CLEAN_CMD}" $confPath
config_write "TARGET_CHOOSE_CMD" "${TARGET_CHOOSE_CMD}" $confPath
config_write "PRE_BUILD_SCRIPT" "${PRE_BUILD_SCRIPT}" $confPath
config_write "BUILD_TYPE_CMD" "${BUILD_TYPE_CMD}" $confPath
config_write "BUILD_CMD" "${BUILD_CMD}" $confPath
config_write "FILE_MANAGER_CMD" "${FILE_MANAGER_CMD}" $confPath
config_write "UPLOAD_CMD" "${UPLOAD_CMD}" $confPath
config_write "UPLOAD_LINK_CMD" "${UPLOAD_LINK_CMD}" $confPath
config_write "UPLOAD_LINK_ALT_CMD" "${UPLOAD_LINK_ALT_CMD}" $confPath
config_write "TG_SEND_PRIOR_CMD" "${TG_SEND_PRIOR_CMD}" $confPath
config_write "TG_SEND_CFG_FILE" "${TG_SEND_CFG_FILE}" $confPath
config_write "UPLOAD_DEST" "${UPLOAD_DEST}" $confPath
config_write "UPLOAD_PATH" "${UPLOAD_PATH}" $confPath
config_write "SOURCE_PATH" "${SOURCE_PATH}" $confPath
config_write "BUILD_PRODUCT_NAME" "${BUILD_PRODUCT_NAME}" $confPath
config_write "BUILD_FILE_NAME" "${BUILD_FILE_NAME}" $confPath
config_write "ADB_DEST_FOLDER" "${ADB_DEST_FOLDER}" $confPath
config_write "UNHANDLED_PATH" "${UNHANDLED_PATH}" $confPath
config_write "AUTO_RM_BUILD" "${AUTO_RM_BUILD}" $confPath
config_write "AUTO_REBOOT" "${AUTO_REBOOT}" $confPath
config_write "AUTO_SLOT" "${AUTO_REBOOT}" $confPath
config_write "UPLOAD_DONE_MSG" "${UPLOAD_DONE_MSG}" $confPath
config_write "HANDLE_DONE_MSG" "${HANDLE_DONE_MSG}" $confPath
config_write "FAILURE_MSG" "${FAILURE_MSG}" $confPath
config_write "TWRP_PIN" "${TWRP_PIN}" $confPath
config_write "TWRP_SIDELOAD" "${TWRP_SIDELOAD}" $confPath
config_write "FASTBOOT_PKG" "${FASTBOOT_PKG}" $confPath
}
# loads given config file
# exists script if not found
# $1: config file path
load_config()
{
cFile=$1
if [[ -f $cFile ]]; then
linesNO=$(wc -l $cFile)
linesNO="${linesNO:0:2}"
for (( ii = 1; ii <= linesNO; ii++ )); do
curLine=$(sed "${ii}q;d" $cFile)
firstChar="${curLine:0:1}"
if [[ $firstChar != "#" ]] && [[ $firstChar != '' ]]; then
cVar="$(echo $curLine | awk '{print $1}')"
cVal="$(config_read $cVar $cFile)"
export $cVar="${cVal}"
fi
done
else
echo -e "${RED}ERROR! ${BLUE}${cFile}${RED} not found${NC}"
exit 2
fi
}
# writes a new build.conf file
init_conf()
{
echo -e "${GREEN}Initializing settings${NC}"
echo -e "${GREEN}Default values are inside [] just press enter to apply them${NC}"
echo -en "${YELLOW}Enter clean command [${BLUE}make clobber${YELLOW}]: ${NC}"
read CLEAN_CMD
if [[ $CLEAN_CMD == '' ]]; then
CLEAN_CMD='make clobber'
fi
echo -en "${YELLOW}Enter target choose command "
echo -en "[${BLUE}lunch yaap_guacamole-user${YELLOW}]: ${NC}"
read TARGET_CHOOSE_CMD
if [[ $TARGET_CHOOSE_CMD == '' ]]; then
TARGET_CHOOSE_CMD='lunch yaap_guacamole-user'
fi
echo -en "${YELLOW}Enter pre build script path "
echo -en "[${BLUE}blank${YELLOW}]: ${NC}"
read PRE_BUILD_SCRIPT
echo -en "${YELLOW}Enter build type command "
echo -en "[${BLUE}blank${YELLOW}]: ${NC}"
read BUILD_TYPE_CMD
echo -en "${YELLOW}Enter build command [${BLUE}mka kronic${YELLOW}]: ${NC}"
read BUILD_CMD
if [[ $BUILD_CMD == '' ]]; then
BUILD_CMD='mka kronic'
fi
echo -en "${YELLOW}Enter file manager command "
echo -en "('c' for none) [${BLUE}dolphin${YELLOW}]: ${NC}"
read FILE_MANAGER_CMD
if [[ $FILE_MANAGER_CMD == '' ]]; then
FILE_MANAGER_CMD='dolphin'
fi
if [[ $FILE_MANAGER_CMD == 'c' ]]; then
FILE_MANAGER_CMD=''
fi
echo -en "${YELLOW}Enter upload command [${BLUE}rclone copy -v${YELLOW}]: ${NC}"
read UPLOAD_CMD
if [[ $UPLOAD_CMD == '' ]]; then
UPLOAD_CMD='rclone copy -P'
fi
echo -en "${YELLOW}Enter upload link command [${BLUE}rclone link${YELLOW}]: ${NC}"
read UPLOAD_LINK_CMD
if [[ $UPLOAD_LINK_CMD == '' ]]; then
UPLOAD_LINK_CMD='rclone link'
fi
echo -en "${YELLOW}Enter upload link alternative command [${BLUE}none${YELLOW}]: ${NC}"
read UPLOAD_LINK_ALT_CMD
echo -en "${YELLOW}Enter telegram send prior command [${BLUE}none${YELLOW}]: ${NC}"
read TG_SEND_PRIOR_CMD
echo -en "${YELLOW}Enter telegram send config file path [${BLUE}none${YELLOW}]: ${NC}"
read TG_SEND_CFG_FILE
echo -en "${YELLOW}Enter upload destination (remote) "
echo -en "[${BLUE}GDrive:/builds${YELLOW}]: ${NC}"
read UPLOAD_DEST
if [[ $UPLOAD_DEST == '' ]]; then
UPLOAD_DEST='GDrive:/builds'
fi
echo -en "${YELLOW}Enter upload folder path (local) ('c' for none) "
echo -en "[${BLUE}gdrive:/idoybh2/builds/${YELLOW}]: ${NC}"
read UPLOAD_PATH
if [[ $UPLOAD_PATH == '' ]]; then
UPLOAD_PATH='gdrive:/idoybh2/builds/'
fi
if [[ $UPLOAD_PATH == 'c' ]]; then
UPLOAD_PATH=''
fi
echo -en "${YELLOW}Enter source path [${BLUE}.${YELLOW}]: ${NC}"
read SOURCE_PATH
if [[ $SOURCE_PATH == '' ]]; then
SOURCE_PATH='.'
fi
echo -en "${YELLOW}Enter build product name [${BLUE}dumpling${YELLOW}]: ${NC}"
read BUILD_PRODUCT_NAME
if [[ $BUILD_PRODUCT_NAME == '' ]]; then
BUILD_PRODUCT_NAME='dumpling'
fi
echo -en "${YELLOW}Enter built zip file name [${BLUE}YAAP*.zip${YELLOW}]: ${NC}"
read BUILD_FILE_NAME
if [[ $BUILD_FILE_NAME == '' ]]; then
BUILD_FILE_NAME='YAAP*.zip'
fi
echo -en "${YELLOW}Enter ADB push destination folder "
echo -en "[${BLUE}Flash/YAAP${YELLOW}]: ${NC}"
read ADB_DEST_FOLDER
if [[ $ADB_DEST_FOLDER == '' ]]; then
ADB_DEST_FOLDER='Flash/YAAP'
fi
echo -en "${YELLOW}Enter default move path ('c' for none) "
echo -en "[${BLUE}\$HOME/Desktop${YELLOW}]: ${NC}"
read UNHANDLED_PATH
if [[ $UNHANDLED_PATH == '' ]]; then
UNHANDLED_PATH="${HOME}/Desktop"
fi
if [[ $UNHANDLED_PATH == 'c' ]]; then
UNHANDLED_PATH=''
fi
echo -en "${YELLOW}Automatically remove build file? "
echo -en "y/[${BLUE}n${YELLOW}]/N(ever): ${NC}"
read AUTO_RM_BUILD
if [[ $AUTO_RM_BUILD == 'y' ]]; then
AUTO_RM_BUILD=1
elif [[ $AUTO_RM_BUILD == 'N' ]]; then
AUTO_RM_BUILD=0
else
AUTO_RM_BUILD=2
fi
echo -en "${YELLOW}Automatically reboot (to and from recovery)? "
echo -en "y/[${BLUE}n${YELLOW}]: ${NC}"
read AUTO_REBOOT
if [[ $AUTO_REBOOT == 'y' ]]; then
AUTO_REBOOT=1
else
AUTO_REBOOT=0
fi
echo -en "${YELLOW}Automatically switch slots on fastboot flash? "
echo -en "y/[${BLUE}n${YELLOW}]: ${NC}"
read AUTO_SLOT
if [[ $AUTO_SLOT == 'y' ]]; then
AUTO_SLOT=1
else
AUTO_SLOT=0
fi
echo -en "${YELLOW}Set extra upload message [${BLUE}blank${YELLOW}]: ${NC}"
read UPLOAD_DONE_MSG
echo -en "${YELLOW}Set extra handling done message [${BLUE}blank${YELLOW}]: ${NC}"
read HANDLE_DONE_MSG
echo -en "${YELLOW}Set extra failure message [${BLUE}blank${YELLOW}]: ${NC}"
read FAILURE_MSG
echo -en "${YELLOW}Set TWRP decryption pin "
echo -en "(0 for decrypted; blank to wait) [${BLUE}blank${YELLOW}]: ${NC}"
read TWRP_PIN
echo -en "${YELLOW}ADB sideload instead of push? "
echo -en "y/[${BLUE}n${YELLOW}]: ${NC}"
read TWRP_SIDELOAD
if [[ $TWRP_SIDELOAD = 'y' ]]; then
TWRP_SIDELOAD=1
else
TWRP_SIDELOAD=0
fi
echo -en "${YELLOW}Fastboot package flashing ? "
echo -en "[${BLUE}y${YELLOW}]/n: ${NC}"
read FASTBOOT_PKG
if [[ $FASTBOOT_PKG = 'n' ]]; then
FASTBOOT_PKG=0
else
FASTBOOT_PKG=1
fi
echo -e "${RED}Note! If you chose 'n' settings will only persist for current session${NC}"
echo -en "${YELLOW}Write current config to file? [${BLUE}y${YELLOW}]/n: ${NC}"
read isWriteConf
if [[ $isWriteConf != 'n' ]]; then
echo -en "${YELLOW}Enter config file name [${BLUE}build.conf${YELLOW}]: ${NC}"
read confPath
if [[ $confPath == '' ]]; then
confPath="build.conf"
fi
echo -e "${GREEN}Rewriting file${NC}"
rewrite_config $confPath
fi
}
# checking configs (paths only, there's a limit for the spoonfeed)
check_confs()
{
SOURCE_PATH=$(realpath "$SOURCE_PATH")
if [[ ! -d $SOURCE_PATH ]]; then
echo -en "${RED}ERROR! Provided source path "
echo -e "${BLUE}${SOURCE_PATH}${RED} is invalid${NC}"
exit 1
fi
if [[ $UNHANDLED_PATH != '' ]]; then
UNHANDLED_PATH=$(realpath "$UNHANDLED_PATH")
if [[ ! -d $UNHANDLED_PATH ]]; then
echo -en "${RED}ERROR! Provided unhandled path "
echo -e "${BLUE}${UNHANDLED_PATH}${RED} is invalid${NC}"
exit 1
fi
fi
if [[ $PRE_BUILD_SCRIPT != '' ]] && [[ ! -f $PRE_BUILD_SCRIPT ]]; then
echo -en "${RED}ERROR! Provided pre-build script "
echo -e "${BLUE}${PRE_BUILD_SCRIPT}${RED} does not exist${NC}"
exit 1
fi
if [[ $TG_SEND_CFG_FILE != '' ]] && [[ ! -f $TG_SEND_CFG_FILE ]]; then
echo -en "${RED}ERROR! Provided tlegram-send config "
echo -e "${BLUE}${TG_SEND_CFG_FILE}${RED} does not exist${NC}"
exit 1
fi
if [[ $WAS_INIT == 0 ]]; then # show not configured warning
echo -e "${RED}WARNING! Script configs were never initialized!${NC}"
echo -en "${GREEN}Please set ${BLUE}WAS_INIT${GREEN} to ${BLUE}1${GREEN} "
echo -e "in ${BLUE}build.config${GREEN} to hide this warning${NC}"
echo -e "${GREEN}You can also re run the script with ${BLUE}-i${GREEN} flag to do so"
wait_for 3
fi
}
# Prints a table to summarize flags
print_flags()
{
if [[ $productChanged != 0 ]] || [[ $targetChanged != 0 ]] || [[ $typeChanged != 0 ]] || [[ $tgConfigChanged != 0 ]] || [[ $configFile != "build.conf" ]]; then
echo -e "${YELLOW}----OVERRIDES----${NC}"
fi
[[ $configFile != "build.conf" ]] && echo -en "${RED}"
echo -e "Config file : ${BLUE}${configFile}${NC}"
[[ $configFile != "build.conf" ]] && diff --color build.conf $configFile && echo "-----------------"
[[ $productChanged != 0 ]] && echo -e "${RED}Product : ${BUILD_PRODUCT_NAME}${NC}"
[[ $targetChanged != 0 ]] && echo -e "${RED}Type cmd : ${BUILD_TYPE_CMD}${NC}"
[[ $typeChanged != 0 ]] && echo -e "${RED}Target cmd : ${TARGET_CHOOSE_CMD}${NC}"
[[ $tgConfigChanged != 0 ]] && echo -e "${RED}TG config : ${TG_SEND_CFG_FILE}${NC}"
echo
echo -e "${YELLOW}------BUILD------${NC}"
[[ $isClean == 1 ]] && echo -en "${RED}"
echo -e "Clean : ${isClean}${NC}"
[[ $installClean == 1 ]] && echo -en "${RED}"
echo -e "Installclean : ${installClean}${NC}"
[[ $isDry == 1 ]] && echo -en "${RED}"
echo -e "Dry : ${isDry}${NC}"
echo
echo -e "${YELLOW}------FILE-------${NC}"
[[ $isUpload == 1 ]] && echo -en "${RED}"
echo -e "Upload : ${isUpload}${NC}"
[[ $isPush == 1 ]] && echo -en "${RED}"
echo -e "Push : ${isPush}${NC}"
[[ $isFastboot == 1 ]] && echo -en "${RED}"
echo -e "Fastboot : ${isFastboot}${NC}"
[[ $isKeep == 1 ]] && echo -en "${RED}"
echo -e "Keep file : ${isKeep}${NC}"
[[ $isMagisk == 1 ]] && echo -en "${RED}"
echo -e "Magisk : ${isMagisk}${NC}"
echo
echo -e "${YELLOW}-------ETC-------${NC}"
[[ $isSilent == 1 ]] && echo -en "${RED}"
echo -e "Silent : ${isSilent}${NC}"
[[ $powerOpt != 0 ]] && echo -en "${RED}"
echo -e "Power : ${powerOpt}${NC}"
}
# Prints a table to summarize configs
print_confs()
{
echo
echo -e "${YELLOW}-----CONFIGS-----${NC}"
echo -e "Script dir :${BLUE} ${PWD}${NC}"
echo -e "Source dir :${BLUE} ${SOURCE_PATH}${NC}"
echo -e "Product name :${BLUE} ${BUILD_PRODUCT_NAME}${NC}"
echo -e "Upload destination :${BLUE} ${UPLOAD_DEST}${NC}"
echo -e "ADB push destination :${BLUE} ${ADB_DEST_FOLDER}${NC}"
if [[ $UNHANDLED_PATH != '' ]]; then
echo -e "Move build destination :${BLUE} ${UNHANDLED_PATH}${NC}"
fi
if [[ $UPLOAD_DONE_MSG != '' ]]; then
echo -e "Upload done message :${BLUE} ${UPLOAD_DONE_MSG}${NC}"
fi
if [[ $HANDLE_DONE_MSG != '' ]]; then
echo -e "Handling done message :${BLUE} ${HANDLE_DONE_MSG}${NC}"
fi
if [[ $FAILURE_MSG != '' ]]; then
echo -e "Failure message :${BLUE} ${FAILURE_MSG}${NC}"
fi
if [[ $preBuildScripts != "" ]]; then
for script in ${preBuildScripts// / }; do
[[ ! -f $script ]] && continue
echo -e "Pre-build script :${BLUE} ${script}${NC}"
done
fi
}
# performs required pre build operations
pre_build()
{
tg_send "[pre-build] Preparing env for <code>${BUILD_PRODUCT_NAME}</code>"
source "${SOURCE_PATH}/build/envsetup.sh"
eval $TARGET_CHOOSE_CMD # target
if [[ $isClean == 1 ]]; then
echo -e "${GREEN}Cleaning build${NC}"
tg_send "[pre-build] Cleaning build"
eval $CLEAN_CMD
fi
if [[ $installClean == 1 ]]; then
echo -e "${GREEN}Running installclean${NC}"
tg_send "[pre-build] Running installclean"
make installclean
fi
if [[ $BUILD_TYPE_CMD != '' ]]; then
eval $BUILD_TYPE_CMD # build type
fi
if [[ $PRE_BUILD_SCRIPT != '' ]] && [[ -f $PRE_BUILD_SCRIPT ]]; then
echo -e "${GREEN}Running ${BLUE}${PRE_BUILD_SCRIPT}${NC}"
tg_send "[pre-build] Running <code>${PRE_BUILD_SCRIPT}</code>"
source "${PRE_BUILD_SCRIPT}"
fi
if [[ $preBuildScripts != '' ]]; then
for script in ${preBuildScripts// / }; do
[[ ! -f $script ]] && continue
echo -e "${GREEN}Running ${BLUE}${script}${NC}"
tg_send "[pre-build] Running <code>${script}</code>"
source "${script}"
done
fi
currMsg="" # edit over pre-build ops
tg_send "Build started for <code>${BUILD_PRODUCT_NAME}</code>"
start_time=$(date +"%s")
}
# magisk patch and pull
magisk_patch()
{
stateA=('device')
adb_wait 1 "${stateA[@]}"
adb_wait_unlocked
tg_send "Patching boot.img with magisk"
echo -e "${GREEN}Magisk patching${NC}"
echo -e "${GREEN}Env setup${NC}"
idir="${SOURCE_PATH}/out/target/product/${BUILD_PRODUCT_NAME}/obj/PACKAGING/target_files_intermediates/*_${BUILD_PRODUCT_NAME}-target_files*/IMAGES"
idir=$(eval echo "${idir}")
# set these flags according to your device
mflags="KEEPVERITY=true LEGACYSAR=true RECOVERYMODE=false"
cryptoState=$(adb shell getprop ro.crypto.state)
[[ $cryptoState == "encrypted" ]] && mflags="${mflags} KEEPFORCEENCRYPT=true"
if ls "${idir}/vbmeta.img" &> /dev/null; then
mflags="${mflags} PATCHVBMETAFLAG=true"
fi
echo -e "${GREEN}Pushing ${BLUE}boot.img${NC}"
adb push "${idir}/boot.img" "/sdcard/" || exit 1
adb shell su -c "rm -f /data/adb/magisk/new-boot.img"
echo -e "${GREEN}Patching ${BLUE}/sdcard/boot.img${NC}"
adb shell su -c "${mflags} /data/adb/magisk/boot_patch.sh /sdcard/boot.img" || exit 1
ver=$(adb shell magisk -V) || exit 1
adb shell rm "/sdcard/Download/magisk_patched-*.img" &> /dev/null
adb shell su -c "mv /data/adb/magisk/new-boot.img /sdcard/Download/magisk_patched-${ver}.img" || exit 1
adb shell su -c "/data/adb/magisk/magiskboot cleanup"
echo -e "${GREEN}Pulling version ${BLUE}${ver}${NC}"
rm ./magisk_patched-*.img
adb pull "/sdcard/Download/magisk_patched-${ver}.img" ./ || exit 1
fpath=$(realpath "./magisk_patched-${ver}.img")
echo -e "${GREEN}Magisk patching done. Image: ${BLUE}${fpath}${NC}"
}
# handles the push flag (-p)
handle_push()
{
echo -e "${GREEN}Pushing...${NC}"
isOn=1 # Device is booted (reverse logic)
isRec=1 # Device is on recovery mode (reverse logic)
isPushed=1 # Weater the push went fine (reverse logic)
while [[ $isOn != 0 ]] && [[ $isRec != 0 ]] && [[ $isPushed != 0 ]]; do
adb_reset
adb devices | grep -w 'device' &> /dev/null
isOn=$?
adb devices | grep -w 'recovery' &> /dev/null
isRec=$?
sdcardPath=""
if [[ $isRec == 0 ]]; then
echo -e "${GREEN}Device detected in ${BLUE}recovery${NC}"
sdcardPath="/sdcard"
elif [[ $isOn == 0 ]]; then
echo -e "${GREEN}Device detected${NC}"
sdcardPath="/storage/emulated/0"
else
if [[ $TWRP_SIDELOAD == 1 ]]; then
isPushed=0
buildH=1
break
fi
echo -en "${RED}Please plug in a device with ADB enabled and press any key${NC}"
read -n1 temp
echo
fi
if [[ $isRec != 0 ]] && [[ $isOn != 0 ]]; then
sleep 1
continue
fi
if [[ $TWRP_SIDELOAD == 1 ]]; then
isPushed=0
buildH=1
break
fi
adb_wait_unlocked 1 "${sdcardPath}"
adb push "${PATH_TO_BUILD_FILE}" "${sdcardPath}/${ADB_DEST_FOLDER}/"
isPushed=$?
if [[ $isPushed == 0 ]]; then
echo -e "${GREEN}Pushed to: ${BLUE}${ADB_DEST_FOLDER}${NC}"
buildH=1
else
isOn=1
isRec=1
isPushed=1
echo -en "${RED}Push error (see output). Press any key to try again${NC}"
read -n1 temp
echo
fi
done
if [[ $isPushed == 0 ]]; then
if [[ $AUTO_REBOOT == 0 ]]; then
echo -en "${YELLOW}Flash now? y/[n]/A(lways): ${NC}"
read isFlash
if [[ $isFlash == 'A' ]]; then
config_write "AUTO_REBOOT" 1 $configFile
isFlash='y'
fi
else
isFlash='y'
fi
# flash build
if [[ $isFlash == 'y' ]]; then
tg_send "Flashing build"
start_time=$(date +"%s")
if [[ $isOn == 0 ]]; then
echo -e "${GREEN}Rebooting to recovery${NC}"
adb reboot recovery
isDecrypted=0
if [[ $TWRP_SIDELOAD == 0 ]]; then
stateA=('recovery')
adb_wait 3 "${stateA[@]}"
echo -e "${GREEN}Device detected in ${BLUE}recovery${NC}"
fi
if [[ $TWRP_PIN != '' ]] && [[ $TWRP_PIN != '0' ]] && [[ $TWRP_SIDELOAD == 0 ]]; then
adb_reset
echo -e "${GREEN}Trying decryption with provided pin${NC}"
adb shell twrp decrypt $TWRP_PIN
if [[ $? == 0 ]]; then
isDecrypted=1
echo -e "${GREEN}Data decrypted${NC}"
wait_for 5
adb_reset
else
echo -e "${RED}Data decryption failed. Please try manually${NC}"
fi
fi
if [[ $TWRP_PIN != '0' ]] && [[ $isDecrypted != 1 ]] && [[ $TWRP_SIDELOAD == 0 ]]; then
echo -en "${YELLOW}Press any key ${RED}after${YELLOW} decrypting data in TWRP${NC}"
read -n1 temp
echo
adb_reset
fi
else
adb_reset
fi
# Add extra pre-flash operations here
fileName=$(basename $PATH_TO_BUILD_FILE)
echo -e "${GREEN}Flashing ${BLUE}${fileName}${NC}"
isFlashed=1 # reverse logic
while [[ $isFlashed != 0 ]]; do
if [[ $TWRP_SIDELOAD == 1 ]]; then
stateA=('sideload')
adb_wait 3 "${stateA[@]}"
adb sideload $PATH_TO_BUILD_FILE
isFlashed=$?
else
adb shell twrp install "/sdcard/${ADB_DEST_FOLDER}/${fileName}"
isFlashed=$?
fi
if [[ $isFlashed != 0 ]]; then
echo -e "${RED}Flash error. Press any key to try again."
echo -en "Press 'c' to continue anyway${NC}"
read -n1 temp
[[ $temp != 'c' ]] && continue
fi
# Add additional flash operations here (magisk provided as example)
# adb shell twrp install "/sdcard/Flash/Magisk/Magisk-v20.1\(20100\).zip"
done
if [[ $AUTO_REBOOT == 0 ]]; then
echo -en "${YELLOW}Press any key to reboot${NC}"
read -n1 temp
echo
fi
adb reboot
get_time
tg-send "Flashing done in <code>${buildTime}</code>"
fi
fi
}
# handles the fastboot flag (-f)
handle_fastboot()
{
ans='n'
if [[ $AUTO_REBOOT == 1 ]]; then
ans='y'
else
echo -en "${YELLOW}Reboot to fastboot? y/[n]: ${NC}"
read ans
fi
if [[ $ans == 'y' ]]; then
adb reboot bootloader &> /dev/null
if [[ $? != 0 ]]; then
stateA=('device' 'bootloader')
adb_wait 2 "${stateA[@]}"
fi
adb devices | grep -w "device" &> /dev/null
[[ $? == 0 ]] && adb reboot bootloader &> /dev/null
fi
if ! [[ $(fastboot devices) ]]; then
echo -e "${GREEN}Waiting for device in ${BLUE}bootloader${NC}"
fi
fastboot wait-for-device &> /dev/null
wait_for 3
slot=$(fastboot getvar current-slot 2>&1 | head -n 1)
slot="$(echo $slot | sed "s/current-slot: //")"
echo -e "Currently on slot ${BLUE}${slot}${NC}"
slot2="a"
[[ $slot == "a" ]] && slot2="b"
if [[ $AUTO_SLOT == 1 ]]; then
ans=y
else
echo -en "${YELLOW}Switch to slot ${BLUE}${slot2}${YELLOW}? [y]/n: ${NC}"
read ans
fi
[[ $ans != 'n' ]] && fastboot --set-active=$slot2
# flashing
tg_send "Flashing build via fastboot"
start_time=$(date +"%s")
if [[ $FASTBOOT_PKG != 1 ]]; then
for img in ./out/target/product/"${BUILD_PRODUCT_NAME}"/obj/PACKAGING/target_files_intermediates/*_*"${BUILD_PRODUCT_NAME}"-target_files/IMAGES/*.img; do
[[ $img == "userdata" ]] && continue
partName=$(basename "${img}" | sed 's/.img//')
fastboot flash "${partName}" "${img}"
done
else
fastboot update --skip-reboot --skip-secondary $PATH_TO_BUILD_FILE
fi
# after flash operations here (magisk as an example):
# fastboot flash boot magisk_patched*.img
get_time
if [[ $AUTO_REBOOT != 1 ]]; then
echo -en "${YELLOW}Continue boot now? y/[n]: ${NC}"
read ans
else
ans=y
fi
if [[ $ans == 'y' ]]; then
wait_for 5
fastboot reboot
fi
tg_send "Flashing done in <code>${buildTime}</code>"
}
# handles the upload flag (-h)
handle_upload()
{
tg_send "Uploading build"
echo -e "${GREEN}Uploading...${NC}"
isUploaded=0
if [[ -f $PATH_TO_BUILD_FILE ]]; then
start_time=$(date +"%s")
pid=""
if [[ $isSilent == 0 ]]; then
upload_prog_send &
pid=$!
fi
trap - SIGINT # upload is a long external command with own cancellation handling