MediaImporter / media-importer.sh
Newer Older
1565 lines | 53.011kb
Bogdan Timofte authored 9 months ago
1
#!/bin/bash
2

            
3
# Standalone Media Importer
4
# Version: 1.0
5
# A comprehensive media file organizer that sorts photos and videos by date
6
# with various organization patterns and timezone handling
7

            
8
VERSION="1.0"
9
SCRIPT_NAME="Standalone Media Importer"
10

            
11
# Default values
Bogdan Timofte authored 9 months ago
12
# Default organization: 'ymd' (single folder per day yyyy-mm-dd). Override with -o/--organization.
13
ORGANIZATION="ymd"
14
FILENAME_MODE="full"  # options: auto, full, orig
Bogdan Timofte authored 9 months ago
15
COLLECT_UNSORTABLE=0
16
SOURCE_PATTERNS=()
17
DESTINATION=""
18
KEEP_ORIGINALS=0
Bogdan Timofte authored 3 weeks ago
19
VERIFY_MODE="size"  # options: size, strict, none
Bogdan Timofte authored 2 weeks ago
20
DATE_SOURCE="auto"  # options: auto, exif, filesystem
21
SYNC_METADATA=0     # when 1, write reconstructed date into destination metadata
Bogdan Timofte authored a week ago
22
QUICKTIME_UTC=1     # when 0, treat QuickTime dates as already local (for cameras that store local time instead of UTC)
Bogdan Timofte authored 2 weeks ago
23
UNATTENDED=0        # when 1, never prompt; destination conflicts get numeric suffixes
Bogdan Timofte authored 9 months ago
24
DRY_RUN=0
25
VERBOSE=0
Bogdan Timofte authored 8 months ago
26
CLEANUP_EMPTY_DIRS=1
Bogdan Timofte authored 2 weeks ago
27
CLEANUP_MEDIA_SIDECARS=1  # when 1, delete device sidecars (GoPro THM/LRV, Garmin GLV) after successful import
Bogdan Timofte authored 2 weeks ago
28
CONFLICT_APPLY_ALL=""  # suffix|skip after an interactive "all similar" choice
29
RESOLVED_DESTINATION_PATH=""
30
RESERVED_DESTINATION_PATHS=()
Bogdan Timofte authored 9 months ago
31

            
32
# Counters and statistics
33
TOTAL_FILES=0
34
PROCESSED_FILES=0
35
SKIPPED_FILES=0
36
ERROR_FILES=0
37
TOTAL_SIZE=0
38
PROCESSED_SIZE=0
39
START_TIME=$(date +%s)
Bogdan Timofte authored 2 weeks ago
40
CURRENT_FILE_INDEX=0
Bogdan Timofte authored 9 months ago
41

            
42
# Colors for output
43
RED='\033[0;31m'
44
GREEN='\033[0;32m'
45
YELLOW='\033[1;33m'
Bogdan Timofte authored 9 months ago
46
# BLUE is used for informational/verbose messages. Dark terminal themes may render the
47
# default blue hard to read, so use a brighter cyan by default and allow users to
48
# override via the VERBOSE_COLOR environment variable (must be a terminal escape seq).
49
if [[ -n "${VERBOSE_COLOR:-}" ]]; then
50
    BLUE="$VERBOSE_COLOR"
51
else
52
    BLUE=$'\033[1;36m'  # bright cyan
53
fi
Bogdan Timofte authored 9 months ago
54
NC='\033[0m' # No Color
55

            
56
# Function to print colored output
57
print_color() {
58
    local color="$1"
59
    local message="$2"
60
    echo -e "${color}${message}${NC}"
61
}
62

            
63
# Function to log messages with timestamp
64
log_message() {
65
    local message="$1"
66
    local level="${2:-INFO}"
67
    local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
68

            
69
    case "$level" in
70
        "ERROR")
71
            print_color "$RED" "[$timestamp] ERROR: $message" >&2
72
            ;;
73
        "WARNING")
74
            print_color "$YELLOW" "[$timestamp] WARNING: $message"
75
            ;;
76
        "SUCCESS")
77
            print_color "$GREEN" "[$timestamp] SUCCESS: $message"
78
            ;;
79
        "INFO")
80
            if [[ $VERBOSE -eq 1 ]]; then
81
                print_color "$BLUE" "[$timestamp] INFO: $message"
82
            fi
83
            ;;
84
        *)
85
            echo "[$timestamp] $message"
86
            ;;
87
    esac
88
}
89

            
90
# Function to display help
91
show_help() {
Bogdan Timofte authored 9 months ago
92
        cat << EOF
93
    $SCRIPT_NAME v$VERSION
Bogdan Timofte authored 9 months ago
94

            
Bogdan Timofte authored 9 months ago
95
Usage:
Bogdan Timofte authored 9 months ago
96
    $0 [OPTIONS]
97

            
Bogdan Timofte authored 9 months ago
98
What it does:
99
    Sorts photos and videos into dated folders (by year/month/day/hour) and
100
    generates filenames from the file's creation timestamp or preserves the
101
    original name.
102

            
103
Options:
104
    -o, --organization PATTERN   y|m|d|h|ym|ymd   (default: ymd)
105
    -F, --filename-mode MODE     auto|full|orig    (default: full)
106
    -s, --source PATH            File or directory to process (repeatable). Default: cwd
107
    -d, --destination PATH       Destination folder. Required when multiple -s are given.
Bogdan Timofte authored 8 months ago
108
    -k, --keep-originals         Copy files instead of moving
Bogdan Timofte authored 3 weeks ago
109
    --verify-mode MODE           size|strict|none (default: size)
Bogdan Timofte authored 2 weeks ago
110
    --date-source SOURCE         auto|exif|filesystem (default: auto)
111
    --sync-metadata              Write chosen date into destination metadata (automatic for GoPro filesystem dates)
Bogdan Timofte authored a week ago
112
    --no-quicktime-utc           Treat QuickTime dates as already local time (for cameras that store local time instead of UTC)
Bogdan Timofte authored 2 weeks ago
113
    --unattended                 Never prompt; resolve destination conflicts with numeric suffixes
Bogdan Timofte authored 9 months ago
114
    --collect-unsortable         Put files without dates into DEST/unsortable
Bogdan Timofte authored 2 weeks ago
115
    --keep-sidecars              Keep device sidecar files (default: delete after import)
Bogdan Timofte authored 8 months ago
116
    --keep-empty-dirs            Keep empty directories after processing
117
    --dry-run                    Show actions without changing files
Bogdan Timofte authored 9 months ago
118
    -v, --verbose                Verbose output
119
    -h, --help                   Show this help
120
    --version                    Show version
121

            
122
Examples:
123
    $0 -s /path/to/photos
124
    $0 -s /path/to/DCIM -d /mnt/sorted --dry-run
125

            
126
Dependencies:
127
    exiftool (required). mediainfo and file are optional.
Bogdan Timofte authored 9 months ago
128
EOF
129
}
130

            
Bogdan Timofte authored 9 months ago
131
# Central media extensions list (used by find functions)
132
MEDIA_EXTENSIONS=("*.jpg" "*.jpeg" "*.png" "*.tiff" "*.tif" "*.cr2" "*.nef" "*.arw" "*.dng" "*.raw" "*.mp4" "*.mov" "*.avi" "*.mts" "*.m2ts" "*.mkv" "*.wmv" "*.3gp" "*.m4v")
133

            
Bogdan Timofte authored 9 months ago
134
# Function to show version
135
show_version() {
136
    echo "$SCRIPT_NAME v$VERSION"
137
    echo "A comprehensive media file organizer with timezone support"
138
}
139

            
140
# Function to check dependencies
141
check_dependencies() {
142
    local missing_deps=()
143

            
144
    # Check for required dependencies
145
    if ! command -v exiftool &> /dev/null; then
146
        missing_deps+=("exiftool")
147
    fi
148

            
149
    # Check for optional dependencies
150
    local optional_missing=()
151
    if ! command -v mediainfo &> /dev/null; then
152
        optional_missing+=("mediainfo")
153
    fi
154

            
155
    if ! command -v file &> /dev/null; then
156
        optional_missing+=("file")
157
    fi
158

            
159
    if [[ ${#missing_deps[@]} -gt 0 ]]; then
160
        print_color "$RED" "ERROR: Missing required dependencies:"
161
        for dep in "${missing_deps[@]}"; do
162
            echo "  - $dep"
163
        done
164
        echo ""
165
        echo "Installation instructions:"
166
        echo "  macOS: brew install exiftool"
167
        echo "  Ubuntu/Debian: sudo apt-get install libimage-exiftool-perl"
168
        echo "  CentOS/RHEL: sudo yum install perl-Image-ExifTool"
169
        echo "  Arch: sudo pacman -S perl-image-exiftool"
170
        exit 1
171
    fi
172

            
173
    if [[ ${#optional_missing[@]} -gt 0 && $VERBOSE -eq 1 ]]; then
174
        log_message "Optional dependencies not found (functionality may be limited): ${optional_missing[*]}" "WARNING"
175
    fi
176

            
177
    log_message "All required dependencies found" "SUCCESS"
178
}
179

            
Bogdan Timofte authored 9 months ago
180
# Determine filesystem/device ID for a path (portable between Linux and macOS)
181
get_dev() {
182
    local path="$1"
183
    if [[ -z "$path" ]]; then
184
        path="."
185
    fi
186

            
187
    # Prefer GNU stat if available
188
    if stat --version >/dev/null 2>&1; then
189
        stat -c %d "$path" 2>/dev/null || stat -c %i "$path" 2>/dev/null || echo ""
190
    else
191
        # BSD/macOS stat
192
        stat -f %d "$path" 2>/dev/null || stat -f %i "$path" 2>/dev/null || echo ""
193
    fi
194
}
195

            
196
# Function to get file size in bytes (portable between Linux and macOS)
Bogdan Timofte authored 9 months ago
197
get_file_size() {
198
    local file="$1"
199
    if [[ -f "$file" ]]; then
Bogdan Timofte authored 9 months ago
200
        # Try GNU stat
201
        if stat -c%s "$file" >/dev/null 2>&1; then
Bogdan Timofte authored 9 months ago
202
            stat -c%s "$file" 2>/dev/null || stat -f%z "$file" 2>/dev/null
Bogdan Timofte authored 9 months ago
203
        elif stat -f%z "$file" >/dev/null 2>&1; then
204
            stat -f%z "$file" 2>/dev/null
Bogdan Timofte authored 9 months ago
205
        else
206
            # Fallback to ls
Bogdan Timofte authored 9 months ago
207
            ls -ln "$file" | awk '{print $5}'
Bogdan Timofte authored 9 months ago
208
        fi
209
    else
210
        echo "0"
211
    fi
212
}
213

            
Bogdan Timofte authored 9 months ago
214
# Safe move/copy helpers: filter out benign "set flags (was: ...): Operation not supported"
215
# which appears when moving files onto filesystems that don't support BSD file flags
216
# (macOS mv may try to preserve flags and print this warning while still succeeding).
217
safe_mv() {
218
    local src="$1" dst="$2"
Bogdan Timofte authored 2 weeks ago
219
    if [[ -e "$dst" ]]; then
220
        log_message "Refusing to overwrite existing destination: $dst" "ERROR"
221
        return 1
222
    fi
Bogdan Timofte authored 9 months ago
223
    # Redirect stderr through a filter that removes the known benign message
224
    mv "$src" "$dst" 2> >(grep -v "set flags (was:" >&2)
225
    return $?
226
}
227

            
Bogdan Timofte authored 2 weeks ago
228
safe_cp() {
229
    local src="$1" dst="$2"
230
    if [[ -e "$dst" ]]; then
231
        log_message "Refusing to overwrite existing destination: $dst" "ERROR"
232
        return 1
233
    fi
234
    cp -p "$src" "$dst" 2> >(grep -v "set flags (was:" >&2)
235
    return $?
236
}
237

            
238
ensure_unique_destination_path() {
239
    local desired_path="$1"
240

            
241
    if [[ -z "$desired_path" ]]; then
242
        return 1
243
    fi
244

            
245
    if ! destination_path_unavailable "$desired_path"; then
246
        echo "$desired_path"
247
        return 0
248
    fi
249

            
250
    local dir_path filename ext stem candidate
251
    dir_path=$(dirname "$desired_path")
252
    filename=$(basename "$desired_path")
253

            
254
    if [[ "$filename" == *.* ]]; then
255
        ext="${filename##*.}"
256
        stem="${filename%.*}"
257
    else
258
        ext=""
259
        stem="$filename"
260
    fi
261

            
262
    local i
263
    i=1
264
    while [[ $i -le 9999 ]]; do
265
        if [[ -n "$ext" ]]; then
266
            candidate="$dir_path/${stem}_${i}.${ext}"
267
        else
268
            candidate="$dir_path/${stem}_${i}"
269
        fi
270
        if ! destination_path_unavailable "$candidate"; then
271
            echo "$candidate"
272
            return 0
273
        fi
274
        i=$((i + 1))
275
    done
276

            
277
    return 1
278
}
279

            
Bogdan Timofte authored 2 weeks ago
280
destination_path_reserved() {
281
    local candidate="$1"
282
    local reserved_path
283
    for reserved_path in "${RESERVED_DESTINATION_PATHS[@]}"; do
284
        if [[ "$reserved_path" == "$candidate" ]]; then
285
            return 0
286
        fi
287
    done
288
    return 1
289
}
Bogdan Timofte authored 9 months ago
290

            
Bogdan Timofte authored 2 weeks ago
291
destination_path_unavailable() {
292
    local candidate="$1"
293
    [[ -e "$candidate" ]] || destination_path_reserved "$candidate"
294
}
295

            
296
reserve_destination_path() {
297
    local candidate="$1"
298
    if [[ -n "$candidate" ]] && ! destination_path_reserved "$candidate"; then
299
        RESERVED_DESTINATION_PATHS+=("$candidate")
300
    fi
301
}
302

            
303
prompt_destination_conflict_choice() {
304
    local source_file="$1"
305
    local desired_path="$2"
306
    local choice
307

            
308
    if [[ ! -r /dev/tty || ! -w /dev/tty ]]; then
309
        return 1
310
    fi
311

            
312
    {
313
        print_color "$YELLOW" "Destination already exists:"
314
        echo "  Source:      $source_file"
315
        echo "  Destination: $desired_path"
316
        echo ""
317
        echo "Choose conflict action:"
318
        echo "  [s] suffix once"
319
        echo "  [S] suffix for all similar conflicts"
320
        echo "  [k] skip once"
321
        echo "  [K] skip all similar conflicts"
322
        echo "  [a] abort import"
323
    } > /dev/tty
324

            
325
    while true; do
326
        printf "Action [s/S/k/K/a]: " > /dev/tty
327
        IFS= read -r choice < /dev/tty || return 1
328
        case "$choice" in
329
            s|"")
330
                echo "suffix"
331
                return 0
332
                ;;
333
            S)
334
                echo "suffix_all"
335
                return 0
336
                ;;
337
            k)
338
                echo "skip"
339
                return 0
340
                ;;
341
            K)
342
                echo "skip_all"
343
                return 0
344
                ;;
345
            a|A)
346
                echo "abort"
347
                return 0
348
                ;;
349
            *)
350
                print_color "$YELLOW" "Please choose s, S, k, K, or a." > /dev/tty
351
                ;;
352
        esac
353
    done
354
}
355

            
356
resolve_destination_conflict() {
357
    local desired_path="$1"
358
    local source_file="$2"
359
    local resolved_path choice
360
    RESOLVED_DESTINATION_PATH=""
361

            
362
    if [[ -z "$desired_path" ]]; then
363
        return 1
364
    fi
365

            
366
    if ! destination_path_unavailable "$desired_path"; then
367
        RESOLVED_DESTINATION_PATH="$desired_path"
368
        reserve_destination_path "$RESOLVED_DESTINATION_PATH"
369
        return 0
370
    fi
371

            
372
    if [[ "$CONFLICT_APPLY_ALL" == "skip" ]]; then
373
        return 3
374
    fi
375

            
376
    if [[ "$CONFLICT_APPLY_ALL" == "suffix" || $UNATTENDED -eq 1 ]]; then
377
        resolved_path=$(ensure_unique_destination_path "$desired_path")
378
        if [[ -z "$resolved_path" ]]; then
379
            return 1
380
        fi
381
        RESOLVED_DESTINATION_PATH="$resolved_path"
382
        reserve_destination_path "$RESOLVED_DESTINATION_PATH"
383
        return 0
384
    fi
385

            
386
    choice=$(prompt_destination_conflict_choice "$source_file" "$desired_path")
387
    if [[ $? -ne 0 || -z "$choice" ]]; then
388
        log_message "Cannot prompt for destination conflict; using unattended numeric suffix mode" "WARNING"
389
        resolved_path=$(ensure_unique_destination_path "$desired_path")
390
        if [[ -z "$resolved_path" ]]; then
391
            return 1
392
        fi
393
        RESOLVED_DESTINATION_PATH="$resolved_path"
394
        reserve_destination_path "$RESOLVED_DESTINATION_PATH"
395
        return 0
396
    fi
397

            
398
    case "$choice" in
399
        suffix)
400
            resolved_path=$(ensure_unique_destination_path "$desired_path")
401
            ;;
402
        suffix_all)
403
            CONFLICT_APPLY_ALL="suffix"
404
            resolved_path=$(ensure_unique_destination_path "$desired_path")
405
            ;;
406
        skip)
407
            return 3
408
            ;;
409
        skip_all)
410
            CONFLICT_APPLY_ALL="skip"
411
            return 3
412
            ;;
413
        abort)
414
            return 4
415
            ;;
416
        *)
417
            return 1
418
            ;;
419
    esac
420

            
421
    if [[ -z "$resolved_path" ]]; then
422
        return 1
423
    fi
424

            
425
    RESOLVED_DESTINATION_PATH="$resolved_path"
426
    reserve_destination_path "$RESOLVED_DESTINATION_PATH"
427
    return 0
428
}
429

            
430
extract_filesystem_date() {
431
    # Returns yyyy-mm-dd hh:mm:ss based on filesystem mtime.
432
    # We intentionally use mtime (not birthtime) because birthtime isn't preserved by copies
433
    # across filesystems, while mtime can be preserved via `cp -p`.
434
    local file="$1"
435
    if [[ ! -e "$file" ]]; then
436
        return 2
437
    fi
438

            
439
    local epoch=""
440

            
441
    if [[ "$OSTYPE" == "darwin"* ]]; then
442
        epoch=$(stat -f %m "$file" 2>/dev/null || echo "")
443
        [[ -n "$epoch" ]] || return 2
444
        date -j -r "$epoch" "+%Y-%m-%d %H:%M:%S" 2>/dev/null || return 2
445
        return 0
446
    else
447
        epoch=$(stat -c %Y "$file" 2>/dev/null || echo "")
448
        [[ -n "$epoch" ]] || return 2
449
        date -d "@$epoch" "+%Y-%m-%d %H:%M:%S" 2>/dev/null || return 2
450
        return 0
451
    fi
452
}
453

            
454
filesystem_date_reference() {
455
    local file="$1"
456
    local dir base stem ext sidecar_ext sidecar
457
    dir=$(dirname "$file")
458
    base=$(basename "$file")
459
    stem="${base%.*}"
460
    ext="${base##*.}"
461

            
462
    if [[ "$ext" =~ ^([Mm][Pp]4)$ ]]; then
463
        for sidecar_ext in THM thm LRV lrv; do
464
            sidecar="$dir/$stem.$sidecar_ext"
465
            if [[ -f "$sidecar" ]]; then
466
                echo "$sidecar"
467
                return 0
468
            fi
469
        done
470
    fi
471

            
472
    echo "$file"
473
}
474

            
475
is_gopro_media_file() {
476
    local filename
477
    filename=$(basename "$1")
478
    [[ "$filename" =~ ^G[HPX][0-9]{6}\.[Mm][Pp]4$ ]]
479
}
480

            
Bogdan Timofte authored 2 weeks ago
481
is_garmin_media_file() {
482
    local filename
483
    filename=$(basename "$1")
484
    [[ "$filename" =~ ^VIRB[0-9]{4}\.[Mm][Pp]4$ ]]
485
}
486

            
487
get_device_sidecar_extensions() {
488
    local file="$1"
489
    local filename
490
    filename=$(basename "$file")
491

            
492
    if is_gopro_media_file "$file"; then
493
        echo "THM thm LRV lrv"
494
    elif is_garmin_media_file "$file"; then
495
        echo "GLV glv"
496
    fi
497
}
498

            
Bogdan Timofte authored 2 weeks ago
499
should_prefer_gopro_filesystem_date() {
500
    local file="$1"
501

            
502
    is_gopro_media_file "$file"
503
}
504

            
505
filesystem_date_source_label() {
506
    local file="$1"
507
    local reference="$2"
508

            
509
    if is_gopro_media_file "$file"; then
510
        echo "Filesystem:$(basename "$reference")"
511
    elif [[ "$reference" != "$file" ]]; then
512
        echo "Filesystem:$(basename "$reference")"
513
    else
514
        echo "Filesystem"
515
    fi
516
}
517

            
518
date_to_exiftool_format() {
519
    # yyyy-mm-dd hh:mm:ss -> yyyy:mm:dd hh:mm:ss
520
    local s="$1"
521
    if [[ "$s" =~ ^([0-9]{4})-([0-9]{2})-([0-9]{2})[[:space:]]+([0-9]{2}):([0-9]{2}):([0-9]{2})$ ]]; then
522
        echo "${BASH_REMATCH[1]}:${BASH_REMATCH[2]}:${BASH_REMATCH[3]} ${BASH_REMATCH[4]}:${BASH_REMATCH[5]}:${BASH_REMATCH[6]}"
523
        return 0
524
    fi
525
    return 1
526
}
527

            
528
sync_destination_metadata_to_date() {
529
    local file="$1"
530
    local date_str="$2" # yyyy-mm-dd hh:mm:ss
531

            
532
    local exif_dt
533
    exif_dt=$(date_to_exiftool_format "$date_str") || return 1
534

            
535
    exiftool -overwrite_original \
536
        "-CreateDate=$exif_dt" \
537
        "-DateTimeOriginal=$exif_dt" \
538
        "-DateTime=$exif_dt" \
539
        "-ModifyDate=$exif_dt" \
540
        "-MediaCreateDate=$exif_dt" \
541
        "-TrackCreateDate=$exif_dt" \
542
        "-QuickTime:CreateDate=$exif_dt" \
543
        "-QuickTime:ModifyDate=$exif_dt" \
544
        "$file" >/dev/null 2>&1
545
    return 0
546
}
547

            
548
verify_synced_metadata_date() {
549
    local file="$1"
550
    local expected_date="$2"
551

            
552
    local metadata_date
553
    metadata_date=$(exiftool -api QuickTimeUTC=0 -s -s -s -QuickTime:CreateDate "$file" 2>/dev/null | head -1)
554
    if [[ -z "$metadata_date" ]]; then
555
        metadata_date=$(exiftool -api QuickTimeUTC=0 -s -s -s -CreateDate "$file" 2>/dev/null | head -1)
556
    fi
557

            
558
    if [[ "$metadata_date" =~ ^([0-9]{4}):([0-9]{2}):([0-9]{2})[[:space:]]+([0-9]{2}):([0-9]{2}):([0-9]{2}) ]]; then
559
        metadata_date="${BASH_REMATCH[1]}-${BASH_REMATCH[2]}-${BASH_REMATCH[3]} ${BASH_REMATCH[4]}:${BASH_REMATCH[5]}:${BASH_REMATCH[6]}"
560
    fi
561

            
562
    if [[ "$metadata_date" != "$expected_date" ]]; then
563
        log_message "Destination metadata sync mismatch: expected $expected_date, got ${metadata_date:-none} for $file" "ERROR"
564
        return 1
565
    fi
566

            
567
    return 0
568
}
569

            
570
should_sync_imported_metadata() {
571
    local original_filename="$1"
572
    local date_source="$2"
573

            
574
    if [[ $SYNC_METADATA -eq 1 ]]; then
575
        return 0
576
    fi
577

            
578
    if [[ "$date_source" == Filesystem* ]] && is_gopro_media_file "$original_filename"; then
579
        return 0
580
    fi
581

            
582
    return 1
Bogdan Timofte authored 9 months ago
583
}
584

            
Bogdan Timofte authored 3 weeks ago
585
verify_copied_file() {
586
    local src="$1"
587
    local dst="$2"
588
    local expected_date="$3"
589

            
590
    if [[ ! -f "$dst" ]]; then
591
        log_message "Verified copy missing at destination: $dst" "ERROR"
592
        return 1
593
    fi
594

            
595
    local src_size dst_size
596
    src_size=$(get_file_size "$src")
597
    dst_size=$(get_file_size "$dst")
598
    if [[ "$src_size" != "$dst_size" ]]; then
599
        log_message "Size mismatch after copy: $src ($src_size) != $dst ($dst_size)" "ERROR"
600
        return 1
601
    fi
602

            
603
    if [[ "$VERIFY_MODE" == "strict" ]]; then
604
        if ! cmp -s "$src" "$dst"; then
605
            log_message "Content mismatch after copy: $src -> $dst" "ERROR"
606
            return 1
607
        fi
608
    elif [[ "$VERIFY_MODE" == "none" ]]; then
609
        return 0
610
    fi
611

            
612
    if [[ -n "$expected_date" ]]; then
613
        local destination_date_info
614
        destination_date_info=$(extract_file_date "$dst")
615
        local extract_status=$?
616
        if [[ $extract_status -ne 0 || -z "$destination_date_info" ]]; then
617
            log_message "Destination metadata validation failed: $dst" "ERROR"
618
            return 1
619
        fi
620

            
621
        local destination_date="${destination_date_info%|*}"
622
        if [[ "$destination_date" != "$expected_date" ]]; then
623
            log_message "Destination metadata mismatch: expected $expected_date, got $destination_date for $dst" "ERROR"
624
            return 1
625
        fi
626
    fi
627

            
628
    return 0
629
}
630

            
631
remove_source_file() {
632
    local src="$1"
633
    rm -f "$src"
634
}
635

            
636
copy_with_verification() {
637
    local src="$1"
638
    local dst="$2"
639
    local expected_date="$3"
640

            
Bogdan Timofte authored 2 weeks ago
641
    if [[ -e "$dst" ]]; then
642
        log_message "Refusing to overwrite existing destination: $dst" "ERROR"
Bogdan Timofte authored 3 weeks ago
643
        return 1
644
    fi
645

            
Bogdan Timofte authored 2 weeks ago
646
    local dst_dir tmp
647
    dst_dir=$(dirname "$dst")
648
    tmp=$(mktemp "$dst_dir/.media-importer.$(basename "$dst").tmp.XXXXXX") || return 1
649
    rm -f "$tmp"
650

            
651
    if ! safe_cp "$src" "$tmp"; then
652
        rm -f "$tmp"
653
        return 1
654
    fi
655

            
656
    if ! verify_copied_file "$src" "$tmp" "$expected_date"; then
657
        rm -f "$tmp"
658
        return 1
659
    fi
660

            
661
    if [[ -e "$dst" ]]; then
662
        log_message "Destination appeared during copy, refusing to overwrite: $dst" "ERROR"
663
        rm -f "$tmp"
664
        return 1
665
    fi
666

            
667
    if ! safe_mv "$tmp" "$dst"; then
668
        rm -f "$tmp"
669
        return 1
670
    fi
671

            
672
    if [[ ! -f "$dst" ]]; then
673
        log_message "Copied file missing after final move: $dst" "ERROR"
Bogdan Timofte authored 3 weeks ago
674
        return 1
675
    fi
676

            
677
    return 0
678
}
679

            
680
verified_move_file() {
681
    local src="$1"
682
    local dst="$2"
683
    local expected_date="$3"
684

            
685
    if ! copy_with_verification "$src" "$dst" "$expected_date"; then
686
        return 1
687
    fi
688

            
689
    if ! remove_source_file "$src"; then
690
        log_message "Copied and verified destination, but failed to remove source: $src" "ERROR"
691
        return 1
692
    fi
693

            
694
    return 0
695
}
696

            
Bogdan Timofte authored 9 months ago
697
# Function to format file size
698
format_size() {
699
    local size=$1
700
    if (( size < 1024 )); then
701
        echo "${size}B"
702
    elif (( size < 1048576 )); then
703
        echo "$(( size / 1024 ))KB"
704
    elif (( size < 1073741824 )); then
705
        echo "$(( size / 1048576 ))MB"
706
    else
707
        echo "$(( size / 1073741824 ))GB"
708
    fi
709
}
710

            
Bogdan Timofte authored 2 weeks ago
711
format_duration() {
712
    local total_seconds=$1
713
    local hours=$((total_seconds / 3600))
714
    local minutes=$(((total_seconds % 3600) / 60))
715
    local seconds=$((total_seconds % 60))
716

            
717
    if (( hours > 0 )); then
718
        printf "%dh %02dm %02ds" "$hours" "$minutes" "$seconds"
719
    elif (( minutes > 0 )); then
720
        printf "%dm %02ds" "$minutes" "$seconds"
721
    else
722
        printf "%ds" "$seconds"
723
    fi
724
}
725

            
Bogdan Timofte authored 2 weeks ago
726
format_data_rate() {
727
    local bytes_count="$1"
728
    local elapsed_seconds="$2"
Bogdan Timofte authored 2 weeks ago
729

            
Bogdan Timofte authored 2 weeks ago
730
    awk -v bytes="$bytes_count" -v seconds="$elapsed_seconds" '
Bogdan Timofte authored 2 weeks ago
731
        BEGIN {
Bogdan Timofte authored 2 weeks ago
732
            if (seconds <= 0 || bytes <= 0) {
Bogdan Timofte authored 2 weeks ago
733
                exit
734
            }
735

            
736
            mb_per_second = bytes / seconds / 1048576
Bogdan Timofte authored 2 weeks ago
737
            printf "%.2f MB/sec", mb_per_second
Bogdan Timofte authored 2 weeks ago
738
        }
739
    '
740
}
741

            
Bogdan Timofte authored 2 weeks ago
742
report_line() {
743
    local label="$1"
744
    local value="$2"
745
    printf "  %-24s %s\n" "$label" "$value"
746
}
747

            
Bogdan Timofte authored 9 months ago
748
# Function to extract date from file
749
extract_file_date() {
750
    local file="$1"
751
    local create_date=""
752
    local date_source=""
753
    local exif_found=0
Bogdan Timofte authored 2 weeks ago
754

            
755
    # Filesystem authoritative mode, and GoPro media in auto mode.
756
    # GoPro fallback order is THM, LRV, then the MP4 filesystem timestamp.
757
    if [[ "$DATE_SOURCE" == "filesystem" ]] || { [[ "$DATE_SOURCE" == "auto" ]] && should_prefer_gopro_filesystem_date "$file"; }; then
758
        local filesystem_reference
759
        filesystem_reference=$(filesystem_date_reference "$file")
760
        create_date=$(extract_filesystem_date "$filesystem_reference") || return 2
761
        echo "$create_date|$(filesystem_date_source_label "$file" "$filesystem_reference")"
762
        return 0
763
    fi
764

            
Bogdan Timofte authored 9 months ago
765
    # Try to get creation date from EXIF data
766
    local exif_output=$(exiftool -G1 -s -CreateDate -DateTimeOriginal -DateTime "$file" 2>/dev/null)
767
    if [[ -n "$exif_output" ]]; then
768
        # Parse the exiftool output to find the best date
769
        while IFS= read -r line; do
770
            if [[ "$line" =~ ^\[([^\]]+)\][[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$ ]]; then
771
                local group="${BASH_REMATCH[1]}"
772
                local tag="${BASH_REMATCH[2]}"
773
                local value="${BASH_REMATCH[3]}"
774
                # Trim spaces from tag name
775
                tag=$(echo "$tag" | sed 's/[[:space:]]*$//')
776
                # Prefer DateTimeOriginal, then CreateDate, then DateTime
777
                if [[ "$tag" == "DateTimeOriginal" && -z "$create_date" ]]; then
778
                    create_date="$value"
779
                    date_source="$group:$tag"
780
                    exif_found=1
781
                elif [[ "$tag" == "CreateDate" && "$date_source" != *"DateTimeOriginal"* ]]; then
782
                    create_date="$value"
783
                    date_source="$group:$tag"
784
                    exif_found=1
785
                elif [[ "$tag" == "DateTime" && -z "$create_date" ]]; then
786
                    create_date="$value"
787
                    date_source="$group:$tag"
788
                    exif_found=1
789
                fi
790
            fi
791
        done <<< "$exif_output"
792
    fi
793
    # If no EXIF date found, try mediainfo for video files
794
    if [[ -z "$create_date" ]] && command -v mediainfo &> /dev/null; then
795
        local media_date=$(mediainfo --Inform="General;%Recorded_Date%" "$file" 2>/dev/null)
796
        if [[ -n "$media_date" && "$media_date" != "0000-00-00 00:00:00" ]]; then
797
            create_date="$media_date"
798
            date_source="MediaInfo:Recorded_Date"
799
        fi
800
    fi
Bogdan Timofte authored 2 weeks ago
801

            
802
    # In auto mode, if metadata is missing/unreliable, fall back to filesystem timestamps
803
    if [[ -z "$create_date" && "$DATE_SOURCE" == "auto" ]]; then
804
        local filesystem_reference
805
        filesystem_reference=$(filesystem_date_reference "$file")
806
        create_date=$(extract_filesystem_date "$filesystem_reference") || return 2
807
        date_source=$(filesystem_date_source_label "$file" "$filesystem_reference")
808
    fi
809

            
Bogdan Timofte authored 9 months ago
810
    # If no EXIF or mediainfo date found, return failure
811
    if [[ -z "$create_date" ]]; then
812
        return 2  # No date metadata found
813
    fi
814

            
815
    # Convert EXIF format (YYYY:MM:DD HH:MM:SS) to standard format
816
    # Always output as yyyy-mm-dd hh:mm:ss (pad single digits)
817
    if [[ "$create_date" =~ ^([0-9]{4}):([0-9]{1,2}):([0-9]{1,2})[[:space:]]*([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})$ ]]; then
818
        year="${BASH_REMATCH[1]}"
819
        month=$(printf "%02d" "$((10#${BASH_REMATCH[2]}))")
820
        day=$(printf "%02d" "$((10#${BASH_REMATCH[3]}))")
821
        hour=$(printf "%02d" "$((10#${BASH_REMATCH[4]}))")
822
        minute=$(printf "%02d" "$((10#${BASH_REMATCH[5]}))")
823
        second=$(printf "%02d" "$((10#${BASH_REMATCH[6]}))")
824
        create_date="$year-$month-$day $hour:$minute:$second"
825
    else
826
        # Try to convert yyyy-mm-dd hh:mm:ss (already correct)
827
        if [[ "$create_date" =~ ^([0-9]{4})-([0-9]{2})-([0-9]{2})[[:space:]]*([0-9]{2}):([0-9]{2}):([0-9]{2})$ ]]; then
828
            # Already correct
829
            :
830
        else
831
            print_color "$RED" "Error: Cannot parse date '$create_date'" >&2
832
            return 2
833
        fi
834
    fi
835

            
Bogdan Timofte authored a week ago
836
    # For QuickTime files, the CreateDate is in UTC and needs conversion to local time.
837
    # Skip this conversion with --no-quicktime-utc for cameras that store local time in the UTC field
838
    # (common on some GoPro models/firmware with incorrect timezone settings).
839
    if [[ "$date_source" == *"QuickTime"* && $QUICKTIME_UTC -eq 1 ]]; then
Bogdan Timofte authored 9 months ago
840
        # Convert UTC time to local time
841
        if [[ "$OSTYPE" == "darwin"* ]]; then
842
            # On macOS, use TZ=UTC to interpret the input time as UTC
843
            local utc_timestamp=$(TZ=UTC date -j -f "%Y-%m-%d %H:%M:%S" "$create_date" "+%s" 2>/dev/null)
844
            if [[ -n "$utc_timestamp" ]]; then
845
                create_date=$(date -j -r "$utc_timestamp" "+%Y-%m-%d %H:%M:%S" 2>/dev/null)
846
                date_source="$date_source (converted from UTC)"
847
            fi
848
        else
849
            local utc_timestamp=$(date -d "$create_date UTC" "+%s" 2>/dev/null)
850
            if [[ -n "$utc_timestamp" ]]; then
851
                create_date=$(date -d "@$utc_timestamp" "+%Y-%m-%d %H:%M:%S" 2>/dev/null)
852
                date_source="$date_source (converted from UTC)"
853
            fi
854
        fi
855
    fi
856

            
857
    echo "$create_date|$date_source"
858
    return 0
859
}
860

            
861
# Function to generate destination path based on organization pattern
862
generate_destination_path() {
863
    local date_str="$1"
864
    local original_filename="$2"
865
    local base_destination="$3"
866

            
867
    # Extract date components - handle both GNU and BSD date
868
    local year month day hour minute second
869
    if [[ "$OSTYPE" == "darwin"* ]]; then
870
        # macOS (BSD date) - use more robust regex (allow single-digit month/day, tolerate extra spaces)
871
        if [[ "$date_str" =~ ^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})[[:space:]]*([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})$ ]]; then
872
            year="${BASH_REMATCH[1]}"
873
            month=$(printf "%02d" "$((10#${BASH_REMATCH[2]}))")
874
            day=$(printf "%02d" "$((10#${BASH_REMATCH[3]}))")
875
            hour=$(printf "%02d" "$((10#${BASH_REMATCH[4]}))")
876
            minute=$(printf "%02d" "$((10#${BASH_REMATCH[5]}))")
877
            second=$(printf "%02d" "$((10#${BASH_REMATCH[6]}))")
878
        else
879
            return 1
880
        fi
881
    else
882
        # Linux (GNU date)
883
        year=$(date -d "$date_str" "+%Y" 2>/dev/null)
884
        month=$(date -d "$date_str" "+%m" 2>/dev/null)
885
        day=$(date -d "$date_str" "+%d" 2>/dev/null)
886
        hour=$(date -d "$date_str" "+%H" 2>/dev/null)
887
        minute=$(date -d "$date_str" "+%M" 2>/dev/null)
888
        second=$(date -d "$date_str" "+%S" 2>/dev/null)
889
    fi
890

            
891
    if [[ -z "$year" || -z "$month" || -z "$day" ]]; then
892
        return 1
893
    fi
894

            
895
    # Get file extension
896
    local extension="${original_filename##*.}"
897
    local lowercase_ext=$(echo "$extension" | tr '[:upper:]' '[:lower:]')
898

            
899
    # Generate path and filename based on organization pattern
900
    local dir_path=""
901
    local filename=""
Bogdan Timofte authored 9 months ago
902

            
903
    # If no organization specified, use flat destination (base) and choose filename per mode
904
    if [[ -z "$ORGANIZATION" ]]; then
Bogdan Timofte authored 9 months ago
905
        dir_path="$base_destination"
Bogdan Timofte authored 9 months ago
906
        if [[ "$FILENAME_MODE" == "orig" ]]; then
907
            filename="$original_filename"
908
        else
909
            # full or auto both map to full date for flat layout
910
            filename="${year}-${month}-${day}_${hour}-${minute}-${second}.${lowercase_ext}"
911
        fi
912
        echo "$dir_path/$filename"
913
        return 0
914
    fi
915

            
916
    case "$ORGANIZATION" in
Bogdan Timofte authored 9 months ago
917
            "y")
918
                dir_path="$base_destination/$year"
919
                filename="${month}-${day}_${hour}-${minute}-${second}.${lowercase_ext}"
920
                ;;
921
            "m")
922
                dir_path="$base_destination/$year/$month"
923
                filename="${day}_${hour}-${minute}-${second}.${lowercase_ext}"
924
                ;;
925
            "d")
926
                dir_path="$base_destination/$year/$month/$day"
927
                filename="${month}-${day}_${hour}-${minute}-${second}.${lowercase_ext}"
928
                ;;
929
            "h")
930
                dir_path="$base_destination/$year/$month/$day/$hour"
931
                filename="${minute}-${second}.${lowercase_ext}"
932
                ;;
Bogdan Timofte authored 9 months ago
933
            "ym")
934
                # Single folder per month named yyyy-mm; filename includes day and time
935
                dir_path="$base_destination/${year}-${month}"
936
                filename="${day}_${hour}-${minute}-${second}.${lowercase_ext}"
937
                ;;
Bogdan Timofte authored 8 months ago
938
            "ymd")
Bogdan Timofte authored 9 months ago
939
                # Single folder per day named yyyy-mm-dd; filename is time
940
                dir_path="$base_destination/${year}-${month}-${day}"
941
                filename="${hour}-${minute}-${second}.${lowercase_ext}"
942
                ;;
Bogdan Timofte authored 9 months ago
943
            *)
944
                log_message "Invalid organization pattern: $ORGANIZATION" "ERROR"
945
                return 1
946
                ;;
947
        esac
Bogdan Timofte authored 9 months ago
948

            
949
    # Apply filename mode overrides
950
    case "$FILENAME_MODE" in
951
        orig)
952
            filename="$original_filename"
953
            ;;
954
        full)
955
            filename="${year}-${month}-${day}_${hour}-${minute}-${second}.${lowercase_ext}"
956
            ;;
957
        auto)
958
            # keep the auto-generated filename from the organization case
959
            ;;
960
        *)
961
            # fallback to full
962
            filename="${year}-${month}-${day}_${hour}-${minute}-${second}.${lowercase_ext}"
963
            ;;
964
    esac
965

            
Bogdan Timofte authored 9 months ago
966
    echo "$dir_path/$filename"
967
    return 0
968
}
969

            
970
# Function to find files matching patterns
971
find_source_files() {
Bogdan Timofte authored 9 months ago
972
    # Emit absolute file paths for all media files under sources, excluding DESTINATION when it is inside a source
973
    local abs_dest=""
974
    if [[ -n "$DESTINATION" ]]; then
975
        abs_dest=$(cd "$DESTINATION" 2>/dev/null && pwd) || abs_dest="$DESTINATION"
976
    fi
977

            
978
    # Build -iname expression for find
979
    local ext_expr=""
980
    for ext in "${MEDIA_EXTENSIONS[@]}"; do
981
        if [[ -n "$ext_expr" ]]; then
982
            ext_expr="$ext_expr -o"
983
        fi
984
        ext_expr="$ext_expr -iname $ext"
985
    done
986

            
Bogdan Timofte authored 9 months ago
987
    if [[ ${#SOURCE_PATTERNS[@]} -eq 0 ]]; then
Bogdan Timofte authored 9 months ago
988
        # Default: scan current directory
989
        local start_dot="."
990
        local abs_current
991
        abs_current=$(pwd)
992
        local find_cmd=(find -L "$start_dot" -type f)
993
        # If dest is inside cwd, add exclusion
994
        if [[ -n "$abs_dest" && "$abs_dest" == "$abs_current"* ]]; then
995
            find_cmd+=( ! -path "$abs_dest/*" ! -path "$abs_dest" )
Bogdan Timofte authored 9 months ago
996
        fi
Bogdan Timofte authored 9 months ago
997
        # Add expression
998
        # shellcheck disable=SC2068
Bogdan Timofte authored 2 weeks ago
999
        "${find_cmd[@]}" ! -name '._*' \( $ext_expr \) 2>/dev/null || true
Bogdan Timofte authored 9 months ago
1000
    else
Bogdan Timofte authored 9 months ago
1001
        # Scan each provided source
1002
        for src in "${SOURCE_PATTERNS[@]}"; do
1003
            if [[ -f "$src" ]]; then
Bogdan Timofte authored 2 weeks ago
1004
                if [[ "$(basename "$src")" == ._* ]]; then
1005
                    continue
1006
                fi
Bogdan Timofte authored 9 months ago
1007
                # single file - skip if it's inside dest
1008
                local abs_file
1009
                abs_file=$(cd "$(dirname "$src")" 2>/dev/null && pwd)/$(basename "$src")
1010
                if [[ -n "$abs_dest" && "$abs_file" == "$abs_dest"* ]]; then
1011
                    continue
1012
                fi
1013
                echo "$abs_file"
1014
            elif [[ -d "$src" ]]; then
1015
                local abs_src
1016
                abs_src=$(cd "$src" 2>/dev/null && pwd)
1017
                if [[ -n "$abs_src" ]]; then
1018
                    if [[ -n "$abs_dest" && "$abs_dest" == "$abs_src"* ]]; then
Bogdan Timofte authored 2 weeks ago
1019
                        find -L "$abs_src" -type f ! -name '._*' \( $ext_expr \) ! -path "$abs_dest/*" ! -path "$abs_dest" 2>/dev/null || true
Bogdan Timofte authored 9 months ago
1020
                    else
Bogdan Timofte authored 2 weeks ago
1021
                        find -L "$abs_src" -type f ! -name '._*' \( $ext_expr \) 2>/dev/null || true
Bogdan Timofte authored 9 months ago
1022
                    fi
Bogdan Timofte authored 9 months ago
1023
                else
1024
                    print_color "$YELLOW" "Warning: Could not resolve source directory: $src"
Bogdan Timofte authored 9 months ago
1025
                fi
Bogdan Timofte authored 9 months ago
1026
            else
1027
                print_color "$YELLOW" "Warning: Source path does not exist or is not a file/directory: $src"
Bogdan Timofte authored 9 months ago
1028
            fi
1029
        done
1030
    fi
1031
}
1032

            
Bogdan Timofte authored 2 weeks ago
1033
cleanup_media_sidecars() {
Bogdan Timofte authored 2 weeks ago
1034
    local file="$1"
1035
    local dir base stem ext sidecar_ext sidecar
1036
    dir=$(dirname "$file")
1037
    base=$(basename "$file")
1038
    stem="${base%.*}"
1039
    ext="${base##*.}"
1040

            
1041
    if [[ ! "$ext" =~ ^([Mm][Pp]4)$ ]]; then
1042
        return 0
1043
    fi
1044

            
Bogdan Timofte authored 2 weeks ago
1045
    local sidecar_exts
1046
    sidecar_exts=$(get_device_sidecar_extensions "$file")
1047
    [[ -z "$sidecar_exts" ]] && return 0
1048

            
1049
    for sidecar_ext in $sidecar_exts; do
Bogdan Timofte authored 2 weeks ago
1050
        sidecar="$dir/$stem.$sidecar_ext"
1051
        if [[ -f "$sidecar" ]]; then
1052
            if rm -f "$sidecar"; then
Bogdan Timofte authored 2 weeks ago
1053
                log_message "Deleted sidecar: $sidecar" "INFO"
Bogdan Timofte authored 2 weeks ago
1054
            else
Bogdan Timofte authored 2 weeks ago
1055
                log_message "Failed to delete sidecar: $sidecar" "WARNING"
Bogdan Timofte authored 2 weeks ago
1056
            fi
1057
        fi
1058
    done
1059
}
1060

            
Bogdan Timofte authored 9 months ago
1061
# Function to process a single file
1062
process_file() {
1063
    local file="$1"
1064
    local file_size=$(get_file_size "$file")
Bogdan Timofte authored 2 weeks ago
1065
    local file_label
1066
    file_label="$(basename "$file")"
Bogdan Timofte authored 9 months ago
1067
    TOTAL_SIZE=$((TOTAL_SIZE + file_size))
1068

            
Bogdan Timofte authored 2 weeks ago
1069
    if [[ $TOTAL_FILES -gt 0 && $CURRENT_FILE_INDEX -gt 0 ]]; then
1070
        print_color "$BLUE" "Processing [$CURRENT_FILE_INDEX/$TOTAL_FILES]: $file_label ($(format_size "$file_size"))"
1071
    else
1072
        print_color "$BLUE" "Processing: $file_label ($(format_size "$file_size"))"
1073
    fi
Bogdan Timofte authored 9 months ago
1074
    log_message "Processing: $file" "INFO"
1075

            
1076
    # Extract date information
1077
    local date_info=$(extract_file_date "$file")
1078
        local extract_status=$?
1079
        if [[ $extract_status -eq 2 ]]; then
1080
            if [[ $COLLECT_UNSORTABLE -eq 1 ]]; then
1081
                local unsortable_dir="$DESTINATION/unsortable"
1082
                mkdir -p "$unsortable_dir"
1083
                local unsortable_path="$unsortable_dir/$(basename "$file")"
Bogdan Timofte authored 2 weeks ago
1084
                local desired_unsortable_path="$unsortable_path"
1085
                local unsortable_conflict_status
1086
                resolve_destination_conflict "$unsortable_path" "$file"
1087
                unsortable_conflict_status=$?
1088
                if [[ $unsortable_conflict_status -eq 0 ]]; then
1089
                    unsortable_path="$RESOLVED_DESTINATION_PATH"
1090
                    if [[ "$unsortable_path" != "$desired_unsortable_path" ]]; then
1091
                        log_message "Destination already exists or is already planned: $desired_unsortable_path - using: $unsortable_path" "WARNING"
1092
                    fi
1093
                elif [[ $unsortable_conflict_status -eq 3 ]]; then
1094
                    log_message "Destination conflict skipped: $desired_unsortable_path" "WARNING"
1095
                    SKIPPED_FILES=$((SKIPPED_FILES + 1))
1096
                    return 1
1097
                elif [[ $unsortable_conflict_status -eq 4 ]]; then
1098
                    log_message "Import aborted by user at destination conflict: $desired_unsortable_path" "ERROR"
1099
                    ERROR_FILES=$((ERROR_FILES + 1))
1100
                    FATAL_ERROR=1
1101
                    return 2
1102
                else
1103
                    log_message "Could not resolve a unique destination path for $file (wanted: $desired_unsortable_path)" "ERROR"
1104
                    ERROR_FILES=$((ERROR_FILES + 1))
1105
                    return 1
1106
                fi
Bogdan Timofte authored 9 months ago
1107
                if [[ $DRY_RUN -eq 1 ]]; then
1108
                    print_color "$BLUE" "Would move unsortable: $file -> $unsortable_path"
1109
                else
Bogdan Timofte authored 3 weeks ago
1110
                    if verified_move_file "$file" "$unsortable_path" ""; then
Bogdan Timofte authored 9 months ago
1111
                        log_message "Unsortable: $file -> $unsortable_path" "SUCCESS"
1112
                    else
Bogdan Timofte authored 3 weeks ago
1113
                        log_message "Failed to move unsortable file after verification: $file" "ERROR"
Bogdan Timofte authored 9 months ago
1114
                    fi
1115
                fi
1116
                SKIPPED_FILES=$((SKIPPED_FILES + 1))
1117
            else
1118
                log_message "Could not extract date from $file - skipping" "WARNING"
1119
                SKIPPED_FILES=$((SKIPPED_FILES + 1))
1120
            fi
1121
            return 1
1122
        elif [[ $extract_status -ne 0 ]] || [[ -z "$date_info" ]]; then
1123
            log_message "Could not extract date from $file - skipping" "WARNING"
1124
            SKIPPED_FILES=$((SKIPPED_FILES + 1))
1125
            return 1
1126
        fi
1127
        local date_str="${date_info%|*}"
1128
        local date_source="${date_info#*|}"
1129
        log_message "Date: $date_str (from $date_source)" "INFO"
1130
        # Generate destination path
Bogdan Timofte authored 2 weeks ago
1131
        local original_basename
1132
        original_basename="$(basename "$file")"
1133
        local dest_path
1134
        dest_path=$(generate_destination_path "$date_str" "$original_basename" "$DESTINATION")
Bogdan Timofte authored 9 months ago
1135
        if [[ $? -ne 0 ]] || [[ -z "$dest_path" ]]; then
1136
            log_message "Could not generate destination path for $file" "ERROR"
1137
            ERROR_FILES=$((ERROR_FILES + 1))
1138
            FATAL_ERROR=1
1139
            return 2
1140
    fi
Bogdan Timofte authored 2 weeks ago
1141

            
1142
    local desired_dest_path="$dest_path"
1143
    local conflict_status
1144
    resolve_destination_conflict "$dest_path" "$file"
1145
    conflict_status=$?
1146
    if [[ $conflict_status -eq 0 ]]; then
1147
        dest_path="$RESOLVED_DESTINATION_PATH"
1148
    elif [[ $conflict_status -eq 3 ]]; then
1149
        log_message "Destination conflict skipped: $desired_dest_path" "WARNING"
1150
        SKIPPED_FILES=$((SKIPPED_FILES + 1))
1151
        return 1
1152
    elif [[ $conflict_status -eq 4 ]]; then
1153
        log_message "Import aborted by user at destination conflict: $desired_dest_path" "ERROR"
1154
        ERROR_FILES=$((ERROR_FILES + 1))
1155
        FATAL_ERROR=1
1156
        return 2
1157
    else
1158
        log_message "Could not resolve a unique destination path for $file (wanted: $desired_dest_path)" "ERROR"
1159
        ERROR_FILES=$((ERROR_FILES + 1))
1160
        return 1
Bogdan Timofte authored 9 months ago
1161
    fi
Bogdan Timofte authored 2 weeks ago
1162
    if [[ "$dest_path" != "$desired_dest_path" ]]; then
1163
        log_message "Destination already exists or is already planned: $desired_dest_path - using: $dest_path" "WARNING"
1164
    fi
1165

            
1166
    local dest_dir
1167
    dest_dir=$(dirname "$dest_path")
Bogdan Timofte authored 9 months ago
1168

            
1169
    if [[ $DRY_RUN -eq 1 ]]; then
1170
        if [[ $KEEP_ORIGINALS -eq 1 ]]; then
1171
            print_color "$BLUE" "Would copy: $file -> $dest_path"
1172
        else
1173
            print_color "$BLUE" "Would move: $file -> $dest_path"
1174
        fi
Bogdan Timofte authored 2 weeks ago
1175
        if should_sync_imported_metadata "$original_basename" "$date_source"; then
1176
            print_color "$BLUE" "Would sync metadata date: $dest_path -> $date_str"
1177
        fi
Bogdan Timofte authored 9 months ago
1178
        PROCESSED_FILES=$((PROCESSED_FILES + 1))
1179
        PROCESSED_SIZE=$((PROCESSED_SIZE + file_size))
1180
        return 0
1181
    fi
1182

            
1183
    # Create destination directory
1184
    if ! mkdir -p "$dest_dir"; then
1185
        log_message "Could not create directory: $dest_dir" "ERROR"
1186
        ERROR_FILES=$((ERROR_FILES + 1))
1187
        return 1
1188
    fi
1189

            
Bogdan Timofte authored 2 weeks ago
1190
    local sync_metadata_after_copy=0
1191
    local verification_date="$date_str"
1192
    if should_sync_imported_metadata "$original_basename" "$date_source"; then
1193
        sync_metadata_after_copy=1
1194
        verification_date=""
1195
    fi
1196

            
1197
    # Copy or move file using safe helpers after destination conflicts are resolved.
Bogdan Timofte authored 9 months ago
1198
    if [[ $KEEP_ORIGINALS -eq 1 ]]; then
Bogdan Timofte authored 2 weeks ago
1199
        if copy_with_verification "$file" "$dest_path" "$verification_date"; then
1200
            if [[ $sync_metadata_after_copy -eq 1 ]]; then
1201
                if ! sync_destination_metadata_to_date "$dest_path" "$date_str"; then
1202
                    log_message "Failed to sync destination metadata timestamps: $dest_path" "ERROR"
1203
                    ERROR_FILES=$((ERROR_FILES + 1))
1204
                    return 1
1205
                elif ! verify_synced_metadata_date "$dest_path" "$date_str"; then
1206
                    ERROR_FILES=$((ERROR_FILES + 1))
1207
                    return 1
1208
                fi
1209
            fi
Bogdan Timofte authored 9 months ago
1210
            log_message "Copied: $file -> $dest_path" "SUCCESS"
Bogdan Timofte authored 2 weeks ago
1211
            if [[ $CLEANUP_MEDIA_SIDECARS -eq 1 ]]; then
1212
                cleanup_media_sidecars "$file"
Bogdan Timofte authored 2 weeks ago
1213
            fi
Bogdan Timofte authored 9 months ago
1214
            PROCESSED_FILES=$((PROCESSED_FILES + 1))
1215
            PROCESSED_SIZE=$((PROCESSED_SIZE + file_size))
1216
            return 0
1217
        else
Bogdan Timofte authored 3 weeks ago
1218
            log_message "Failed to copy or verify destination: $file -> $dest_path" "ERROR"
Bogdan Timofte authored 9 months ago
1219
            ERROR_FILES=$((ERROR_FILES + 1))
1220
            return 1
1221
        fi
1222
    else
Bogdan Timofte authored 2 weeks ago
1223
        if [[ $sync_metadata_after_copy -eq 1 ]]; then
1224
            if copy_with_verification "$file" "$dest_path" "$verification_date"; then
1225
                if ! sync_destination_metadata_to_date "$dest_path" "$date_str"; then
1226
                    log_message "Failed to sync destination metadata timestamps: $dest_path" "ERROR"
1227
                    ERROR_FILES=$((ERROR_FILES + 1))
1228
                    return 1
1229
                fi
1230
                if ! verify_synced_metadata_date "$dest_path" "$date_str"; then
1231
                    ERROR_FILES=$((ERROR_FILES + 1))
1232
                    return 1
1233
                fi
1234
                if ! remove_source_file "$file"; then
1235
                    log_message "Copied, verified, and synced destination, but failed to remove source: $file" "ERROR"
1236
                    ERROR_FILES=$((ERROR_FILES + 1))
1237
                    return 1
1238
                fi
1239
                log_message "Moved: $file -> $dest_path" "SUCCESS"
Bogdan Timofte authored 2 weeks ago
1240
            if [[ $CLEANUP_MEDIA_SIDECARS -eq 1 ]]; then
1241
                cleanup_media_sidecars "$file"
Bogdan Timofte authored 2 weeks ago
1242
            fi
Bogdan Timofte authored 2 weeks ago
1243
                PROCESSED_FILES=$((PROCESSED_FILES + 1))
1244
                PROCESSED_SIZE=$((PROCESSED_SIZE + file_size))
1245
                return 0
1246
            else
1247
                log_message "Failed to move after copy verification: $file -> $dest_path" "ERROR"
1248
                ERROR_FILES=$((ERROR_FILES + 1))
1249
                return 1
1250
            fi
1251
        elif verified_move_file "$file" "$dest_path" "$date_str"; then
Bogdan Timofte authored 9 months ago
1252
            log_message "Moved: $file -> $dest_path" "SUCCESS"
Bogdan Timofte authored 2 weeks ago
1253
            if [[ $CLEANUP_MEDIA_SIDECARS -eq 1 ]]; then
1254
                cleanup_media_sidecars "$file"
Bogdan Timofte authored 2 weeks ago
1255
            fi
Bogdan Timofte authored 2 weeks ago
1256
            if [[ $sync_metadata_after_copy -eq 1 ]]; then
1257
                if ! sync_destination_metadata_to_date "$dest_path" "$date_str"; then
1258
                    log_message "Failed to sync destination metadata timestamps: $dest_path" "WARNING"
1259
                elif ! verify_synced_metadata_date "$dest_path" "$date_str"; then
1260
                    log_message "Failed to verify synced destination metadata timestamps: $dest_path" "WARNING"
1261
                fi
1262
            fi
Bogdan Timofte authored 9 months ago
1263
            PROCESSED_FILES=$((PROCESSED_FILES + 1))
1264
            PROCESSED_SIZE=$((PROCESSED_SIZE + file_size))
1265
            return 0
1266
        else
Bogdan Timofte authored 3 weeks ago
1267
            log_message "Failed to move after copy verification: $file -> $dest_path" "ERROR"
Bogdan Timofte authored 9 months ago
1268
            ERROR_FILES=$((ERROR_FILES + 1))
1269
            return 1
1270
        fi
1271
    fi
1272
}
1273

            
1274
# Function to display final report
1275
show_report() {
1276
    local end_time=$(date +%s)
1277
    local elapsed_time=$((end_time - START_TIME))
1278
    local hours=$((elapsed_time / 3600))
1279
    local minutes=$(((elapsed_time % 3600) / 60))
1280
    local seconds=$((elapsed_time % 60))
1281

            
1282
    echo ""
1283
    print_color "$GREEN" "=========================================="
1284
    print_color "$GREEN" "           PROCESSING REPORT"
1285
    print_color "$GREEN" "=========================================="
1286
    echo ""
Bogdan Timofte authored 2 weeks ago
1287

            
1288
    echo "Files Summary:"
1289
    report_line "Total files found:" "$TOTAL_FILES"
1290
    report_line "Successfully processed:" "$PROCESSED_FILES"
1291
    report_line "Skipped:" "$SKIPPED_FILES"
1292
    report_line "Errors:" "$ERROR_FILES"
1293
    echo ""
1294

            
Bogdan Timofte authored 9 months ago
1295
    echo "Size Summary:"
Bogdan Timofte authored 2 weeks ago
1296
    report_line "Total size found:" "$(format_size $TOTAL_SIZE)"
1297
    report_line "Successfully processed:" "$(format_size $PROCESSED_SIZE)"
Bogdan Timofte authored 9 months ago
1298
    echo ""
Bogdan Timofte authored 2 weeks ago
1299

            
1300
    echo "Time Summary:"
1301
    report_line "Time elapsed:" "$(printf "%02d:%02d:%02d" $hours $minutes $seconds)"
1302
    if [[ $elapsed_time -gt 0 && $PROCESSED_SIZE -gt 0 ]]; then
1303
        local data_rate
1304
        data_rate=$(format_data_rate "$PROCESSED_SIZE" "$elapsed_time")
1305
        if [[ -n "$data_rate" ]]; then
1306
            report_line "Data rate:" "$data_rate"
1307
        fi
1308
    fi
1309

            
1310
    echo ""
1311

            
Bogdan Timofte authored 9 months ago
1312
    if [[ $DRY_RUN -eq 1 ]]; then
1313
        print_color "$YELLOW" "DRY RUN MODE - No files were actually moved/copied"
1314
    elif [[ $KEEP_ORIGINALS -eq 1 ]]; then
1315
        print_color "$BLUE" "COPY MODE - Original files were preserved"
1316
    else
1317
        print_color "$GREEN" "MOVE MODE - Files were moved to destination"
1318
    fi
1319

            
1320
    echo ""
1321
    print_color "$GREEN" "=========================================="
1322
}
1323

            
Bogdan Timofte authored 2 weeks ago
1324
if [[ "${BASH_SOURCE[0]}" != "$0" ]]; then
1325
    return 0
1326
fi
1327

            
Bogdan Timofte authored 9 months ago
1328
# Parse command line arguments
1329
while [[ $# -gt 0 ]]; do
1330
    case $1 in
1331
        -o|--organization)
1332
            ORGANIZATION="$2"
Bogdan Timofte authored 8 months ago
1333
            # Accept new patterns: ym, ymd as well as single-letter ones
1334
            if [[ ! "$ORGANIZATION" =~ ^(y|m|d|h|ym|ymd)$ ]]; then
Bogdan Timofte authored 9 months ago
1335
                print_color "$RED" "Error: Invalid organization pattern. Must be one of: y, m, d, h, ym, ymd"
Bogdan Timofte authored 9 months ago
1336
                exit 1
1337
            fi
1338
            shift 2
1339
            ;;
Bogdan Timofte authored 9 months ago
1340

            
1341
        -F|--filename-mode)
1342
            FILENAME_MODE="$2"
1343
            if [[ ! "$FILENAME_MODE" =~ ^(auto|full|orig)$ ]]; then
1344
                print_color "$RED" "Error: Invalid filename mode. Must be one of: auto, full, orig"
1345
                exit 1
1346
            fi
1347
            shift 2
Bogdan Timofte authored 9 months ago
1348
            ;;
Bogdan Timofte authored 9 months ago
1349
    # (conflict resolution option removed; let exiftool/filesystem handle naming conflicts)
Bogdan Timofte authored 9 months ago
1350
        --collect-unsortable)
1351
            COLLECT_UNSORTABLE=1
1352
            shift
1353
            ;;
Bogdan Timofte authored 8 months ago
1354
        --keep-empty-dirs)
1355
            CLEANUP_EMPTY_DIRS=0
1356
            shift
1357
            ;;
Bogdan Timofte authored 9 months ago
1358
        -s|--source)
1359
            SOURCE_PATTERNS+=("$2")
1360
            shift 2
1361
            ;;
1362
        -d|--destination)
1363
            DESTINATION="$2"
1364
            shift 2
1365
            ;;
1366
        -k|--keep-originals)
1367
            KEEP_ORIGINALS=1
1368
            shift
1369
            ;;
Bogdan Timofte authored 3 weeks ago
1370
        --verify-mode)
1371
            VERIFY_MODE="$2"
1372
            if [[ ! "$VERIFY_MODE" =~ ^(size|strict|none)$ ]]; then
1373
                print_color "$RED" "Error: Invalid verify mode. Must be one of: size, strict, none"
1374
                exit 1
1375
            fi
1376
            shift 2
1377
            ;;
Bogdan Timofte authored 2 weeks ago
1378
        --date-source)
1379
            DATE_SOURCE="$2"
1380
            if [[ ! "$DATE_SOURCE" =~ ^(auto|exif|filesystem)$ ]]; then
1381
                print_color "$RED" "Error: Invalid date source. Must be one of: auto, exif, filesystem"
1382
                exit 1
1383
            fi
1384
            shift 2
1385
            ;;
1386
        --sync-metadata)
1387
            SYNC_METADATA=1
1388
            shift
1389
            ;;
Bogdan Timofte authored a week ago
1390
        --no-quicktime-utc)
1391
            QUICKTIME_UTC=0
1392
            shift
1393
            ;;
Bogdan Timofte authored 2 weeks ago
1394
        --unattended)
1395
            UNATTENDED=1
1396
            shift
1397
            ;;
Bogdan Timofte authored 2 weeks ago
1398
        --keep-sidecars)
1399
            CLEANUP_MEDIA_SIDECARS=0
Bogdan Timofte authored 2 weeks ago
1400
            shift
1401
            ;;
Bogdan Timofte authored 9 months ago
1402
        --dry-run)
1403
            DRY_RUN=1
1404
            shift
1405
            ;;
1406
        -v|--verbose)
1407
            VERBOSE=1
1408
            shift
1409
            ;;
1410
        -h|--help)
1411
            show_help
1412
            exit 0
1413
            ;;
1414
        --version)
1415
            show_version
1416
            exit 0
1417
            ;;
1418
        *)
1419
            print_color "$RED" "Error: Unknown option: $1"
1420
            echo "Use -h or --help for usage information."
1421
            exit 1
1422
            ;;
1423
    esac
1424
done
1425

            
Bogdan Timofte authored 2 weeks ago
1426
# Non-interactive execution cannot safely ask conflict questions.
1427
if [[ $UNATTENDED -eq 0 && ( ! -t 0 || ! -t 1 ) ]]; then
1428
    UNATTENDED=1
1429
fi
1430

            
Bogdan Timofte authored 9 months ago
1431
# If no organization is provided, leave ORGANIZATION empty and filename mode will decide naming
1432

            
Bogdan Timofte authored 3 weeks ago
1433
# If no source specified, default to current directory. Refuse to run when cwd is unsafe.
1434
if [[ ${#SOURCE_PATTERNS[@]} -eq 0 ]]; then
1435
    cwd=$(pwd)
1436
    # Resolve home and root paths
1437
    home_dir="$HOME"
1438
    case "$cwd" in
1439
        "$home_dir"|"/"|"/etc"|"/var"|"/bin"|"/sbin"|"/dev"|"/proc"|"/sys"|"/run"|"/usr"|"/lib"|"/lib64"|"/boot"|"/snap")
1440
            print_color "$RED" "Refusing to run with default source in unsafe directory: $cwd"
1441
            print_color "$RED" "Please specify $cwd as source explicitly with -s or run from a safer directory."
1442
            exit 1
1443
            ;;
1444
        *)
1445
            SOURCE_PATTERNS+=("$cwd")
1446
            ;;
1447
    esac
1448
fi
1449

            
1450
# Set default destination: if user didn't provide -d and a source was given, use first source + /sorted
Bogdan Timofte authored 9 months ago
1451
if [[ -z "$DESTINATION" ]]; then
Bogdan Timofte authored 9 months ago
1452
    if [[ ${#SOURCE_PATTERNS[@]} -gt 1 ]]; then
1453
        print_color "$RED" "Error: Multiple sources specified - destination (-d|--destination) is required when using multiple sources."
1454
        echo "Use -h for help."
1455
        exit 1
1456
    fi
1457

            
1458
    if [[ ${#SOURCE_PATTERNS[@]} -gt 0 ]]; then
1459
        first_source="${SOURCE_PATTERNS[0]}"
Bogdan Timofte authored 3 weeks ago
1460
        if [[ -d "$first_source" ]]; then
1461
            DESTINATION="$first_source/sorted"
1462
        elif [[ -f "$first_source" ]]; then
1463
            DESTINATION="$(dirname "$first_source")/sorted"
Bogdan Timofte authored 9 months ago
1464
        else
1465
            print_color "$YELLOW" "Warning: first source does not exist; defaulting destination to ./sorted"
1466
            DESTINATION="./sorted"
1467
        fi
1468
    else
1469
        DESTINATION="./sorted"
1470
    fi
Bogdan Timofte authored 9 months ago
1471
fi
1472

            
1473
# Convert destination to absolute path
1474
DESTINATION=$(cd "$(dirname "$DESTINATION")" 2>/dev/null && pwd)/$(basename "$DESTINATION") || DESTINATION=$(realpath "$DESTINATION" 2>/dev/null) || DESTINATION="$DESTINATION"
1475

            
1476
# Display configuration
1477
print_color "$GREEN" "$SCRIPT_NAME v$VERSION"
1478
echo ""
1479
echo "Configuration:"
1480
echo "  Organization pattern: $ORGANIZATION"
1481
echo "  Destination:         $DESTINATION"
1482
echo "  Keep originals:      $([ $KEEP_ORIGINALS -eq 1 ] && echo "Yes" || echo "No")"
Bogdan Timofte authored 3 weeks ago
1483
echo "  Verify mode:         $VERIFY_MODE"
Bogdan Timofte authored 2 weeks ago
1484
echo "  Date source:         $DATE_SOURCE"
1485
echo "  Sync metadata:       $([ $SYNC_METADATA -eq 1 ] && echo "Yes" || echo "No")"
Bogdan Timofte authored a week ago
1486
echo "  QuickTime UTC:       $([ $QUICKTIME_UTC -eq 1 ] && echo "Yes" || echo "No (local time assumed)")"
Bogdan Timofte authored 2 weeks ago
1487
echo "  Unattended:          $([ $UNATTENDED -eq 1 ] && echo "Yes" || echo "No")"
Bogdan Timofte authored 9 months ago
1488
echo "  Dry run:             $([ $DRY_RUN -eq 1 ] && echo "Yes" || echo "No")"
Bogdan Timofte authored 8 months ago
1489
echo "  Keep empty dirs:     $([ $CLEANUP_EMPTY_DIRS -eq 0 ] && echo "Yes" || echo "No")"
Bogdan Timofte authored 9 months ago
1490
echo "  Verbose:             $([ $VERBOSE -eq 1 ] && echo "Yes" || echo "No")"
1491

            
1492
if [[ ${#SOURCE_PATTERNS[@]} -gt 0 ]]; then
1493
    echo "  Source patterns:"
1494
    for pattern in "${SOURCE_PATTERNS[@]}"; do
1495
        echo "    - $pattern"
1496
    done
1497
else
1498
    echo "  Source patterns:     All media files in current directory"
1499
fi
1500

            
1501
echo ""
1502

            
1503
# Check dependencies
1504
check_dependencies
1505

            
1506
# Create destination directory if it doesn't exist (unless dry run)
1507
if [[ $DRY_RUN -eq 0 ]]; then
1508
    if ! mkdir -p "$DESTINATION"; then
1509
        print_color "$RED" "Error: Cannot create destination directory: $DESTINATION"
1510
        exit 1
1511
    fi
1512
fi
1513

            
1514
# Find all source files
1515

            
1516
print_color "$BLUE" "Scanning for media files..."
1517
files=()
1518
while IFS= read -r file; do
1519
    files+=("$file")
1520
done < <(find_source_files)
Bogdan Timofte authored 2 weeks ago
1521
if [[ ${#files[@]} -gt 0 ]]; then
1522
    IFS=$'\n' files=($(printf '%s\n' "${files[@]}" | sort))
1523
    unset IFS
1524
fi
Bogdan Timofte authored 9 months ago
1525
TOTAL_FILES=${#files[@]}
1526

            
1527
if [[ $TOTAL_FILES -eq 0 ]]; then
1528
    print_color "$YELLOW" "No media files found matching the specified patterns."
1529
    exit 0
1530
fi
1531

            
1532
print_color "$BLUE" "Found $TOTAL_FILES media files to process"
1533
echo ""
1534

            
1535
# Process each file
1536

            
1537
FATAL_ERROR=0
1538
for file in "${files[@]}"; do
1539
    if [[ -f "$file" ]]; then
Bogdan Timofte authored 2 weeks ago
1540
        CURRENT_FILE_INDEX=$((CURRENT_FILE_INDEX + 1))
Bogdan Timofte authored 9 months ago
1541
        process_file "$file"
1542
        if [[ $FATAL_ERROR -eq 1 ]]; then
1543
            print_color "$RED" "Fatal error encountered. Stopping further processing."
1544
            break
1545
        fi
1546
    fi
1547
done
1548

            
Bogdan Timofte authored 8 months ago
1549
# Clean up empty directories if requested (default behavior)
1550
if [[ $CLEANUP_EMPTY_DIRS -eq 1 && $DRY_RUN -eq 0 ]]; then
1551
    print_color "$BLUE" "Cleaning up empty directories..."
1552
    # Find and remove empty directories under destination, but don't remove the destination itself
1553
    find "$DESTINATION" -type d -empty -not -path "$DESTINATION" -delete 2>/dev/null || true
1554
    print_color "$GREEN" "Empty directory cleanup completed"
1555
fi
1556

            
Bogdan Timofte authored 9 months ago
1557
# Show final report
1558
show_report
1559

            
1560
# Exit with appropriate code
1561
if [[ $ERROR_FILES -gt 0 ]]; then
1562
    exit 1
1563
else
1564
    exit 0
1565
fi