metaflac-field2field: minor improvements, shellcheck

This commit is contained in:
Matteo Cypriani 2018-04-11 22:11:11 +02:00
parent 22783997cc
commit aca5e08738
1 changed files with 39 additions and 35 deletions

View File

@ -18,41 +18,45 @@
#set -x #set -x
print_usage() USAGE=$(cat <<EOF
{ Usage:
echo "Usage:" $0 <ACTION> <SRC_FIELD> <DST_FIELD> <FILE.flac> [ <FILE2.flac> [...] ]
echo " $0 <ACTION> <SRC_FIELD> <DST_FIELD> <FILE.flac> [ <FILE2.flac> [...] ]"
echo "<ACTION> : copy | move | rmcopy | mvcopy" <ACTION> : copy | move | rmcopy | mvcopy
echo " copy: each occurrence of SRC_FIELD is duplicated to create an occurrence of" copy: each occurrence of SRC_FIELD is duplicated to create an occurrence of
echo " DST_FIELD; all the existing occurrences of SRC_FIELD and DST_FIELD are" DST_FIELD; all the existing occurrences of SRC_FIELD and DST_FIELD are
echo " kept." kept.
echo " move: each occurrence of SRC_FIELD is renamed to create an occurrence of" move: each occurrence of SRC_FIELD is renamed to create an occurrence of
echo " DST_FIELD; the existing occurrences of DST_FIELD are kept." DST_FIELD; the existing occurrences of DST_FIELD are kept.
echo " rmcopy: the existing occurrences of DST_FIELD are deleted, then each" rmcopy: the existing occurrences of DST_FIELD are deleted, then each
echo " occurrence of SRC_FIELD is duplicated to create an occurrence" occurrence of SRC_FIELD is duplicated to create an occurrence
echo " of DST_FIELD; the occurrences of SRC_FIELD are kept." of DST_FIELD; the occurrences of SRC_FIELD are kept.
echo " rmmove: the existing occurrences of DST_FIELD are deleted, then each" rmmove: the existing occurrences of DST_FIELD are deleted, then each
echo " occurrence of SRC_FIELD is renamed to create an occurrence of" occurrence of SRC_FIELD is renamed to create an occurrence of
echo " DST_FIELD." DST_FIELD.
exit 1 EOF
} )
# Right number of arguments? # Right number of arguments?
if [ $# -lt 4 ] ; then if [ $# -lt 4 ] ; then
echo "Wrong number of arguments!" echo "Wrong number of arguments!" >&2
print_usage print_usage
fi fi
ACTION=$1 ACTION="$1"
shift shift
# Known action type? # Known action type?
case $ACTION in case "$ACTION" in
copy) ;; copy) ;;
move) ;; move) ;;
rmcopy) ;; rmcopy) ;;
rmmove) ;; rmmove) ;;
*) echo "Action unknown \"$ACTION\"!" ; print_usage ;; *)
printf 'Action unknown "%s"!\n\n' "$ACTION" >&2
printf '%s\n' "$USAGE" >&2
exit 1
;;
esac esac
SRC_FIELD=$1 SRC_FIELD=$1
@ -60,28 +64,28 @@ shift
DST_FIELD=$1 DST_FIELD=$1
shift shift
echo "Source field: $SRC_FIELD" printf 'Source field: %s\n' "$SRC_FIELD"
echo "Destination field: $DST_FIELD" printf 'Destination field: %s\n' "$DST_FIELD"
echo "Action: $ACTION" printf 'Action: %s\n' "$ACTION"
echo "Processing files..." echo "Processing files..."
if echo $ACTION | egrep '^rm' >/dev/null ; then if echo "$ACTION" | grep '^rm' >/dev/null ; then
echo "Deleting field $DST_FIELD on each file..." printf 'Deleting field "%s" on each file...\n' "$DST_FIELD"
metaflac --remove-tag=$DST_FIELD "$@" metaflac --remove-tag="$DST_FIELD" "$@"
fi fi
for file in "$@" ; do for file in "$@" ; do
echo "Setting field \"$DST_FIELD\" on file: $file" printf 'Setting field "%s" on file: "%s"\n' "$DST_FIELD" "$file"
metaflac --show-tag=$SRC_FIELD "$file" | sed 's/^[A-Z]\+=//' | while read c1 ; do metaflac --show-tag="$SRC_FIELD" "$file" | sed 's/^[A-Z]\+=//' | while read -r c1 ; do
echo "Adding $DST_FIELD=$c1..." printf 'Adding "%s"...\n' "$DST_FIELD=$c1"
metaflac --set-tag=$DST_FIELD="$c1" "$file" metaflac --set-tag="$DST_FIELD"="$c1" "$file"
done done
done done
if echo $ACTION | egrep 'move$' >/dev/null ; then if echo "$ACTION" | grep 'move$' >/dev/null ; then
echo "Deleting $SRC_FIELD on each file..." printf 'Deleting "%s" on each file...\n' "$SRC_FIELD"
metaflac --remove-tag=$SRC_FIELD "$@" metaflac --remove-tag="$SRC_FIELD" "$@"
fi fi
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4