110 lines
2.6 KiB
Bash
Executable File
110 lines
2.6 KiB
Bash
Executable File
#!/bin/bash
|
|
# Author: smirky@smirky.net
|
|
# Created on: 09.06.2017
|
|
# Last modifier: smirky@smirky.net
|
|
# Last modified: 23.03.2018
|
|
|
|
# Some 'sane' defaults
|
|
unset verbose
|
|
unset restore
|
|
unset music_dir
|
|
old_state_file=''
|
|
version="20180323"
|
|
|
|
print_version() {
|
|
printf "Version: %s\\n" "${version}"
|
|
}
|
|
|
|
print_usage() {
|
|
printf "Usage: %s -d DIRECTORY [-r STATE_FILE] [-V]\\n" "${0}"
|
|
}
|
|
|
|
print_help() {
|
|
print_usage
|
|
printf "Rename all mp3 files in a directory with ability to restore.
|
|
|
|
-d, --directory DIRECTORY specify the work path
|
|
-r, --restore STATE_FILE restore a previous state of file names
|
|
-V, --verbose output verbose details while running
|
|
-h, --help display this help and exit
|
|
-v, --version output version information and exit\\n"
|
|
}
|
|
|
|
vprintf() {
|
|
if [ "${verbose}" ]; then
|
|
printf "%s\\n" "$@"
|
|
fi
|
|
}
|
|
|
|
OPTS=$(getopt -o d:r:Vhv --long directory:,restore:,verbose,help,version -n 'parse-options' -- "$@")
|
|
|
|
while true; do
|
|
case "$1" in
|
|
-v | --version ) print_version && exit;;
|
|
-h | --help ) print_help && exit;;
|
|
-V | --verbose ) verbose=true; shift;;
|
|
-r | --restore ) restore=true && old_state_file="$2"; shift 2;;
|
|
-d | --directory ) music_dir="$2"; shift 2;;
|
|
-- ) shift; break;;
|
|
* ) print_usage; exit;;
|
|
esac
|
|
if [ "$1" == "" ]; then
|
|
break
|
|
fi
|
|
done
|
|
if [ -z "${music_dir}" ]; then
|
|
print_usage
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -d "${music_dir}" ]; then
|
|
printf "%s doesn't exist!\\n" "${music_dir}"
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -w "${music_dir}" ]; then
|
|
printf "No permissions to write in %s !\\n" "${music_dir}"
|
|
exit 1
|
|
fi
|
|
|
|
state_dir="${music_dir}/_STATES_"
|
|
if [ ! -d "${state_dir}" ]; then
|
|
vprintf "Creating: ${state_dir}"
|
|
mkdir "${state_dir}"
|
|
fi
|
|
|
|
new_state_file="${state_dir}/$(date +'%Y.%m.%d_%H:%M:%S')"
|
|
vprintf "Generating state file: ${new_state_file}"
|
|
touch "${new_state_file}"
|
|
|
|
if [ $restore ]; then
|
|
if [ ! -f "${old_state_file}" ]; then
|
|
printf "No such STATE_FILE: %s !\\n" "${old_state_file}"
|
|
rm "${new_state_file}"
|
|
exit 1
|
|
fi
|
|
IFS=$'\t'
|
|
while read -r old current
|
|
do
|
|
vprintf "mv \"${music_dir}/${current}\" \"${music_dir}/${old}\""
|
|
mv "${music_dir}/${current}" "${music_dir}/${old}"
|
|
printf "%s\\t%s\\n" "${old}" "${current}" >> "${new_state_file}"
|
|
done < "${old_state_file}"
|
|
exit
|
|
fi
|
|
|
|
vprintf "Generating mp3 file list..."
|
|
cd "${music_dir}" || return
|
|
IFS=$'\n'
|
|
list=$(find . -type f -iname "*.mp3" -printf '%P\n')
|
|
cd - 1>/dev/null || return
|
|
|
|
for i in ${list} ; do
|
|
new_name="$(head -c 500 /dev/urandom | tr -dc 'a-zA-Z0-9_-' | fold -w 30 | head -n 1).mp3"
|
|
vprintf "mv \"${music_dir}/$i\" \"${music_dir}/${new_name}\""
|
|
mv "${music_dir}/${i}" "${music_dir}/${new_name}"
|
|
printf "%s\\t%s\\n" "${i}" "${new_name}" >> "${new_state_file}"
|
|
done
|
|
vprintf "Done!"
|
|
|