bash-music-sync/music-sync.sh

573 lines
15 KiB
Bash
Raw Normal View History

2018-11-30 10:48:04 +01:00
#!/bin/bash
2020-02-12 00:21:50 +01:00
#
#/
#/ Usage:
#/ music-sync <options> -s|--source <source> -d|--dest <destination>
#/ music-sync <options> <source> <destination>
#/
#/ Syncronises music from one folder to another.
#/
#/ Options:
2020-02-16 02:18:07 +01:00
#/ -s, --source=<source> The source folder of the music
#/ -d, --dest=<destination> The destination folder of the music
#/ -t, --temp=<folder> The temporary cache for converted files (default: /tmp/converted)
#/ -c, --convert=<bitrate> Convert files to a given bitrate in kbps before syncing (default: 192)
#/ -a, --resize-art=<width> Resize album-art before syncing (default width: 200)
#/ -j, --jobs=<nproc> Number of processes to use in multi-threading (default: nproc - 2)
#/ -v, --verbose=<0-6> Set log level (default: 2)
2020-02-12 00:21:50 +01:00
#/ -h, --help Display this help text
2020-02-16 02:18:07 +01:00
#/ -p, --playlists Enable playlist sync
#/ -m, --mapping=DIR1,DIR2 Add mappings for playlists from DIR1 to DIR2
#/ -w, --windows-format Use crlf and backslashes for playlists
2020-02-12 00:21:50 +01:00
#/
#/ Log levels:
#/ 0 | Verbose
#/ 1 | Debug
#/ 2 | Info
#/ 3 | Warning
#/ 4 | Error
#/ 5 | Fatal
#/ 6 | Silence
#/
#/ Exit Codes:
#/ 1 Dependencies not met
#/ 2 Invalid Argument
#/ 3 Source Unreachable
#/ 4 Destination Unreachable
#/ 5 Command failed
#/
2018-11-30 10:48:04 +01:00
2018-11-30 16:26:58 +01:00
begin=$(date +"%s")
2018-11-30 10:48:04 +01:00
# saner programming env: these switches turn some bugs into errors
set -o errexit -o pipefail -o noclobber -o nounset
source="-"
dest="-"
2018-12-01 18:40:13 +01:00
convert=false
2018-11-30 16:12:50 +01:00
verbose=2
2019-03-16 15:05:11 +01:00
bitrate=192
2018-11-30 10:48:04 +01:00
help=false
temp="/tmp/converted"
2018-12-05 15:37:27 +01:00
convertart=false
2019-03-16 17:32:39 +01:00
coverartsize=200
2020-02-12 13:58:57 +01:00
jobcount=$(expr $(nproc) - 2)
2020-02-12 00:13:48 +01:00
multithread=false
2020-02-12 00:21:50 +01:00
script_name=$(basename "${0}")
script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
2020-02-16 13:27:14 +01:00
mappingdest=''
mappingsrc=''
2020-02-16 02:18:07 +01:00
windowspaths=false
playlists=false
2018-11-30 10:48:04 +01:00
2018-11-30 17:50:37 +01:00
CheckDeps() {
if [[ $1 == 2 ]]; then
2018-12-01 18:40:13 +01:00
# Check getopt
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
VerboseOutput 5 "\`getopt --test\` failed"
VerboseOutput 5 "Sorry, It seems that your shell is not supported"
VerboseOutput 5 "If you're using MacOS or another unix-like system, please install GNU getopt"
ExecTime
exit 1
fi
2019-03-16 18:31:17 +01:00
VerboseOutput 0 "\`getopt --test\` succeeded"
2018-11-30 10:48:04 +01:00
fi
2018-11-30 17:50:37 +01:00
2018-12-01 18:40:13 +01:00
if [[ $1 == 1 ]]; then
2020-02-08 13:50:50 +01:00
# Check ffmpeg
if [[ $convert == true ]]; then
if [[ ! $(ffmpeg -h 2>/dev/null) ]]; then
VerboseOutput 5 "\`ffmpeg -h\` failed"
VerboseOutput 5 "Sorry, It seems that ffmpeg is not installed on your system"
VerboseOutput 5 "Please install ffmpeg from your repositories and make sure it is available in your \$PATH"
VerboseOutput 5 "Otherwise disable conversion"
ExecTime
exit 1
fi
VerboseOutput 0 "\`ffmpeg -h\` succeeded"
2020-02-08 13:50:50 +01:00
fi
2018-12-05 15:37:27 +01:00
# Check EyeD3
if [[ $convertart == true ]]; then
if [[ ! $(eyeD3 --version 2>/dev/null) ]]; then
VerboseOutput 5 "\`eyeD3 --version\` failed"
VerboseOutput 5 "Sorry, It seems that eyeD3 is not installed on your system"
VerboseOutput 5 "Please install eyeD3 from your repositories and make sure it is available in your \$PATH"
VerboseOutput 5 "Otherwise disable albumart conversion"
ExecTime
exit 1
fi
VerboseOutput 0 "\`eyeD3 --version\` succeeded"
2018-12-05 15:37:27 +01:00
fi
2019-03-16 18:31:17 +01:00
2019-03-16 14:39:42 +01:00
# Check ImageMagick
if [[ $convertart == true ]]; then
if [[ ! $(convert --version 2>/dev/null) ]]; then
VerboseOutput 5 "\`convert --version\` failed"
VerboseOutput 5 "Sorry, It seems that ImageMagick is not installed on your system"
VerboseOutput 5 "Please install ImageMagick from your repositories and make sure it is available in your \$PATH"
VerboseOutput 5 "Otherwise disable albumart conversion"
ExecTime
exit 1
fi
VerboseOutput 0 "\`convert --version\` succeeded"
2018-12-01 18:40:13 +01:00
fi
2020-02-12 00:13:48 +01:00
# Check Gnu parallel
if [[ $multithread == true ]]; then
if [[ ! $(parallel -h 2>/dev/null) ]]; then
VerboseOutput 5 "\`parallel -h\` failed"
VerboseOutput 5 "Sorry, It seems that parallel is not installed on your system"
2020-02-16 02:18:07 +01:00
VerboseOutput 5 "Please install gnu-parallel from your repositories and make sure it is available in your \$PATH"
VerboseOutput 5 "Otherwise disable multithreading"
ExecTime
exit 1
fi
2020-02-16 02:18:07 +01:00
VerboseOutput 0 "\`parallel -h\` succeeded"
fi
# Check playlist requirement
if [[ $playlists == true ]]; then
if [[ $windowspaths == true ]]; then
if [[ ! $(unix2dos --version 2>/dev/null) ]]; then
VerboseOutput 5 "\`unix2dos --version\` failed"
VerboseOutput 5 "Sorry, It seems that unix2dos is not installed on your system"
VerboseOutput 5 "Please install unix2dos from your repositories and make sure it is available in your \$PATH"
VerboseOutput 5 "Otherwise disable windows-format playlists"
ExecTime
exit 1
fi
fi
VerboseOutput 0 "\`unix2dos --version\` succeeded"
2020-02-12 00:13:48 +01:00
fi
2018-11-30 17:50:37 +01:00
fi
2019-03-16 18:31:17 +01:00
VerboseOutput 1 "Dependency test OK"
2018-11-30 17:50:37 +01:00
}
GetOptions() {
# https://stackoverflow.com/a/29754866
2020-02-16 02:18:07 +01:00
OPTIONS=s:d:t:c::a::v::hj::pwm:
LONGOPTS=source:,dest:,temp:,convert::,resize-art::,verbose::,help,jobs::,playlists,windows-format,mapping:
2018-12-05 15:37:27 +01:00
2018-11-30 10:48:04 +01:00
# -use ! and PIPESTATUS to get exit code with errexit set
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out “--options”)
# -pass arguments only via -- "$@" to separate them correctly
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
# e.g. return value is 1
# then getopt has complained about wrong arguments to stdout
Usage
exit 2
fi
2023-01-13 09:39:12 +01:00
# read getopt's output this way to handle the quoting right:
2018-11-30 10:48:04 +01:00
eval set -- "$PARSED"
# now enjoy the options in order and nicely split until we see --
while true; do
case "$1" in
-v|--verbose)
2018-11-30 16:12:50 +01:00
if [[ $2 != "" ]]; then
verbose=${2}
fi
2020-02-12 13:53:58 +01:00
shift 2
2018-11-30 10:48:04 +01:00
;;
-h|--help)
help=true
shift
;;
-s|--source)
2019-03-16 18:31:17 +01:00
VerboseOutput 0 "--source given."
VerboseOutput 1 "Source is ${2}"
2018-11-30 10:48:04 +01:00
source="$2"
shift 2
;;
-d|--dest)
2019-03-16 18:31:17 +01:00
VerboseOutput 0 "--dest given."
VerboseOutput 1 "Destination is ${2}"
2018-11-30 10:48:04 +01:00
dest="$2"
shift 2
;;
-t|--temp)
2019-03-16 18:31:17 +01:00
VerboseOutput 0 "--temp given"
VerboseOutput 1 "Tempfolder is ${2}"
temp="$2"
shift 2
;;
2018-12-01 18:40:13 +01:00
-c|--convert)
2019-03-16 18:31:17 +01:00
VerboseOutput 0 "--convert given"
2018-12-01 18:40:13 +01:00
convert=true
if [[ $2 != "" ]]; then
bitrate=${2}
fi
2019-03-16 18:31:17 +01:00
VerboseOutput 1 "Converted bitrate is ${bitrate}"
2020-02-12 13:53:58 +01:00
shift 2
2018-12-01 18:40:13 +01:00
;;
-a|--resize-art)
2019-03-16 18:31:17 +01:00
VerboseOutput 0 "--resize-art given"
2018-12-05 15:37:27 +01:00
convertart=true
2019-03-16 17:32:39 +01:00
if [[ $2 != "" ]]; then
coverartsize=${2}
fi
2020-02-12 13:53:58 +01:00
shift 2
2019-03-16 18:31:17 +01:00
VerboseOutput 1 "Album art will ${coverartsize}px wide"
2018-12-05 15:37:27 +01:00
;;
2020-02-12 00:13:48 +01:00
-j|--jobs)
VerboseOutput 0 "--jobs given"
multithread=true
if [[ $2 != "" ]]; then
jobcount=${2}
fi
2020-02-12 13:53:58 +01:00
shift 2
2020-02-12 13:58:57 +01:00
VerboseOutput 1 "Multithreading will use ${jobcount} threads"
2020-02-12 00:13:48 +01:00
;;
2020-02-16 02:18:07 +01:00
-p|--playlists)
VerboseOutput 0 "--playlists given"
playlists=true
shift
VerboseOutput 1 "Playlist sync enabled"
;;
-w|--windows-format)
VerboseOutput 0 "--windows given"
windowspaths=true
shift
VerboseOutput 1 "Windows format playlists enabled"
;;
-m|--mapping)
VerboseOutput 0 "--mapping given"
listmappingparam=(${2//,/ })
2020-02-16 13:27:14 +01:00
mappingsrc=${listmappingparam[0]:-""}
mappingdest=${listmappingparam[1]:-""}
2020-02-16 02:18:07 +01:00
shift 2
2020-02-16 13:27:14 +01:00
VerboseOutput 1 "Playlist mappings enabled source=${mappingsrc} dest=${mappingdest}"
2020-02-16 02:18:07 +01:00
;;
2018-11-30 10:48:04 +01:00
--)
shift
break
;;
*)
2018-11-30 16:56:15 +01:00
VerboseOutput 5 "Programming error"
2018-11-30 10:48:04 +01:00
return 3
;;
esac
done
2019-03-16 17:04:47 +01:00
if [[ ! -z "${1+x}" ]]; then
2019-03-16 18:31:17 +01:00
if [[ ${source} != "-" ]]; then
VerboseOutput 4 "Source provided twice. Continueing with ${1}"
fi
2018-12-05 15:42:44 +01:00
source="$1"
2019-03-16 18:31:17 +01:00
VerboseOutput 1 "Source is ${1}"
fi
2019-03-16 17:04:47 +01:00
if [[ ! -z "${2+x}" ]]; then
2019-03-16 18:31:17 +01:00
if [[ ${dest} != "-" ]]; then
VerboseOutput 4 "Destination provided twice. Continueing with ${2}"
fi
2018-12-05 15:42:44 +01:00
dest="$2"
2019-03-16 18:31:17 +01:00
VerboseOutput 1 "Destination is ${2}"
fi
if [[ $dest == "-" ]] || [[ $source == "-" ]]; then
2018-11-30 14:42:20 +01:00
help=true
fi
2019-03-16 18:31:17 +01:00
VerboseOutput 1 "Checks OK. Going on"
2018-11-30 10:48:04 +01:00
}
2018-11-30 10:58:16 +01:00
Usage() {
2020-02-12 00:21:50 +01:00
grep '^#/' "${script_dir}/${script_name}" | sed 's/^#\/\w*//'
2018-11-30 10:58:16 +01:00
}
2018-11-30 10:48:04 +01:00
VerboseOutput() {
2018-11-30 16:12:50 +01:00
level=""
if [[ $verbose -le $1 ]]; then
case "$1" in
0)
2018-11-30 16:56:15 +01:00
level="\033[1;36mVerbose\033[0m"
2018-11-30 16:12:50 +01:00
;;
1)
2018-11-30 16:56:15 +01:00
level="\033[1;34m Debug \033[0m"
2018-11-30 16:12:50 +01:00
;;
2)
2018-11-30 16:56:15 +01:00
level="\033[1;37m Info \033[0m"
2018-11-30 16:12:50 +01:00
;;
3)
2018-11-30 16:56:15 +01:00
level="\033[1;33mWarning\033[0m"
2018-11-30 16:12:50 +01:00
;;
4)
2018-11-30 16:56:15 +01:00
level="\033[1;31m Error \033[0m"
2018-11-30 16:12:50 +01:00
;;
5)
2018-11-30 16:56:15 +01:00
level="\033[1;30m Fatal \033[0m"
2018-11-30 16:12:50 +01:00
;;
esac
2018-11-30 16:56:15 +01:00
echo -e "[$level] $2" >&2
2018-11-30 10:48:04 +01:00
fi
}
2018-11-30 13:02:34 +01:00
CreateFileList() {
# ${1} /mnt/hdd/Example-Artist/Example-Album
# ${2} /mnt/mtp/Example-Artist/Example-Album
2018-11-30 14:57:08 +01:00
# ${3} Example-Artist/Example-Album
2018-11-30 13:02:34 +01:00
IFS=""
2018-12-01 09:38:03 +01:00
sourcepath="${1/\[/\\\[}/*"
sourcepath="${sourcepath/\]/\\\]}"
2018-11-30 13:02:34 +01:00
for file in $sourcepath; do
origfile="${file#"$1/"}"
2020-02-12 13:09:56 +01:00
relfile=$(echo ${origfile} | sed -e 's/\(\.\)*$//g' | tr -s ' ')
2020-02-09 14:42:20 +01:00
mp3file=$(echo "${relfile}" | sed -e 's/.flac/.mp3/')
2020-02-16 02:18:07 +01:00
VerboseOutput 0 "Checking ${3}${origfile}"
if [[ -d "${1}/$origfile" ]]; then
2019-03-16 18:31:17 +01:00
VerboseOutput 0 "${origfile} is folder"
2020-02-16 02:18:07 +01:00
newdir="${3}$origfile"
2018-11-30 13:02:34 +01:00
newdir=${newdir#"/"}
2018-11-30 16:12:50 +01:00
VerboseOutput 1 "Entering $newdir"
2020-02-16 02:18:07 +01:00
CreateFileList "${1}/$origfile/" "${2}/$relfile/" "$newdir/"
2020-02-09 14:42:20 +01:00
elif [[ "${1}/$origfile" != *".m3u" ]] && [[ "${1}/$origfile" -nt "${2}/$mp3file" ]]; then
2020-02-16 02:18:07 +01:00
echo ${3}$origfile >> /tmp/music-sync-filelist
2019-03-16 18:31:17 +01:00
VerboseOutput 0 "${origfile} is newer in source"
2020-02-16 02:18:07 +01:00
VerboseOutput 2 "Added: ${3}${origfile}"
2020-02-18 21:39:10 +01:00
elif [[ $playlists == true ]] && [[ "${1}/$origfile" == *".m3u" ]] && [[ "${1}/$origfile" -nt "${2}/$mp3file" ]]; then
2020-02-16 02:18:07 +01:00
echo ${3}$origfile >> /tmp/music-sync-filelist
VerboseOutput 0 "${origfile} is newer in source"
VerboseOutput 2 "Added playlist: ${3}${origfile}"
2019-03-16 18:31:17 +01:00
fi
2018-11-30 13:02:34 +01:00
done
}
2020-02-12 00:13:48 +01:00
Execute() {
curline=0
percentage=0
2020-02-08 13:50:50 +01:00
IFS="
";
2020-02-12 00:13:48 +01:00
if [[ $multithread == true ]]; then
VerboseOutput 3 "Running in parallel. Mind your load"
export -f ProcessFile
export -f ConvertFile
2020-02-16 11:05:39 +01:00
export -f ConvertPlaylist
2020-02-12 00:13:48 +01:00
export -f CopyFile
export -f VerboseOutput
export -f ExecTime
export dest
export source
export bitrate
export convert
export temp
2020-02-12 00:24:57 +01:00
export verbose
2020-02-12 00:13:48 +01:00
export coverartsize
export begin
2020-02-16 13:27:14 +01:00
export windowspaths
export mappingsrc
export mappingdest
2020-02-12 18:55:04 +01:00
parallel --jobs ${jobcount} --will-cite --line-buffer --arg-file "/tmp/music-sync-filelist" ProcessFile
2020-02-12 00:13:48 +01:00
else
for line in $(cat "/tmp/music-sync-filelist")
do
ProcessFile $line
done
fi
VerboseOutput 2 "Done!"
}
ProcessFile() {
line=${1}
2020-02-12 18:55:04 +01:00
curline="$(grep -n "$line" /tmp/music-sync-filelist | head -n 1 | cut -d: -f1)"
total=$(cat /tmp/music-sync-filelist | wc -l)
percentage=$(echo "scale=4;${curline}/${total}" | bc)
percentage=$(echo "scale=2;${percentage}*100" | bc)
2020-02-12 00:13:48 +01:00
VerboseOutput 2 "Current File: $line"
2020-02-12 18:55:04 +01:00
VerboseOutput 2 "Progress: $curline / $total (${percentage%00}%)"
2020-02-12 00:13:48 +01:00
if [[ $convert == true ]]; then
2020-02-16 02:18:07 +01:00
if [[ $line == *".m3u" ]]; then
ConvertPlaylist "$line"
else
ConvertFile "$line"
fi
2020-02-12 00:13:48 +01:00
fi
CopyFile "$line"
2018-11-30 13:28:29 +01:00
}
2020-02-16 02:18:07 +01:00
ConvertPlaylist() {
line=${1}
if [[ ! -f "${source}/$line" ]]; then
VerboseOutput 5 "Source-file ${source}/$line Unreachable"
ExecTime
exit 3
fi
if [[ ! -d $(dirname "$temp/$line") ]]; then
VerboseOutput 0 "Creating folder $temp/${line%/*}"
mkdir -p "$temp/${line%/*}";
fi;
if [[ -f "$temp/$line" ]]; then
2020-02-16 13:27:14 +01:00
rm "$temp/$line"
2020-02-16 02:18:07 +01:00
fi
VerboseOutput 0 "Creating playlist $temp/$line"
2020-02-16 13:27:14 +01:00
IFS="
"
2020-02-16 02:18:07 +01:00
for item in $(cat "$source/$line")
do
mp3item=$(echo $item | sed -e 's/\.*\//\//g'| tr -s ' ')
if [[ $convert == true ]]; then
mp3item=$(echo "$item" | sed -e 's/.flac/.mp3/')
else
mp3item=$(echo "$item")
fi
2020-02-16 13:27:14 +01:00
playline=$(echo "${mp3item}" | sed -e "s,$mappingsrc,$mappingdest,g")
2020-02-16 02:18:07 +01:00
if [[ $windowspaths == true ]]; then
2020-02-16 13:27:14 +01:00
playline=$(echo "${playline}" | sed -e 's,\/,\\,g')
fi
echo $playline >> $temp/$line
2020-02-16 02:18:07 +01:00
done
if [[ $windowspaths == true ]]; then
VerboseOutput 0 "Converting lf to crlf"
unix2dos $temp/$line 1>/dev/null 2>/dev/null
fi
VerboseOutput 1 "Converted: $line"
}
2020-02-12 00:13:48 +01:00
ConvertFile() {
line=${1}
if [[ ! -f "${source}/$line" ]]; then
VerboseOutput 5 "Source-file ${source}/$line Unreachable"
ExecTime
exit 3
fi
2018-11-30 17:36:54 +01:00
2020-02-16 02:18:07 +01:00
if [[ ! -d $(dirname "$temp/$line") ]]; then
2020-02-12 00:13:48 +01:00
VerboseOutput 0 "Creating folder $temp/${line%/*}"
mkdir -p "$temp/${line%/*}";
fi;
mp3line=$(echo "$line" | sed -e 's/.flac/.mp3/')
2020-02-16 02:18:07 +01:00
if [[ "${source}/$line" -nt "$temp/$mp3line" ]]; then
2020-02-12 00:13:48 +01:00
if [[ $line != *".mp3" ]]; then
VerboseOutput 3 "${line} will be converted to mp3-file ${mp3line}"
2020-02-09 14:42:20 +01:00
fi
2020-02-12 00:13:48 +01:00
VerboseOutput 0 "Converting MP3-file $line"
ffmpeg -y -i "$source/$line" -acodec libmp3lame -map_metadata 0 -id3v2_version 3 -b:a ${bitrate}k "$temp/${mp3line}" 1>/dev/null 2>/dev/null
if [[ $convertart == true ]]; then
VerboseOutput 0 "Creating folder $temp/${mp3line}-images/"
mkdir -p "$temp/${mp3line}-images/"
VerboseOutput 0 "Extracted albumart"
eyeD3 --write-images "$temp/${mp3line}-images/" "$temp/${mp3line}" 1>/dev/null 2>/dev/null
frontcovers=(${temp}/${mp3line}-images/FRONT_COVER.*)
if [ -e "$frontcovers" ]; then
VerboseOutput 0 "Converting albumart"
convert "$temp/${mp3line}-images/FRONT_COVER.*" -resize ${coverartsize}x${coverartsize} "$temp/${mp3line}-images/FRONT_COVER.jpg" 1>/dev/null 2>/dev/null
eyeD3 --remove-all-images "$temp/${mp3line}" 1>/dev/null 2>/dev/null
VerboseOutput 0 "Embedding albumart"
eyeD3 --add-image "$temp/${mp3line}-images/FRONT_COVER.jpg:FRONT_COVER" "$temp/${mp3line}" 1>/dev/null 2>/dev/null
VerboseOutput 1 "Converted cover art: ${mp3line}"
else
VerboseOutput 4 "No front cover art found for ${mp3line}"
fi
2018-11-30 17:36:54 +01:00
fi
2020-02-12 00:13:48 +01:00
VerboseOutput 1 "Converted: $line"
else
VerboseOutput 3 "$line already converted"
fi;
}
CopyFile() {
line=${1}
if [[ ! -d "$dest" ]]; then
VerboseOutput 5 "Destination unreachable"
ExecTime
exit 4
fi
if [[ $convert == true ]]; then
mp3line=$(echo "$line" | sed -e 's/.flac/.mp3/')
else
mp3line=$(echo "$line")
fi
2018-11-30 17:36:54 +01:00
2020-02-12 00:13:48 +01:00
if [[ ! -f "${temp}/$mp3line" ]]; then
VerboseOutput 5 "Source-file ${temp}/$mp3line Unreachable"
ExecTime
exit 3
fi
2018-12-01 11:13:30 +01:00
2020-02-12 13:09:56 +01:00
destline=$(echo $mp3line | sed -e 's/\.*\//\//g'| tr -s ' ')
2020-02-12 00:13:48 +01:00
VerboseOutput 1 "Copying: $line"
2020-02-16 02:18:07 +01:00
if [[ ! -d $(dirname "$dest/$line") ]]; then
2020-02-12 00:13:48 +01:00
VerboseOutput 0 "Creating folder $dest/${line%/*}"
mkdir -p "$dest/${destline%/*}";
fi;
2018-12-01 11:13:30 +01:00
2020-02-12 13:09:56 +01:00
cp -fv "$temp/$mp3line" "$dest/${destline}" 1>/dev/null 2>/dev/null
2018-12-01 11:19:06 +01:00
2020-02-12 00:13:48 +01:00
VerboseOutput 1 "Copied: $line"
2018-11-30 13:41:26 +01:00
}
2020-02-12 00:13:48 +01:00
2018-11-30 13:42:00 +01:00
CleanUp() {
2018-11-30 16:12:50 +01:00
VerboseOutput 1 "Cleaning Up"
2018-12-01 11:13:30 +01:00
if [[ -f /tmp/music-sync-filelist ]]; then
2019-03-16 18:31:17 +01:00
VerboseOutput 1 "Removing filelist"
2018-12-01 11:13:30 +01:00
rm "/tmp/music-sync-filelist"
fi
2018-11-30 16:12:50 +01:00
VerboseOutput 1 "Done"
2018-11-30 13:42:00 +01:00
}
2018-11-30 17:36:54 +01:00
ExecTime() {
termin=$(date +"%s")
difftimelps=$(($termin-$begin))
VerboseOutput 1 "$(($difftimelps / 60)) minutes and $(($difftimelps % 60)) seconds elapsed for Script Execution."
}
2018-12-01 11:13:30 +01:00
ErrorHandler() {
VerboseOutput 5 "Error while executing $1"
CleanUp
exit 5
}
ExitHandler() {
VerboseOutput 5 "Aborted"
CleanUp
exit 0
}
trap 'ExitHandler' SIGINT
trap 'ErrorHandler $BASH_COMMAND' ERR
CheckDeps 2
2018-11-30 10:48:04 +01:00
GetOptions $@
2018-12-01 18:40:13 +01:00
CheckDeps 1
2018-11-30 14:42:20 +01:00
if [[ $help == true ]]; then
2018-11-30 10:58:16 +01:00
Usage
2018-11-30 11:27:32 +01:00
exit
2018-11-30 10:58:16 +01:00
fi
2018-11-30 15:45:02 +01:00
if [[ -f /tmp/music-sync-filelist ]]; then
rm /tmp/music-sync-filelist
fi
VerboseOutput 2 "Scanning for new or updated files"
2018-11-30 13:28:29 +01:00
CreateFileList $source $dest ""
2018-11-30 13:49:53 +01:00
if [[ ! -f /tmp/music-sync-filelist ]]; then
2018-11-30 16:56:15 +01:00
VerboseOutput 2 "Nothing to do!"
2018-12-01 11:13:30 +01:00
CleanUp
2018-11-30 13:49:53 +01:00
exit 0
fi
if [[ $convert == true ]]; then
temp=${temp}/bitrate-${bitrate}
if [[ $convertart == true ]]; then
temp="${temp}/coverart-${coverartsize}"
else
temp="${temp}/coverart-original"
fi
VerboseOutput 2 "Conversion enabled. Using $temp as temp-folder"
else
2018-12-01 18:40:13 +01:00
if [[ $temp != "/tmp/converted" ]]; then
VerboseOutput 2 "Conversion not enabled. Ignoring temp folder"
2018-12-01 18:40:13 +01:00
fi
temp=$source
2020-02-12 00:13:48 +01:00
fi
Execute
2018-11-30 16:26:58 +01:00
CleanUp
ExecTime