#!/bin/bash set -Eeuo pipefail escape_kid3() { printf '%s' "$1" | sed "s/'/'\\\\''/g" } DRY_RUN=false if [[ "$1" == "--dry-run" ]]; then DRY_RUN=true shift fi if [[ $# -ne 1 ]]; then echo "Usage: $0 [--dry-run] file.txt" exit 1 fi list="$1" if [[ ! -f "$list" ]]; then echo "ERROR: File list not found: $list" exit 1 fi while IFS= read -r file; do [[ -z "$file" ]] && continue if [[ ! -e "$file" ]]; then echo "ERROR: Missing file: $file" exit 1 fi if [[ -d "$file" ]]; then echo "WARNING: Is a directory, skipping: $file" continue fi bookdir="$(dirname "$file")" booktitle="$(basename "$bookdir")" author="$(basename "$(dirname "$bookdir")")" echo echo "File: $file" echo "Artist: $author" echo "Album: $booktitle" if $DRY_RUN; then echo "DRY RUN: no changes made" continue fi artist=$(escape_kid3 "$author") album=$(escape_kid3 "$booktitle") kid3-cli "$file" \ -c "tag 1" \ -c "set artist '$artist'" \ -c "set album '$album'" \ -c "tag 2" \ -c "set artist '$artist'" \ -c "set album '$album'" \ -c save done < "$list"