#!/bin/bash # Includes #----------------------------------------------------------------------------------------------------------------------------------- . /usr/global/lib/music.bash # System Constants #----------------------------------------------------------------------------------------------------------------------------------- AR_NAME="album_renum" AR_VERSION="$Name: album_renum-1_0_0-1 $" # Print Version #----------------------------------------------------------------------------------------------------------------------------------- function version_print() { echo ${AR_VERSION} | sed -e 's/.*: //' -e 's/-/ /' -e 's/_/\./g' -e 's/\$$//' } # Prin Help #----------------------------------------------------------------------------------------------------------------------------------- function help_print() { echo "${AR_NAME} [-h | --help] | [-V | --version] | [-T | --test] [-v | --verbose] [ -f | --first_number ] [ -d | --number_of_digit ] " } # Arg Parse #----------------------------------------------------------------------------------------------------------------------------------- function args_parse() { mode="default" verbose="false" source_dir="" target_dir="" first_number="1" number_of_digit="2" tmp_args=$(getopt -o f:d:ThVv --long first_number:,number_of_digit:,test,help,version,verbose -n "${AR_NAME}" -- "$@") if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi # Note the quotes around `$TEMP': they are essential! eval set -- "${tmp_args}" while true ; do case "$1" in # Options -f|--first_number) shift; first_number="$1"; shift;; -d|--number_of_digit) shift; number_of_digit="$1"; shift;; # Mode switches -T|--test) mode="test"; shift;; -h|--help) mode="exit"; help_print; shift;; -V|--version) mode="exit"; version_print; shift;; # Global options -v|--verbose) verbose="true"; shift;; # --) shift; break;; *) echo "args_parse internal error [$1] !"; exit 1;; esac done if [[ "${#}" != "2" ]] then if [[ "${#}" -lt "2" ]] then echo "Not enough args!" else echo "Too many args!" fi mode="exit" help_print else source_dir="$1" target_dir="$2" fi } # Main #----------------------------------------------------------------------------------------------------------------------------------- shopt -s extglob args_parse "$@" if [[ ${mode} == "exit" ]] then exit 0 else if [[ ${mode} == "test" ]] then dry_run=true else dry_run=flase fi fi echo "mode: [${mode}] verbose: [${verbose}] source_dir: [${source_dir}] target_dir: [${target_dir}] first_number: [${first_number}] number_of_digit: [${number_of_digit}]" 1>&2 i="${first_number}" for file in "${source_dir}"/*@(.flac|.mp3) do j=$(printf "%0${number_of_digit}d" $i) target_file="${target_dir}/$(echo $(basename ${file}) | sed -e "s/^[0-9]*-/$j-/")"; cmd="cp \"${file}\" \"${target_file}\"" exec_cmd "${cmd}" tag_delete "${target_file}" "TRACKNUMBER" tag_delete "${target_file}" "DISCNUMBER" tag_delete "${target_file}" "DISCTOTAL" tag_write "${target_file}" "TRACKNUMBER" "$j" i=$(($i+1)) done