aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/usr/bin/.drive-select-objects
blob: 0002b8fd02f958f7362386b2be0e736c0b927485 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
#!/bin/bash
###################################################################################################
# .drive-select-objects
###################################################################################################
# Copyright 2017-2023 Jean-Christophe Manciot <jcmanciot@sdxlive.com>
#
# Licensed under a GPLv3 License.
# You may not use this file except in compliance with the License. You may obtain a copy of the 
# License at:
#
#    https://www.gnu.org/licenses/gpl-3.0.md
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Attribution — You must give appropriate credit, provide a link to the license, and indicate if 
#               changes were made. You may do so in any reasonable manner, but not in any way that 
#               suggests the licensor endorses you or your use.
###################################################################################################

###################################################################################################
#
# Selecting the local/remote files/folders (objects) by honouring the filters within ./.driveselect 
# & <google_drive_folder>/.driveignore, down to the depth set within
# <google_drive_folder>/.driverc.
# By default, when those files are empty, all objects recursively found locally or remotely 
# (<source>) below the current working directory are affected by the current <command>.
#
###################################################################################################
#
# Requirements:
# ------------
#
# - We assume that:
#       + drive-google package is already installed.
#       + coreutils, findutils, grep, perl-base, sed & zenity packages are already installed.
#
# - This command must be called from within your local Google drive folder(s).
#
# - <google_drive_folder>/.driverc:it must be properly setup, either manually or with drive-setup,
#                                  with the following settings:
#       * depth=
#               + -1: an infinite traversal depth, i.e recursive traversal from the current working  
#                     directory until there is no more child folder
#               + non-zero number: a fixed traversal depth
#       * ...: cf. https://github.com/odeke-em/drive for a complete list of available  
#              options. They can only be set manually through .driverc or through drive-setup for
#              the sake of simplicity.
#              Cf. /etc/drive-google/.driverc for some examples.
#
# - <google_drive_folder>/.driveignore: it may be used to exclude/include some objects from the 
#                                       scope of the current drive-<command> by using regular
#                                       expressions.
#                                       Cf. /etc/drive-google/.driveignore for its format & some 
#                                       examples.
#
# - ./.driveselect: it may be used to include/exclude the objects from the scope of the current 
#                   drive-<command>. 
#                   Cf. /etc/drive-google/.driveselect for its format & some examples.
#
###################################################################################################
#
# Parameters:
# ----------
#
# - <source>: where to recursively find objects:
#       + local_fs: on the local file system within Google Drive folder where the call is made.
#       + remote_fs: on the remote Google Drive from the equivalent folder where the call is made.
#       + select_file: all objects are already listed under [<command>] section in .driveselect.
#
# - <command>: related command corresponding to the section within ./.driveselect and/or 
#              <google_drive_folder>/.driveignore.
#              Cf. /etc/drive-google/.driveselect & /etc/drive-google/.driveignore for a list of 
#              currently supported commands.
#
###################################################################################################
#
# Return values:
# -------------
#
# - array of filtered & sorted object names (with their path)
#   return code: 0
# - "Outside drive context" if we have found the local root Google Drive folder, but we are not 
#   located beneath it.
#   return code: 7
# - "No drive context found" if there is no .gd folder above and beneath the local folder:
#       + either drive has not been initialized properly (at least one hop away from '/')
#       + or we are not located below the <google_drive_folder> top level branch
#   return code: 8
# - "Connection error accessing your Google Drive online in ${local_folder}" if drive could not 
#   connect to the online Google Drive. 
#   return code: 9
#
###################################################################################################
#
# Usage examples:
# --------------
# cd ~/<google_drive_folder>/parent/folder/
# readarray -t filtered_objects < <(.drive-select-objects 'remote_fs' 'pull')
# printf '%s\n' "${filtered_objects[@]}"
#
###################################################################################################
#
# Notes:
# -----
#
# The functions defined below use special $'string' expressions.
# Words of the form $'string' are treated specially by bash.
# The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C 
# standard. Backslash escape sequences, if present, are decoded as follows:
# - \\     backslash
# - \'     single quote
# - \"     double quotes
# - \n     new line
# - \a     alert (bell)
# - \b     backspace
# - \e
# - \E     an escape character
# - \f     form feed
# - \n     new line
# - \r     carriage return
# - \t     horizontal tab
# - \v     vertical tab
# - \nnn   the eight-bit character whose value is the octal value nnn (one to three digits)
# - \xHH   the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
# - \cx    a control-x character
#
###################################################################################################
if [[ $LOG_DRIVE_SELECT_OBJECTS == ON ]]; then
        # Getting the last existing file descriptor of ~/.drive-google/logs/drive-google.log
        fd=$(ls -al /proc/$$/fd | grep ~/.drive-google/logs/drive-google.log | sed -E 's|^.* ([0-9]+) -> .*$|\1|g' | sort -V | tail -n 1)

        # Case where fd is unset, i.e the file ~/.drive-google/logs/drive-google.log has not yet been opened
        if [[ -z $fd ]]; then
                lowest_unused_fd () 
                {
                        local fd=0
                        while [ -e /proc/$$/fd/${fd} ]; do
                                fd=$((fd+1))
                        done
                        echo $fd
                }
                fd=$(lowest_unused_fd)

                # Opening ~/.drive-google/logs/drive-google.log as file descriptor $fd for appending
                eval "exec $fd>> ~/.drive-google/logs/drive-google.log"
        fi

        # Writing the trace output generated when set -x is enabled to file descriptor $fd 
        eval "BASH_XTRACEFD=$fd"
        # Logging line numbers - We could also use ${0} for ${BASH_SOURCE}
        BASH_SOURCE_BASENAME=$(basename ${BASH_SOURCE})
        export PS4='${BASH_SOURCE_BASENAME}.${LINENO}+ '
        # Expanding all variables and prints the full commands before output of the command
        set -x
fi

# Preventing globbing (pathname expansion)
set -o noglob
# Allowing extended globbing
shopt -s extglob
# Expanding aliases
shopt -s expand_aliases

# Including alias definitions to sed/grep/awk/perl commands for readability sake
. /usr/include/drive-google/aliases

#------------------------------------build-find-include-pattern------------------------------------
# Building the include pattern to be used by 'find' only on local objects --> without prepended stats 
# Parameters:
# - <$1>:
#       + true:         use ${included_regex_patterns[@]}
#       + false:        don't use ${included_regex_patterns[@]}
# - <$2>:
#       + true:         use ${included_objects[@]}
#       + false:        don't use ${included_objects[@]}
# Return values:
# - $find_include_pattern
# Usage: find_include_pattern=$(build-find-include-pattern <true|false> <true|false>)
#        eval find "$find_include_pattern"
# The function uses the global array variables:
# - "${included_regex_patterns[@]}"
# - "${eval_esc_included_objects[@]}"
function build-find-include-pattern ()
{
        local find_include_pattern

        # Building include patterns for find

        # 1) Using regular expressions from .driveignore
        if [[ ($1 == 'true') && (${#included_regex_patterns[@]} -ne 0) ]]; then
                for included_regex_pattern in "${included_regex_patterns[@]}"
                do
                        if [[ -z "$find_include_pattern" ]]; then
                                find_include_pattern="-regextype grep -regex '\./${included_regex_pattern}'"
                        else
                                find_include_pattern="$find_include_pattern -o -regex '\./${included_regex_pattern}'"
                        fi
                done
        fi

        # 2) Using patterns from .driveselect
        # The $included_object may contain some standard wildcards
        if [[ ($2 == 'true') && (${#eval_esc_included_objects[@]} -ne 0) ]]; then
                for eval_esc_included_object in "${eval_esc_included_objects[@]}"
                do
                        if [[ -z "$find_include_pattern" ]]; then
                                find_include_pattern="-path $'./${eval_esc_included_object}'"
                        else
                                find_include_pattern="${find_include_pattern} -o -path $'./${eval_esc_included_object}'"
                        fi
                done
        fi

        # Returning the include pattern for find
        echo "$find_include_pattern"
}

#------------------------------------build-find-exclude-pattern------------------------------------
# Building the exclude pattern to be used by 'find' only on local objects --> without prepended stats 
# Parameters:
# - <$1>:
#       + true:         use ${excluded_regex_patterns[@]}
#       + false:        don't use ${excluded_regex_patterns[@]}
# - <$2>:
#       + true:         use ${excluded_objects[@]}
#       + false:        don't use ${excluded_objects[@]}
# Return values:
# - "$find_exclude_pattern"
# Usage: find_exclude_pattern=$(build-find-exclude-pattern <true|false> <true|false>)
#        eval find "$find_exclude_pattern"
# The function uses the global array variables:
# - "${excluded_regex_patterns[@]}"
# - "${eval_esc_excluded_objects[@]}"
function build-find-exclude-pattern ()
{
        local find_exclude_pattern

        # Building exclude patterns for find
        # 1) Using regular expressions from .driveignore
        if [[ ($1 == 'true') && (${#excluded_regex_patterns[@]} -ne 0) ]]; then
                for excluded_regex_pattern in "${excluded_regex_patterns[@]}"
                do
                        if [[ -z "$find_exclude_pattern" ]]; then
                                find_exclude_pattern="-regextype grep ! -regex '\./${excluded_regex_pattern}'"
                        else
                                find_exclude_pattern="$find_exclude_pattern ! -regex '\./${excluded_regex_pattern}'"
                        fi
                done
        fi

        # 2) Using patterns from .driveselect
        # The $excluded_object may contain some standard wildcards
        if [[ ($2 == 'true') && (${#eval_esc_excluded_objects[@]} -ne 0) ]]; then
                for eval_esc_excluded_object in "${eval_esc_excluded_objects[@]}"
                do
                        if [[ -z "$find_exclude_pattern" ]]; then
                                find_exclude_pattern="! -path $'./${eval_esc_excluded_object}'"
                        else
                                find_exclude_pattern="${find_exclude_pattern} ! -path $'./${eval_esc_excluded_object}'"
                        fi
                done
        fi

        # Returning the exclude pattern for find
        echo "$find_exclude_pattern"
}

#------------------------------------build-grep-include-pattern-------------------------------------
# Building the include pattern to be used with 'grep' either on local or remote objects
# Parameters:
# - <$1>:
#       + true:         use ${included_regex_patterns[@]}
#       + false:        don't use ${included_regex_patterns[@]}
# - <$2>:
#       + true:         use ${included_objects[@]}
#       + false:        don't use ${included_objects[@]}
# - <$3>:
#       + local:        no prepended stats in the object name
#       + remote:       prepended stats in the object name
# Return values:
# - "grep -P -- $'$grep_include_pattern'"
# Usage: grep_include_pattern=$(build-grep-include-pattern <true|false> <true|false> <local|remote>)
#        eval "$grep_include_pattern"
# The function uses the global array variables:
# - "${included_regex_patterns[@]}"
# - "${grep_esc_included_objects[@]}"
function build-grep-include-pattern ()
{
        local grep_include_pattern

        # Building include patterns for grep -P
 
        # 1) Using regular expressions from .driveignore
        if [[ ($1 == 'true') && (${#included_regex_patterns[@]} -ne 0) ]]; then
                # Using regular expressions from .driveignore 
                for included_regex_pattern in "${included_regex_patterns[@]}"
                do
                        if [[ -z "$grep_include_pattern" ]]; then
                                if [[ $3 == 'local' ]]; then
                                        grep_include_pattern="${included_regex_pattern}"
                                elif [[ $3 == 'remote' ]]; then
                                        grep_include_pattern="^. [0-9]+.*B ${included_regex_pattern}"
                                fi
                        else
                                if [[ $3 == 'local' ]]; then
                                        grep_include_pattern="${grep_include_pattern}|${included_regex_pattern}"
                                elif [[ $3 == 'remote' ]]; then
                                        grep_include_pattern="${grep_include_pattern}|^. [0-9]+.*B ${included_regex_pattern}"
                                fi
                        fi
                done
        fi

        # 2) Using patterns from .driveselect
        # The $included_object may contain some standard wildcards
        if [[ ($2 == 'true') && (${#grep_esc_included_objects[@]} -ne 0) ]]; then
                # Using patterns from .driveselect
                # Some supported standard wildcard(s) may be in $included_object
                for grep_esc_included_object in "${grep_esc_included_objects[@]}"
                do
                        if [[ -z "$grep_include_pattern" ]]; then
                                if [[ $3 == 'local' ]]; then
                                        grep_include_pattern="${grep_esc_included_object}"
                                elif [[ $3 == 'remote' ]]; then
                                        grep_include_pattern="^. [0-9]+.*B ${grep_esc_included_object}"
                                fi
                        else
                                if [[ $3 == 'local' ]]; then
                                        grep_include_pattern="${grep_include_pattern}|${grep_esc_included_object}"
                                elif [[ $3 == 'remote' ]]; then
                                        grep_include_pattern="${grep_include_pattern}|^. [0-9]+.*B ${grep_esc_included_object}"
                                fi
                        fi
                done
        fi

        # Returning the grep include pattern
        if [[ ! -z "$grep_include_pattern" ]]; then
                echo "grep -P -- $'$grep_include_pattern'"
        fi
}

#------------------------------------build-grep-exclude-pattern-------------------------------------
# Building the exclude pattern to be used with 'grep' either on local or remote objects
# Parameters:
# - <$1>:
#       + true:         use ${excluded_regex_patterns[@]}
#       + false:        don't use ${excluded_regex_patterns[@]}
# - <$2>:
#       + true:         use ${excluded_objects[@]}
#       + false:        don't use ${excluded_objects[@]}
# - <$3>:
#       + local:        no prepended stats in the object name
#       + remote:       prepended stats in the object name
# Return values:
# - "grep -vP -- $'$grep_exclude_pattern'"
# Usage: grep_exclude_pattern=$(build-grep-exclude-pattern <true|false> <true|false> <local|remote>)
#        eval "$grep_exclude_pattern"
# The function uses the global array variables:
# - "${excluded_regex_patterns[@]}"
# - "${grep_esc_excluded_objects[@]}"
function build-grep-exclude-pattern ()
{
        local grep_exclude_pattern

        # Building exclude patterns with grep
        # 1) Using regular expressions from .driveignore
        if [[ ($1 == 'true') && (${#excluded_regex_patterns[@]} -ne 0) ]]; then
                # Using regular expressions from .driveignore 
                for excluded_regex_pattern in "${excluded_regex_patterns[@]}"
                do
                        if [[ -z "$grep_exclude_pattern" ]]; then
                                if [[ $3 == 'local' ]]; then
                                        grep_exclude_pattern="${excluded_regex_pattern}$"
                                elif [[ $3 == 'remote' ]]; then
                                        grep_exclude_pattern="^. [0-9]+.*B ${excluded_regex_pattern}$"
                                fi
                        else
                                if [[ $3 == 'local' ]]; then
                                        grep_exclude_pattern="${grep_exclude_pattern}|${excluded_regex_pattern}$"
                                elif [[ $3 == 'remote' ]]; then
                                        grep_exclude_pattern="${grep_exclude_pattern}|^. [0-9]+.*B ${excluded_regex_pattern}$"
                                fi
                        fi
                done
        fi

        # 2) Using patterns from .driveselect
        # The $esc_excluded_object may contain some standard wildcards converted to regex
        if [[ ($2 == 'true') && (${#grep_esc_excluded_objects[@]} -ne 0) ]]; then
                for grep_esc_excluded_object in "${grep_esc_excluded_objects[@]}"
                do
                        if [[ -z "$grep_exclude_pattern" ]]; then
                                if [[ $3 == 'local' ]]; then
                                        grep_exclude_pattern="${grep_esc_excluded_object}$"
                                elif [[ $3 == 'remote' ]]; then
                                        grep_exclude_pattern="^. [0-9]+.*B ${grep_esc_excluded_object}$"
                                fi
                        else
                                if [[ $3 == 'local' ]]; then
                                        grep_exclude_pattern="${grep_exclude_pattern}|${grep_esc_excluded_object}$"
                                elif [[ $3 == 'remote' ]]; then
                                        grep_exclude_pattern="${grep_exclude_pattern}|^. [0-9]+.*B ${grep_esc_excluded_object}$"
                                fi
                        fi
                done
        fi

        # Returning the exclude pattern
        if [[ ! -z "$grep_exclude_pattern" ]]; then
                echo "grep -vP -- $'$grep_exclude_pattern'"
        fi
}

#------------------------------------build-sed-exclude-pattern-------------------------------------
# Building the exclude pattern to be used with 'sed' either on local or remote objects
# Parameters:
# - <$1>:
#       + local:        no prepended stats in the object name
#       + remote:       prepended stats in the object name
# Return values:
# - "sed -E $'$sed_exclude_pattern'"
# Usage: sed_exclude_pattern=$(build-sed-exclude-pattern <local|remote>)
#        eval "$sed_exclude_pattern"
# The function uses the global array variable:
# - "${sed_esc_excluded_objects[@]}"
# ---------------------------------------Warning---------------------------------------------------
# ${excluded_regex_patterns[@]} from .driveignore cannot be used here because they are already used 
# by grep.
# Grep & sed use different escape characters list: cf. /usr/include/drive-google/aliases
# ---------------------------------------Warning---------------------------------------------------
function build-sed-exclude-pattern ()
{
        local sed_exclude_pattern

        # Building exclude patterns with sed
        # Using only patterns from .driveselect
        # The $esc_excluded_object may contain some standard wildcards converted to regex
        if [[ ${#sed_esc_excluded_objects[@]} -ne 0 ]]; then
                for sed_esc_excluded_object in "${sed_esc_excluded_objects[@]}"
                do
                        if [[ -z "$sed_exclude_pattern" ]]; then
                                if [[ $1 == 'local' ]]; then
                                        sed_exclude_pattern="/${sed_esc_excluded_object}$/d"
                                elif [[ $1 == 'remote' ]]; then
                                        sed_exclude_pattern="/^. [0-9]\+.*B ${sed_esc_excluded_object}$/d"
                                fi
                        else
                                if [[ $1 == 'local' ]]; then
                                        sed_exclude_pattern="${sed_exclude_pattern}; /${sed_esc_excluded_object}$/d"
                                elif [[ $1 == 'remote' ]]; then
                                        sed_exclude_pattern="${sed_exclude_pattern}; /^. [0-9]\+.*B ${sed_esc_excluded_object}$/d"
                                fi
                        fi
                done
        fi

        # Returning the exclude pattern
        if [[ ! -z "$sed_exclude_pattern" ]]; then
                echo "sed $'$sed_exclude_pattern'"
        fi
}

#------------------------------------select-hidden-objects-----------------------------------------
# Selecting hidden objects (those which begin with a dot) or not based on hidden configuration in 
# .driverc passed as parameter
# Parameters:
# - <hidden>:
#       + true:         keep hidden objects
#       + false:        drop hidden objects
# Return values:
# - "cat":                                       hidden objects are not filtered
# - "sed $'$sed_select_hidden_objects_pattern'": hidden objects are filtered
# Usage: sed_select_hidden_objects_pattern=$(select-hidden-objects <true|false>)
#        eval "$sed_select_hidden_objects_pattern"
# The function uses no global variable
function select-hidden-objects ()
{

        if [[ $1 == 'true' ]]; then
                # 1) keep hidden objects
                # Nothing is filtered
                echo 'cat'
        else
                # 2) drop hidden objects
                sed_select_hidden_objects_pattern="/^\\..*/d; /.*\\/\\..*/d"
                echo "sed $'$sed_select_hidden_objects_pattern'"
        fi
}

#------------------------------------.drive-select-objects---------------------------------------
source=$1
command=$2

# Finding local root Google Drive folder
gd_root_folder="$(.drive-find-gd)"

case $? in
        0)
                # Everything is fine - we continue
                ;;
        7)
                # We have found the local root Google Drive folder, but we are not located beneath it.
                echo "Outside drive context"
                exit 7
                ;;
        8)
                # We cannot locate .gd folder above and beneath the local folder
                echo "No drive context found"
                exit 8
                ;;
        *)
                # Something is fatally wrong
                echo "Something is fatally wrong"
                exit 99
                ;;
esac

local_folder="$(pwd)"

# Removing all occurrences of '&' in $object as a workaround to a markup ampersand issue with zenity
noamp_local_folder="$(echo "${local_folder}" | rm_ampersand)"

local_folder_path_from_gd_root="${local_folder#$gd_root_folder}"

# Escaping the '$.*/[\]^' characters in sed searched string
# Letters, digits and (){}+?| must not be escaped
esc_local_folder_path_from_gd_root="$(echo "$local_folder_path_from_gd_root" | escape_sed_bre_search_string)"

# Reading depth from .driverc
depth_value=$(.drive-read-driverc depth "$command")
if [[ -z "$depth_value" ]]; then
        # Setting default value
        depth_value=-1
fi

# Reading hidden_value from .driverc
hidden_value=$(.drive-read-driverc hidden "$command")

if [[ "$hidden_value" != 'true' ]]; then
        # Setting default value
        hidden_value='false'
fi

#--------------------------Reading .driveselect----------------------------------------------------
# Array which will contain all read included objects names from .driveselect (not beginning with !)
read_included_objects=()
# and for which some special characters are escaped for eval
read_eval_esc_included_objects=()
# and for which some special characters are escaped for grep searched string
read_grep_esc_included_objects=()
# Array which will contain all read excluded objects names from .driveselect (beginning with ! before its removal)
read_excluded_objects=()
# and for which some special characters are escaped for eval
read_eval_esc_excluded_objects=()
# and for which some special characters are escaped for grep searched string
read_grep_esc_excluded_objects=()
# and for which some special characters are escaped for sed searched string
read_sed_esc_excluded_objects=()
# Array which will contain all sorted & deduplicated included objects names from .driveselect (not beginning with !)
included_objects=()
# and for which some special characters are escaped for eval
eval_esc_included_objects=()
# and for which some special characters are escaped for grep searched string
grep_esc_included_objects=()
# Array which will contain all sorted & deduplicated excluded objects names from .driveselect (beginning with ! before its removal)
excluded_objects=()
# and for which some special characters are escaped for eval
eval_esc_excluded_objects=()
# and for which some special characters are escaped for grep searched string
grep_esc_excluded_objects=()
# and for which some special characters are escaped for sed searched string
sed_esc_excluded_objects=()

section='out'
command_section=false

# If .driveselect exists
if [[ -f .driveselect ]]; then
        # Reading the object names within .driveselect in the right [$command] or [global] section
        # Some standard wildcards are supported
        while IFS='' read -r line || [[ -n "$line" ]]; do
                # Checking for blank lines or comments
                if [[ (-z "${line}") || ("${line}" =~ ^#) ]]; then
                        continue
                # Checking for [global] or [all] or [*]
                elif [[ "$line" =~ (^\[global\]$)|(^\[all\]$)|(^\[\*\]$) ]]; then
                        section='in'
                        unset read_included_objects
                        unset esc_read_excluded_objects
                # Checking for [$command] (or among other commands separated by /)
                elif [[ ("$line" =~ ^\[${command}\]$) || ("$line" =~ ^\[.*\/${command}\]$) || ("$line" =~ ^\[${command}\/.*\]$) || ("$line" =~ ^\[.*\/${command}\/.*\]$) ]]; then
                        # [$command] section replaces [global] section
                        section='in'
                        unset read_included_objects
                        unset esc_read_excluded_objects
                        command_section=true
                elif [[ ($section == 'in') && ("$line" =~ ^\[.*\]$) ]]; then
                        if [[ $command_section == 'true' ]]; then
                                # We've just finished reading the last [$command] section: we're done
                                break
                        else
                                section='out'
                        fi
                elif [[ $section == 'in' ]]; then
                        if [[ "$line" =~ ^! ]]; then
                                # Line begins with !: object to be excluded
                                #       - Removing leading !
                                excluded_object=$(echo "$line" | rm_not)
                                read_excluded_objects=("${read_excluded_objects[@]}" "$excluded_object")
                                #       - Escaping some special characters for later eval:
                                #               + single quote
                                #               + double quotes
                                eval_esc_excluded_object=$(echo "$excluded_object" | escape_single_quotes | escape_double_quotes)
                                read_eval_esc_excluded_objects=("${read_eval_esc_excluded_objects[@]}" "$eval_esc_excluded_object")
                                #       - Escaping the '$.\^(){}+' characters for later grep -vP searched string
                                #         '?*[]' are not escaped
                                #       - Replacing some wildcards by their regular expression equivalent ([] are left unmodified)
                                #               + ? --> .
                                #               + * --> .*
                                #       - Escaping some special characters for later eval: 
                                #         Done after previous escaping to prevent backslash escapîng
                                #               + single quote
                                #               + double quotes
                                grep_esc_excluded_object=$(echo "$excluded_object" | escape_grep_search_string | replace_wildcards_regex | escape_single_quotes | escape_double_quotes)
                                read_grep_esc_excluded_objects=("${read_grep_esc_excluded_objects[@]}" "$grep_esc_excluded_object")
                                #       - Escaping the '$.^/\' characters for later sed searched string
                                #       - Letters, digits and '(){}+?|[]*' are not  escaped
                                #       - Replacing some wildcards by their regular expression equivalent ([] are left unmodified)
                                #               + ? --> .
                                #               + * --> .*
                                #       - Escaping some special characters for later eval: 
                                #         Done after previous escaping to prevent backslash escapîng
                                #               + single quote
                                #               + double quotes
                                sed_esc_excluded_object=$(echo "$excluded_object" | escape_limited_sed_bre_search_string | replace_wildcards_regex | escape_single_quotes | escape_double_quotes)
                                read_sed_esc_excluded_objects=("${read_sed_esc_excluded_objects[@]}" "$sed_esc_excluded_object")
                        else
                                # Line does not begin with !: object to be included
                                read_included_objects=("${read_included_objects[@]}" "$line")
                                #       - Escaping some special characters for later eval:
                                #               + single quote
                                #               + double quotes
                                eval_esc_included_object=$(echo "$line" | escape_single_quotes | escape_double_quotes)
                                read_eval_esc_included_objects=("${read_eval_esc_included_objects[@]}" "$eval_esc_included_object")
                                #       - Escaping the '$.\^(){}+' characters for later grep -P searched string
                                #         '?*[]' are not escaped
                                #       - Replacing some wildcards by their regular expression equivalent ([] are left unmodified)
                                #               + ? --> .
                                #               + * --> .*
                                #       - Escaping some special characters for later eval: 
                                #         Done after previous escaping to prevent backslash escapîng
                                #               + single quote
                                #               + double quotes
                                grep_esc_included_object=$(echo "$line" | escape_grep_search_string | replace_wildcards_regex | escape_single_quotes | escape_double_quotes)
                                read_grep_esc_included_objects=("${read_grep_esc_included_objects[@]}" "$grep_esc_included_object")
                        fi
                fi
        done < .driveselect
fi

# Sorting & deduplicating the object names in alphabatical order
if [[ ${#read_included_objects[@]} -ne 0 ]]; then
        IFS=$'\n' included_objects=($(sort -u <<<"${read_included_objects[*]}"))
        unset IFS
fi
# Sorting & deduplicating the object names in alphabatical order
if [[ ${#read_eval_esc_included_objects[@]} -ne 0 ]]; then
        IFS=$'\n' eval_esc_included_objects=($(sort -u <<<"${read_eval_esc_included_objects[*]}"))
        unset IFS
fi
# Sorting & deduplicating the object names in alphabatical order
if [[ ${#read_grep_esc_included_objects[@]} -ne 0 ]]; then
        IFS=$'\n' grep_esc_included_objects=($(sort -u <<<"${read_grep_esc_included_objects[*]}"))
        unset IFS
fi
# Sorting & deduplicating the object names in alphabatical order
if [[ ${#read_excluded_objects[@]} -ne 0 ]]; then
        IFS=$'\n' excluded_objects=($(sort -u <<<"${read_excluded_objects[*]}"))
        unset IFS
fi
# Sorting & deduplicating the object names in alphabatical order
if [[ ${#read_eval_esc_excluded_objects[@]} -ne 0 ]]; then
        IFS=$'\n' eval_esc_excluded_objects=($(sort -u <<<"${read_eval_esc_excluded_objects[*]}"))
        unset IFS
fi
# Sorting & deduplicating the object names in alphabatical order
if [[ ${#read_grep_esc_excluded_objects[@]} -ne 0 ]]; then
        IFS=$'\n' grep_esc_excluded_objects=($(sort -u <<<"${read_grep_esc_excluded_objects[*]}"))
        unset IFS
fi
# Sorting & deduplicating the object names in alphabatical order
if [[ ${#read_sed_esc_excluded_objects[@]} -ne 0 ]]; then
        IFS=$'\n' sed_esc_excluded_objects=($(sort -u <<<"${read_sed_esc_excluded_objects[*]}"))
        unset IFS
fi

#--------------------------Reading .driveignore----------------------------------------------------
# Array which will contain all regular expressions from .driveignore including objects from the scope of the current drive-<command>
read_included_regex_patterns=()
# Array which will contain all regular expressions from .driveignore excluding objects from the scope of the current drive-<command>
read_excluded_regex_patterns=()
# Array which will contain all sorted & deduplicated regular expressions from .driveignore including objects from the scope of the current drive-<command>
included_regex_patterns=()
# Array which will contain all sorted & deduplicated regular expressions from .driveignore excluding objects from the scope of the current drive-<command>
excluded_regex_patterns=()

#--------------------------The sections feature is not supported by drive yet---------------------- 
# section='out'
# command_section=false
#
# If .driveignore exists
# if [[ -f "${gd_root_folder}"/.driveignore ]]; then
#         # Reading the regular expressions within .driveignore in the right [$command] or [global] section
#         while IFS='' read -r line || [[ -n "$line" ]]; do
#                 # Checking for blank lines or comments
#                 if [[ (-z "${line}") || ("${line}" =~ ^#) ]]; then
#                         continue
#                 # Checking for [global] or [all] or [*]
#                 elif [[ "$line" =~ (^\[global\])|(^\[all\])|(^\[\*\]) ]]; then
#                         section='in'
#                         unset read_included_regex_patterns
#                         unset read_excluded_regex_patterns
#                 # Checking for [$command] (or among other commands separated by /)
#                 elif [[ ("$line" =~ ^\[${command}\]) || ("$line" =~ ^\[.*\/${command}\]) || ("$line" =~ ^\[${command}\/.*\]) || ("$line" =~ ^\[.*\/${command}\/.*\]) ]]; then
#                         # [$command] section replaces [global] section
#                         section='in'
#                         unset read_included_regex_patterns
#                         unset read_excluded_regex_patterns
#                         command_section=true
#                 elif [[ ($section == 'in') && ("$line" =~ ^\[.*\]) ]]; then
#                         if [[ $command_section == 'true' ]]; then
#                                 # We've just finished reading the last [$command] section: we're done
#                                 break
#                         else
#                                 section='out'
#                         fi
#                 elif [[ $section == 'in' ]]; then
#                         if [[ "$line" =~ ^! ]]; then
#                                 # Line begins with !: objects to be included
#                                 #       - Removing leading !
#                                 included_regex_pattern=$(echo "$line" | sed 's|^!||')
#                                 read_included_regex_patterns=("${read_included_regex_patterns[@]}" "$included_regex_pattern")
#                         else
#                                 # Line does not begin with !: objects to be excluded
#                                 read_excluded_regex_patterns=("${read_excluded_regex_patterns[@]}" "$line")
#                         fi
#                 fi
#         done < "${gd_root_folder}"/.driveignore
# fi
#--------------------------The sections feature is not supported by drive yet---------------------- 

# If .driveignore exists
if [[ -f "${gd_root_folder}"/.driveignore ]]; then
        while IFS='' read -r line || [[ -n "$line" ]]; do
                # Checking for blank lines or comments
                if [[ (-z "$line") || ("$line" =~ ^#) ]]; then
                        continue
                elif [[ "$line" =~ ^! ]]; then
                        # Removing leading ! before storing the inverted regex
                        stripped_line="$(echo $line | rm_not)"
                        read_included_regex_patterns=("${read_included_regex_patterns[@]}" "$stripped_line")
                else
                        read_excluded_regex_patterns=("${read_excluded_regex_patterns[@]}" "$line")
                fi
        done < "${gd_root_folder}"/.driveignore
fi

# Sorting & deduplicating the object names in alphabatical order
if [[ ${#read_included_regex_patterns[@]} -ne 0 ]]; then
        IFS=$'\n' included_regex_patterns=($(sort -u <<<"${read_included_regex_patterns[*]}"))
        unset IFS
fi
# Sorting & deduplicating the object names in alphabatical order
if [[ ${#read_excluded_regex_patterns[@]} -ne 0 ]]; then
        IFS=$'\n' excluded_regex_patterns=($(sort -u <<<"${read_excluded_regex_patterns[*]}"))
        unset IFS
fi

#--------------------------Building the complete/filtered list of included objects into:-----------
#       - ${complete_objects[@]} for 'remote_fs' source;                        **the objects will be filtered afterwards**
#       - ${filtered_objects[@]} for 'local_fs' and 'select_file' sources:      **the objects are filtered now**

esc_excluded_objects_paths=()
# Array which will contain all read object names from .driveignore & .driveselect and recursively found remotely
complete_objects=()

# Array which will contain all objects from ${included_objects[@]}/${complete_objects[@]} filtered with:
#       - ${excluded_regex_patterns[@]}         read from .driveignore
#       - ${excluded_objects[@]}                read from .driveselect
# Job done below only for 'local_fs' source
# Job done later for 'remote_fs' source
# Job already done for 'select_file' source
filtered_objects=()

# Array which will contain all sorted (dismissing the first columns describing the object stats) 
# & deduplicated objects from ${filtered_objects[@]}
sorted_objects=()

# Array which will contain all objects names from ${sorted_objects[@]} trimmed:
#       - only for delete and trash commands
#       - if a file has been previously filtered, we make sure its folder is removed from the final list
trimmed_objects=()

#--------------------------No ${included_objects[@]} nor ${included_regex_patterns[@]}-------------
if [[ (${#included_objects[@]} -eq 0) && (${#included_regex_patterns[@]} -eq 0) ]]; then
        # .driveselect is empty regarding included objects, so all recursively found objects will be affected by the command
        # except for all filtered objects
        case $source in
                # When the source objects are remote, the list objects are made with "drive list"
                remote_fs)
                        # Reading all objects with "drive list" after having kept only:
                        #       + the object type (d: folder and -:file) 
                        #       + the object size (in B, KB, MB, TB...))
                        #       + the object name without its $local_folder_path_from_gd_root/ (all returned objects path start at GD root folder)
                        if [[ $command != 'untrash' ]]; then
                                while IFS= read -d $'\n' -r object ; do
                                        if [[ "${object[@]}" =~ 'Get https://www.googleapis.com/drive/' ]]; then
                                                # Connection error
                                                # Displaying the error 
                                                zenity --error --title "Fatal connection error accessing your Google Drive online in $noamp_local_folder" --text="Could not connect to https://www.googleapis.com/drive/v2/files or/nor to https://accounts.google.com/o/oauth2/token" --width 1400

                                                # Aborting
                                                echo "Connection error accessing your Google Drive online in ${local_folder}"
                                                exit 9
                                        fi

                                        complete_objects=("${complete_objects[@]}" "$object")
                                done < <(drive list -long -depth $depth_value -no-prompt 2>&1 | extract_object_type_size_name)
                        else
                                # Reading the trash
                                while IFS= read -d $'\n' -r object ; do
                                        if [[ "${object[@]}" =~ 'Get https://www.googleapis.com/drive/' ]]; then
                                                # Connection error
                                                # Displaying the error 
                                                zenity --error --title "Fatal connection error accessing your Google Drive online in $noamp_local_folder" --text="Could not connect to https://www.googleapis.com/drive/v2/files or/nor to https://accounts.google.com/o/oauth2/token" --width 1400

                                                # Aborting
                                                echo "Connection error accessing your Google Drive online in ${local_folder}"
                                                exit 9
                                        fi

                                        complete_objects=("${complete_objects[@]}" "$object")
                                done < <(drive list -long -trashed -depth $depth_value -no-prompt 2>&1 | extract_object_type_size_name)
                        fi
                        ;;
                # When the source objects are local, the list objects are made with "find or ls"
                local_fs)
                        # Building the 'sed' hidden objects select pattern with only:
                        #       - "$hidden_value"
                        sed_select_hidden_objects_pattern=$(select-hidden-objects "$hidden_value")
                        if [[ $depth_value -ge 1 ]]; then
                                # Building the exclude pattern to be used by 'find' with:
                                #       - "${excluded_regex_patterns[@]}"
                                #       - "${eval_esc_excluded_objects[@]}"
                                find_exclude_pattern=$(build-find-exclude-pattern 'true' 'true')

                                # Reading all objects with "find" after having 
                                #       - filtered the results with $find_exclude_pattern
                                #       - removed all leading './'
                                while IFS= read -d $'\n' -r object ; do
                                        if [[ "$object" == '.' ]]; then
                                                continue
                                        fi

                                        # Prepend a leading character symbolizing the object type followed by its size in B/KB/MB/TB
                                        object=$(.drive-prepend-object-type-size "$object")

                                        filtered_objects=("${filtered_objects[@]}" "$object")
                                done < <(eval find -L . -maxdepth $depth_value "$find_exclude_pattern" 2>/dev/null | rm_dot_slash | eval "$sed_select_hidden_objects_pattern")
                        elif [[ $depth_value -eq 0 ]]; then
                                # Building the 'grep' exclude pattern with only:
                                #       - "${excluded_regex_patterns[@]}"
                                #       - "${grep_esc_excluded_objects[@]}"
                                grep_exclude_pattern=$(build-grep-exclude-pattern 'true' 'true' 'local')

                                # Reading all objects with "ls" after having 
                                #       - filtered the results with "$grep_exclude_pattern"
                                if [[ ! -z "$grep_exclude_pattern" ]]; then
                                        while IFS= read -d $'\n' -r object ; do
                                                # Prepend a leading character symbolizing the object type followed by its size in B/KB/MB/TB
                                                object=$(.drive-prepend-object-type-size "$object")

                                                filtered_objects=("${filtered_objects[@]}" "$object")
                                        done < <(ls -A | eval "$grep_exclude_pattern" | eval "$sed_select_hidden_objects_pattern")
                                else
                                        while IFS= read -d $'\n' -r object ; do
                                                # Prepend a leading character symbolizing the object type followed by its size in B/KB/MB/TB
                                                object=$(.drive-prepend-object-type-size "$object")

                                                filtered_objects=("${filtered_objects[@]}" "$object")
                                        done < <(ls -A | insert_new_line | eval "$sed_select_hidden_objects_pattern")
                                fi
                        else
                                # When $depth_value is -1
                                # Building the exclude pattern to be used by 'find'
                                #       - "${excluded_regex_patterns[@]}"
                                #       - "${eval_esc_excluded_objects[@]}"
                                find_exclude_pattern=$(build-find-exclude-pattern 'true' 'true')

                                # Reading all objects with "find" after having 
                                #       - filtered the results with $find_exclude_pattern
                                #       - removed all leading './'
                                while IFS= read -d $'\n' -r object ; do
                                        if [[ "$object" == '.' ]]; then
                                                continue
                                        fi

                                        # Prepend a leading character symbolizing the object type followed by its size in B/KB/MB/TB
                                        object=$(.drive-prepend-object-type-size "$object")

                                        filtered_objects=("${filtered_objects[@]}" "$object")
                                done < <(eval  find -L . "$find_exclude_pattern" 2>/dev/null | rm_dot_slash | eval "$sed_select_hidden_objects_pattern")
                        fi
                        ;;
                select_file)
                        # Nothing to do here
                        ;;
        esac
else
        # .driveselect or .driveignore are NOT empty, 
        # so only recursively found objects within the listed objects will be affected by the command
        case $source in
                remote_fs)
                #-------Handling ${included_objects[@]}/${esc_included_objects[@]} & ${included_regex_patterns[@]}-
                        # Supported standard wildcards may be in $included_objects
                        # Building the 'grep' include pattern with:
                        #       - "${included_regex_patterns[@]}"
                        #       - "${grep_esc_included_objects[@]}"
                        grep_include_pattern=$(build-grep-include-pattern 'true' 'true' 'remote')

                        # Reading all objects from "drive list" after having 
                        #       - kept only:
                        #               + the object type (d: folder and -:file) 
                        #               + the object size (in B, KB, MB, TB...))
                        #               + the object name without its $local_folder_path_from_gd_root/ (all returned objects path start
                        #       - filtered the list only once with the $grep_include_pattern
                        if [[ $command != 'untrash' ]]; then
                                while IFS= read -d $'\n' -r object ; do
                                        if [[ "${object[@]}" =~ 'Get https://www.googleapis.com/drive/' ]]; then
                                                # Connection error
                                                # Displaying the error 
                                                zenity --error --title "Fatal connection error accessing your Google Drive online in ${noamp_local_folder}" --text="Could not connect to https://www.googleapis.com/drive/v2/files or/nor to https://accounts.google.com/o/oauth2/token" --width 1400

                                                # Aborting
                                                echo "Connection error accessing your Google Drive online in ${local_folder}"
                                                exit 9
                                        elif [[ "${object[@]}" =~ 'cannot be found remotely' ]]; then
                                                # $object does not exist remotely
                                                continue
                                        else
                                                # $object does exist remotely
                                                complete_objects=("${complete_objects[@]}" "$object")
                                        fi
                                done < <(drive list -long -depth $depth_value -no-prompt 2>&1 | extract_object_type_size_name | eval "$grep_include_pattern")
                        else
                                # Reading the trash
                                while IFS= read -d $'\n' -r object ; do
                                        if [[ "${object[@]}" =~ 'Get https://www.googleapis.com/drive/' ]]; then
                                                # Connection error
                                                # Displaying the error 
                                                zenity --error --title "Fatal connection error accessing your Google Drive online in ${noamp_local_folder}" --text="Could not connect to https://www.googleapis.com/drive/v2/files or/nor to https://accounts.google.com/o/oauth2/token" --width 1400

                                                # Aborting
                                                echo "Connection error accessing your Google Drive online in ${local_folder}"
                                                exit 9
                                        elif [[ "${object[@]}" =~ 'cannot be found remotely' ]]; then
                                                # $object does not exist remotely
                                                continue
                                        else
                                                # $object does exist remotely
                                                complete_objects=("${complete_objects[@]}" "$object")
                                        fi
                                done < <(drive list -long -trashed -depth $depth_value -no-prompt 2>&1 | extract_object_type_size_name | eval "$grep_include_pattern")
                        fi
                        ;;
                local_fs)
                        tmp_objects=()
                        tmp_filtered_objects=()

                        # Building the 'find' exclude pattern with:
                        #       - "${excluded_regex_patterns[@]}"
                        #       - "${eval_esc_excluded_objects[@]}"
                        find_exclude_pattern=$(build-find-exclude-pattern 'true' 'true')

                        # Building the 'grep' exclude pattern with:
                        #       - "${excluded_regex_patterns[@]}"
                        #       - "${grep_esc_excluded_objects[@]}"
                        grep_exclude_pattern=$(build-grep-exclude-pattern 'true' 'true' 'local')

                        # Building the 'sed' hidden objects select pattern with only:
                        #       - "$hidden_value"
                        sed_select_hidden_objects_pattern=$(select-hidden-objects "$hidden_value")

                #-------Handling ${included_objects[@]}--------------------------------------------
                        for included_object in "${included_objects[@]}"
                        do
                                # Doesn't $included_object contain supported standard wildcards?
                                if [[ -z $(echo "$included_object" | extract_standard_wildcards) ]]; then
                                        # No supported standard wildcards in $included_object
                                        if [[ -d "$included_object" ]]; then
                                                if [[ $depth_value -ge 1 ]]; then
                                                        # Reading all objects with "find" after having 
                                                        #       - included the results matching $included_object
                                                        #       - filtered the results with $find_exclude_pattern
                                                        while IFS= read -d $'\n' -r object ; do
                                                                # Prepend a leading character symbolizing the object type followed by its size in B/KB/MB/TB
                                                                object=$(.drive-prepend-object-type-size "$object")

                                                                tmp_objects=("${tmp_objects[@]}" "$object")
                                                        done < <(find -L "$included_object" -maxdepth $depth_value 2>/dev/null | eval "$grep_exclude_pattern" | eval "$sed_select_hidden_objects_pattern")
                                                elif [[ $depth_value -eq 0 ]]; then
                                                        if [[ ("$included_object" == '.') || ("$included_object" == '..') ]]; then
                                                                continue
                                                        fi

                                                        # Excluding the objects matching the patterns from:
                                                        # - ${excluded_regex_patterns[@]}
                                                        # - ${grep_esc_excluded_objects[@]}
                                                        if [[ ! -z "$grep_exclude_pattern" ]]; then
                                                                filtered_object=$(echo "$included_object" | eval "$grep_exclude_pattern" | eval "$sed_select_hidden_objects_pattern")
                                                        else
                                                                filtered_object=$(echo "$included_object" | eval "$sed_select_hidden_objects_pattern")
                                                        fi

                                                        if [[ -z "$filtered_object" ]]; then
                                                                # $included_object has just been excluded
                                                                continue
                                                        fi

                                                        # Prepend a leading character symbolizing the object type followed by its size in B/KB/MB/TB
                                                        filtered_object=$(.drive-prepend-object-type-size "$filtered_object")

                                                        tmp_objects=("${tmp_objects[@]}" "$filtered_object")
                                                else
                                                        # When $depth_value is -1
                                                        # Reading all objects with "find" after having 
                                                        #       - included the results matching $included_object
                                                        #       - filtered the results with $find_exclude_pattern
                                                        while IFS= read -d $'\n' -r object ; do
                                                                # Prepend a leading character symbolizing the object type followed by its size in B/KB/MB/TB
                                                                object=$(.drive-prepend-object-type-size "$object")

                                                                tmp_objects=("${tmp_objects[@]}" "$object")
                                                        done < <(find -L "$included_object" 2>/dev/null | eval "$grep_exclude_pattern" | eval "$sed_select_hidden_objects_pattern")
                                                fi

                                                # Concatenating tmp_objects[] with filtered_objects[] array
                                                tmp_filtered_objects=("${filtered_objects[@]}")
                                                filtered_objects=("${tmp_filtered_objects[@]}" "${tmp_objects[@]}")
                                                unset tmp_objects
                                                unset tmp_filtered_objects
                                        elif [[ -f "$included_object" ]]; then
                                                # $included_object is a file here
                                                # Excluding the objects matching the patterns from:
                                                # - ${excluded_regex_patterns[@]}
                                                # -${grep_esc_excluded_objects[@]}
                                                if [[ ! -z "$grep_exclude_pattern" ]]; then
                                                        filtered_object=$(echo "$included_object" | eval "$grep_exclude_pattern" | eval "$sed_select_hidden_objects_pattern")
                                                else
                                                        filtered_object=$(echo "$included_object" | eval "$sed_select_hidden_objects_pattern")
                                                fi

                                                if [[ -z "$filtered_object" ]]; then
                                                        # $included_object has just been excluded
                                                        continue
                                                fi

                                                # Prepend a leading character symbolizing the object type followed by its size in B/KB/MB/TB
                                                filtered_object=$(.drive-prepend-object-type-size "$filtered_object")

                                                filtered_objects=("${filtered_objects[@]}" "$filtered_object")
                                        else
                                                # $included_object does not exist locally
                                                continue
                                        fi
                                else
                                        # Some supported standard wildcard(s) in $included_object
                                        if [[ $depth_value -ge 0 ]]; then
                                                # Reading all objects with "find" after having 
                                                #       - included the results matching $included_object
                                                #       - excluded the objects matching the patterns from:
                                                #               + ${excluded_regex_patterns[@]}
                                                #               + ${grep_esc_excluded_objects[@]}
                                                #       - removed all leading './'
                                                while IFS= read -d $'\n' -r object ; do
                                                        # Prepend a leading character symbolizing the object type followed by its size in B/KB/MB/TB
                                                        object=$(.drive-prepend-object-type-size "$object")

                                                        tmp_objects=("${tmp_objects[@]}" "$object")
                                                done < <(find -L -maxdepth $depth_value -path "./$included_object" 2>/dev/null | eval "$grep_exclude_pattern" | rm_dot_slash | eval "$sed_select_hidden_objects_pattern")
                                        else
                                                # When $depth_value is -1
                                                # Reading all objects with "find" after having 
                                                #       - included the results matching $included_object
                                                #       - excluded the objects matching the patterns from:
                                                #               + ${excluded_regex_patterns[@]}
                                                #               + ${grep_esc_excluded_objects[@]}
                                                #       - removed all leading './'
                                                while IFS= read -d $'\n' -r object ; do
                                                        # Prepend a leading character symbolizing the object type followed by its size in B/KB/MB/TB
                                                        object=$(.drive-prepend-object-type-size "$object")

                                                        tmp_objects=("${tmp_objects[@]}" "$object")
                                                done < <(find -L -path "./$included_object" 2>/dev/null | eval "$grep_exclude_pattern" | rm_dot_slash | eval "$sed_select_hidden_objects_pattern")
                                        fi

                                        # Concatenating tmp_objects[] with filtered_objects[] array
                                        tmp_filtered_objects=("${filtered_objects[@]}")
                                        filtered_objects=("${tmp_filtered_objects[@]}" "${tmp_objects[@]}")
                                        unset tmp_objects
                                        unset tmp_filtered_objects
                                fi
                        done

                #-------Handling ${included_regex_patterns[@]}-------------------------------------
                        if [[ ${#included_regex_patterns[@]} -ne 0 ]]; then
                                # Building the 'find' include pattern with:
                                #       - "${included_regex_patterns[@]}"
                                find_include_pattern=$(build-find-include-pattern 'true' 'false')

                                if [[ $depth_value -ge 1 ]]; then
                                        # Reading all objects with "find" while
                                        #       - including the objects matching the patterns from ${included_regex_patterns[@]}
                                        #       - excluding the objects matching the patterns from
                                        #               + ${excluded_regex_patterns[@]}
                                        #               + ${grep_esc_excluded_objects[@]}
                                        #       - removing all leading './'
                                        while IFS= read -d $'\n' -r object ; do
                                                # Prepend a leading character symbolizing the object type followed by its size in B/KB/MB/TB
                                                object=$(.drive-prepend-object-type-size "$object")

                                                tmp_objects=("${tmp_objects[@]}" "$object")
                                        done < <(eval find -L . -maxdepth $depth_value "$find_include_pattern" 2>/dev/null | eval "$grep_exclude_pattern" | rm_dot_slash | eval "$sed_select_hidden_objects_pattern")
                                elif [[ $depth_value -eq 0 ]]; then
                                        # Building the 'grep' include pattern with:
                                        #       - "${included_regex_patterns[@]}"
                                        grep_include_pattern=$(build-grep-include-pattern 'true' 'false' 'local')

                                        # Reading all objects with "ls" after having 
                                        #       - included the objects matching the patterns from ${included_regex_patterns[@]}
                                        #       - excluded the results with patterns from:
                                        #               + ${excluded_regex_patterns[@]}
                                        #               + ${grep_esc_excluded_objects[@]}
                                        if [[ ! -z "$grep_exclude_pattern" ]]; then
                                                while IFS= read -d $'\n' -r object ; do
                                                        # Prepend a leading character symbolizing the object type followed by its size in B/KB/MB/TB
                                                        object=$(.drive-prepend-object-type-size "$object")

                                                        tmp_objects=("${tmp_objects[@]}" "$object")
                                                done < <(ls -A | eval "$grep_include_pattern" | eval "$grep_exclude_pattern" | eval "$sed_select_hidden_objects_pattern")
                                        else
                                                while IFS= read -d $'\n' -r object ; do
                                                        # Prepend a leading character symbolizing the object type followed by its size in B/KB/MB/TB
                                                        object=$(.drive-prepend-object-type-size "$object")

                                                        tmp_objects=("${tmp_objects[@]}" "$object")
                                                done < <(ls -A | eval "$grep_include_pattern" | eval "$sed_select_hidden_objects_pattern")
                                        fi
                                else
                                        # When $depth_value is -1
                                        # Reading all objects with "find" after having 
                                        #       - included the objects matching the patterns from ${included_regex_patterns[@]}
                                        #       - excluded the objects matching the patterns from
                                        #               + ${excluded_regex_patterns[@]}
                                        #               + ${grep_esc_excluded_objects[@]}
                                        while IFS= read -d $'\n' -r object ; do
                                                # Prepend a leading character symbolizing the object type followed by its size in B/KB/MB/TB
                                                object=$(.drive-prepend-object-type-size "$object")

                                                tmp_objects=("${tmp_objects[@]}" "$object")
                                        done < <(eval find -L "$find_include_pattern" 2>/dev/null | eval "$grep_exclude_pattern" | rm_dot_slash | eval "$sed_select_hidden_objects_pattern")
                                fi

                                # Concatenating tmp_objects[] with filtered_objects[] array
                                tmp_filtered_objects=("${filtered_objects[@]}")
                                filtered_objects=("${tmp_filtered_objects[@]}" "${tmp_objects[@]}")
                                unset tmp_objects
                                unset tmp_filtered_objects
                        fi
                        ;;
                select_file)
                        # It has already been filtered
                        filtered_objects=("${included_objects[@]}")
                        ;;
        esac
fi

#----------------Filtering all remote objects listed in complete_objects if necessary--------------
if [[ ($source == 'remote_fs') && ((${#excluded_regex_patterns[@]} -ne 0) || (${#grep_esc_excluded_objects[@]} -ne 0)) ]]; then
        # Building the 'grep' exclude pattern with:
        #       - ${excluded_regex_patterns[@]}
        grep_exclude_regex_pattern=$(build-grep-exclude-pattern 'true' 'false' 'remote')

        # Building the 'grep' exclude pattern with:
        #       - "${grep_esc_excluded_objects[@]}"
        grep_exclude_pattern=$(build-grep-exclude-pattern 'false' 'true' 'remote')

        # For all objects in ${complete_objects[@]}
        for ((complete_objects_index=0; complete_objects_index<${#complete_objects[@]}; complete_objects_index++))
        do
                # Filtering the object names according to .driveignore
                if [[ ! -z "$grep_exclude_regex_pattern" ]]; then
                        filtered_object=$(echo "${complete_objects[complete_objects_index]}" | eval "$grep_exclude_regex_pattern")
                        if [[ -z "$filtered_object" ]]; then
                                unset complete_objects[complete_objects_index]
                                complete_objects=("${complete_objects[@]}")
                                ((complete_objects_index--))
                                continue
                        fi
                fi
                
                # Filtering the object names according to escaped excluded objects from .driveselect
                if [[ ! -z "$grep_exclude_pattern" ]]; then
                        filtered_object=$(echo "${complete_objects[complete_objects_index]}" | eval "$grep_exclude_pattern")
                        if [[ -z "$filtered_object" ]]; then
                                if [[ ("$command" == 'delete') || ("$command" == 'trash') ]]; then
                                        # For both commands, we need to remove later the file's folder from the complete_objects list
                                        object_type=$(echo "${complete_objects[complete_objects_index]}" | extract_object_type)
                                        if [[ $object_type == '-' ]]; then
                                                # Object is a file: getting its path after having removed its leading stats
                                                excluded_object_full_name=$(echo "${complete_objects[complete_objects_index]}" | rm_object_stats)
                                                excluded_object_path=$(dirname "$excluded_object_full_name")

                                                # As long as there is a path
                                                while [[ "$excluded_object_path" != \. ]]
                                                do
                                                        # - Escaping the '/$.*[\]^' characters for later sed searched string
                                                        # - Letters, digits and (){}+?| must not be escaped
                                                        esc_excluded_object_path="$(echo "$excluded_object_path" | escape_sed_bre_search_string)"
                                                        esc_excluded_objects_paths=("${esc_excluded_objects_paths[@]}" "$esc_excluded_object_path")
                                                        # Getting the parent's parent
                                                        excluded_object_path=$(dirname "$excluded_object_path")
                                                done

                                                # Sorting the object names in alphabatical ordser & Removing all duplicate entries
                                                if [[ ${#esc_excluded_objects_paths[@]} -ne 0 ]]; then
                                                        IFS=$'\n' esc_excluded_objects_paths=($(sort -u <<<"${esc_excluded_objects_paths[*]}"))
                                                        unset IFS
                                                fi
                                        fi
                                fi

                                unset complete_objects[complete_objects_index]
                                complete_objects=("${complete_objects[@]}")
                                ((complete_objects_index--))
                        fi
                fi
        done
fi

# Array which contains all objects names from ${complete_objects[@]} filtered with:
#       - ${excluded_regex_patterns[@]}         read from .driveignore
#       - ${grep_esc_excluded_objects[@]}        read from .driveselect and escaped for grep
# Job already done for 'local_fs' and 'select_file' sources
if [[ $source == 'remote_fs' ]]; then
        filtered_objects=("${complete_objects[@]}")
fi

#--------------------------Sorting the object names in alphabatical order--------------------------
#--------------------------Removing all duplicate entries------------------------------------------
# dismissing the first columns describing the object stats
if [[ ${#filtered_objects[@]} -ne 0 ]]; then
        IFS=$'\n' sorted_objects=($(sort -u -t ' ' -k 3 <<<"${filtered_objects[*]}"))
        unset IFS
fi

#--------------------------Removing some unnecessary folders for delete & trash commands-----------
# if necessary from SORTED ${sorted_objects[@]}
if [[ (("$command" == 'delete') || ("$command" == 'trash')) && (${#esc_excluded_objects_paths[@]} -ne 0) ]]; then
        for ((sorted_objects_index=0; sorted_objects_index<${#sorted_objects[@]}; sorted_objects_index++))
        do
                for esc_excluded_object_path in "${esc_excluded_objects_paths[@]}"
                do
                        # Checking whether the object is the excluded folder
                        trimmed_object=$(echo "${sorted_objects[sorted_objects_index]}" | rm_object_type_size_excluded_object_path)
                        if [[ -z "$trimmed_object" ]]; then
                                object_type=$(echo "${sorted_objects[sorted_objects_index]}" | extract_object_type)
                                if [[ $object_type == 'd' ]]; then
                                        # The object is the excluded folder
                                        unset sorted_objects[sorted_objects_index]
                                        sorted_objects=("${sorted_objects[@]}")
                                        ((sorted_objects_index--))
                                        continue 2
                                fi
                        fi
                done
        done
fi

# Array which will contain all objects names from ${sorted_objects[@]} trimmed:
#       - only for delete and trash commands
#       - if a file has been previously filtered, we make sure its folder is removed from the final list
trimmed_objects=("${sorted_objects[@]}")

#--------------------------Returning the listed, filtered, sorted, unduplicated and trimmed objects list
if [[ ${#trimmed_objects[@]} -ne 0 ]]; then
        printf '%s\n' "${trimmed_objects[@]}"
fi