#!/bin/bash # Configuration src_dir=/tmp/tmsu #src_dir=/home/user/hydrus_export dest_dir=/home/user/tmsufiles tmsu_db=/home/user/.tmsu/db formats=("jpg" "jpeg" "png" "gif" "mp4" "webm" "tiff") # Verify dependencies if ! command -v b3sum &> /dev/null; then echo "Error: b3sum is required but not installed." >&2 exit 1 fi if ! command -v tmsu &> /dev/null; then echo "Error: TMSU is required but not installed." >&2 exit 1 fi # Create destination directory if it doesn't exist mkdir -p "$dest_dir" || exit 1 # Process files cd "$src_dir" || exit 1 for format in "${formats[@]}"; do while IFS= read -r -d '' f; do # Skip empty results [ -f "$f" ] || continue # 1. Extract base name without extension base=$(basename "$f" ".$format") # 2. Remove duplicate avoidance patterns (numbers in parentheses) clean_base=$(echo "$base" | sed -E 's/ \([0-9]+\)$//') # 3. Extract tags (split on spaces, exclude empty elements) IFS=' ' read -r -a tags_array <<< "$clean_base" # Filter out any empty elements from tags_array filtered_tags=() for tag in "${tags_array[@]}"; do if [ -n "$tag" ]; then filtered_tags+=("$tag") fi done # 4. Compute BLAKE3 hash hash=$(b3sum "$f" | awk '{print $1}') newname="$hash.$format" # 5. Copy with conflict handling if cp --update=none "$f" "$dest_dir/$newname"; then # 6. Tag only if copy succeeded and tags exist if [ ${#filtered_tags[@]} -ne 0 ]; then tmsu --database="$tmsu_db" tag "$dest_dir/$newname" "${filtered_tags[@]}" else echo "No tags found for: $f" >&2 fi else echo "Copy failed or already exists: $f" >&2 fi done < <(find . -maxdepth 1 -type f -iname "*.$format" -print0) done