From b2df304ba8e3ca2a728f28f12050c06fca10b9b5 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 10:48:04 +0100 Subject: [PATCH 01/15] Added parsing of options --- music-sync.sh | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100755 music-sync.sh diff --git a/music-sync.sh b/music-sync.sh new file mode 100755 index 0000000..7450dc8 --- /dev/null +++ b/music-sync.sh @@ -0,0 +1,81 @@ +#!/bin/bash + +# saner programming env: these switches turn some bugs into errors +set -o errexit -o pipefail -o noclobber -o nounset +source="-" +dest="-" +verbose=true +help=false + +GetOptions() { + + # https://stackoverflow.com/a/29754866 + ! getopt --test > /dev/null + if [[ ${PIPESTATUS[0]} -ne 4 ]]; then + VerboseOutput "Fatal" "\`getopt --test\` failed" + echo "Sorry, It seems that your shell is not supported" + echo "If you're using mac or another unix-like system, please install GNU getopt" + exit 1 + fi + + OPTIONS=s:d:vh + LONGOPTS=source:,dest:,verbose,help + + # -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 + help=true + Usage + exit 2 + fi + + # read getopt’s output this way to handle the quoting right: + eval set -- "$PARSED" + + verbose=false + # now enjoy the options in order and nicely split until we see -- + while true; do + case "$1" in + -v|--verbose) + verbose=true + shift + ;; + -h|--help) + help=true + echo "test" + shift + ;; + -s|--source) + source="$2" + shift 2 + ;; + -d|--dest) + dest="$2" + shift 2 + ;; + --) + shift + break + ;; + *) + echo "Programming error" + return 3 + ;; + esac + done + +} + + +VerboseOutput() { + if [[ $verbose ]]; then + echo "[$1] $2" >&2 + fi +} + +GetOptions $@ From f9e4b610724a8c50177d87e94195639b71d0a2b8 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 10:58:16 +0100 Subject: [PATCH 02/15] added usage text --- music-sync.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/music-sync.sh b/music-sync.sh index 7450dc8..8e01bfb 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -71,6 +71,18 @@ GetOptions() { } +Usage() { + echo "Usage:" + echo "music-sync -s|--source -d|--dest " + echo "" + echo "Syncronises music from one folder to another." + echo "" + echo "Options:" + echo " -s, --source The source folder of the music" + echo " -d, --dest The destionation folder of the music" + echo " -v, --verbose Enable verbose output" + echo " -h, --help Display this help text" +} VerboseOutput() { if [[ $verbose ]]; then @@ -79,3 +91,6 @@ VerboseOutput() { } GetOptions $@ +if [[ $help ]]; then + Usage +fi From ad75ff4e281d6a08141b2168b1d9356e43f5d78c Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 11:05:31 +0100 Subject: [PATCH 03/15] Cleanup --- music-sync.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/music-sync.sh b/music-sync.sh index 8e01bfb..4e5f15c 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -29,7 +29,6 @@ GetOptions() { if [[ ${PIPESTATUS[0]} -ne 0 ]]; then # e.g. return value is 1 # then getopt has complained about wrong arguments to stdout - help=true Usage exit 2 fi @@ -47,7 +46,6 @@ GetOptions() { ;; -h|--help) help=true - echo "test" shift ;; -s|--source) @@ -82,15 +80,16 @@ Usage() { echo " -d, --dest The destionation folder of the music" echo " -v, --verbose Enable verbose output" echo " -h, --help Display this help text" + echo "" } VerboseOutput() { - if [[ $verbose ]]; then + if [[ "$verbose" = true ]]; then echo "[$1] $2" >&2 fi } GetOptions $@ -if [[ $help ]]; then +if [[ "$help" == true ]]; then Usage fi From 594d6fb1570ae4e07e78dbd0611eeccae49719f6 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 11:27:32 +0100 Subject: [PATCH 04/15] Added style without -s and -d --- music-sync.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/music-sync.sh b/music-sync.sh index 4e5f15c..d36dd71 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -17,6 +17,10 @@ GetOptions() { echo "If you're using mac or another unix-like system, please install GNU getopt" exit 1 fi + + # Parsing style without -s or -d + source=${*: -2:1} + dest=${*: -1:1} OPTIONS=s:d:vh LONGOPTS=source:,dest:,verbose,help @@ -70,8 +74,10 @@ GetOptions() { } Usage() { + echo "" echo "Usage:" - echo "music-sync -s|--source -d|--dest " + echo "music-sync -s|--source -d|--dest " + echo "music-sync " echo "" echo "Syncronises music from one folder to another." echo "" @@ -92,4 +98,5 @@ VerboseOutput() { GetOptions $@ if [[ "$help" == true ]]; then Usage + exit fi From 186070cd2490ecff8b6a33a57d96dcd891a2964e Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 13:02:34 +0100 Subject: [PATCH 05/15] Added CreateFileList --- music-sync.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/music-sync.sh b/music-sync.sh index d36dd71..28e6a60 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -95,8 +95,29 @@ VerboseOutput() { fi } +CreateFileList() { + # ${1} /mnt/hdd/Example-Artist/Example-Album + # ${2} /mnt/mtp/Example-Artist/Example-Album + # ${3} Example-Artist/Example-Album/ + IFS="" + sourcepath="${1}/*" + for file in $sourcepath; do + relfile="${file#"$1/"}" + if [[ -d "${1}/$relfile" ]]; then + newdir="${3}/$relfile" + newdir=${newdir#"/"} + VerboseOutput "Info" "Entering $newdir" + CreateFileList "${1}/$relfile" "${2}/$relfile" "$newdir" + elif [[ ! -f "${2}/$relfile" || "${2}/$relfile" -nt "${1}/$relfile" ]]; then + echo ${3}/$relfile >> /tmp/music-sync-filelist + VerboseOutput "Info" "Added: ${3}/${relfile}" + fi + done +} + GetOptions $@ if [[ "$help" == true ]]; then Usage exit fi +CreateFileList $source $dest "" \ No newline at end of file From 79d8e62d20deda031bf36ffeffb2dceb0ec9965f Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 13:28:29 +0100 Subject: [PATCH 06/15] Added conversion --- music-sync.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/music-sync.sh b/music-sync.sh index 28e6a60..459627b 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -115,9 +115,22 @@ CreateFileList() { done } +ConvertFiles() { + mkdir -p /tmp/converted + while read -r line + do + VerboseOutput "Info" "Converting $line" + if [[ "/tmp/converted/$line" = */* ]]; then + mkdir -p "/tmp/converted/${line%/*}"; + fi; + lame -q 0 -b 192 $source/$line /tmp/converted/$line 1>/dev/null 2>/dev/null + done < "/tmp/music-sync-filelist" +} + GetOptions $@ if [[ "$help" == true ]]; then Usage exit fi -CreateFileList $source $dest "" \ No newline at end of file +CreateFileList $source $dest "" +ConvertFiles \ No newline at end of file From 0e0b52cb3eccf845244ccef39010b7b10c568154 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 13:41:26 +0100 Subject: [PATCH 07/15] Added CopyFiles --- music-sync.sh | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/music-sync.sh b/music-sync.sh index 459627b..5ac6267 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -127,10 +127,22 @@ ConvertFiles() { done < "/tmp/music-sync-filelist" } +CopyFiles() { + while read -r line + do + VerboseOutput "Info" "Copy $line" + if [[ "$dest/$line" = */* ]]; then + mkdir -p "$dest/${line%/*}"; + fi; + cp -f $source/$line $dest/$line # 1>/dev/null 2>/dev/null + done < "/tmp/music-sync-filelist" +} + GetOptions $@ if [[ "$help" == true ]]; then Usage exit fi CreateFileList $source $dest "" -ConvertFiles \ No newline at end of file +ConvertFiles +CopyFiles \ No newline at end of file From 2dad9781ebe0a510d2d8c5f55cfbcf7138e12d87 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 13:42:00 +0100 Subject: [PATCH 08/15] Added cleanup --- music-sync.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/music-sync.sh b/music-sync.sh index 5ac6267..28787e6 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -138,6 +138,11 @@ CopyFiles() { done < "/tmp/music-sync-filelist" } +CleanUp() { + rm "/tmp/music-sync-filelist" + rm "/tmp/converted" +} + GetOptions $@ if [[ "$help" == true ]]; then Usage @@ -145,4 +150,5 @@ if [[ "$help" == true ]]; then fi CreateFileList $source $dest "" ConvertFiles -CopyFiles \ No newline at end of file +CopyFiles +CleanUp \ No newline at end of file From 143a1dfa81700d49dafd3428d4f1383d25baaad9 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 13:43:21 +0100 Subject: [PATCH 09/15] Added more verbosity --- music-sync.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/music-sync.sh b/music-sync.sh index 28787e6..2f533ff 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -124,6 +124,7 @@ ConvertFiles() { mkdir -p "/tmp/converted/${line%/*}"; fi; lame -q 0 -b 192 $source/$line /tmp/converted/$line 1>/dev/null 2>/dev/null + VerboseOutput "Info" "Converted $line" done < "/tmp/music-sync-filelist" } @@ -136,11 +137,14 @@ CopyFiles() { fi; cp -f $source/$line $dest/$line # 1>/dev/null 2>/dev/null done < "/tmp/music-sync-filelist" + VerboseOutput "Info" "Copied $line" } CleanUp() { + VerboseOutput "Info" "Cleaning Up" rm "/tmp/music-sync-filelist" rm "/tmp/converted" + VerboseOutput "Info" "Done" } GetOptions $@ From 8a52e8dea45df523fdbf85e94084c45f25607191 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 13:45:34 +0100 Subject: [PATCH 10/15] Recursive cleanup --- music-sync.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/music-sync.sh b/music-sync.sh index 2f533ff..1699013 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -143,7 +143,7 @@ CopyFiles() { CleanUp() { VerboseOutput "Info" "Cleaning Up" rm "/tmp/music-sync-filelist" - rm "/tmp/converted" + rm -rf "/tmp/converted" VerboseOutput "Info" "Done" } From 5924c0fecee56b64c673cc25a83a2a42258eb5d3 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 13:49:53 +0100 Subject: [PATCH 11/15] Solved some bugs --- music-sync.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/music-sync.sh b/music-sync.sh index 1699013..4283ebb 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -108,7 +108,7 @@ CreateFileList() { newdir=${newdir#"/"} VerboseOutput "Info" "Entering $newdir" CreateFileList "${1}/$relfile" "${2}/$relfile" "$newdir" - elif [[ ! -f "${2}/$relfile" || "${2}/$relfile" -nt "${1}/$relfile" ]]; then + elif [[ ! -f "${2}/$relfile" || "${1}/$relfile" -nt "${2}/$relfile" ]]; then echo ${3}/$relfile >> /tmp/music-sync-filelist VerboseOutput "Info" "Added: ${3}/${relfile}" fi @@ -153,6 +153,10 @@ if [[ "$help" == true ]]; then exit fi CreateFileList $source $dest "" +if [[ ! -f /tmp/music-sync-filelist ]]; then + VerboseOutput "Info" "Nothing to do!" + exit 0 +fi ConvertFiles CopyFiles CleanUp \ No newline at end of file From 3cf5414dc09117c8ee66a5c012df16ab3ed7bcb5 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 13:57:50 +0100 Subject: [PATCH 12/15] A little faster --- music-sync.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/music-sync.sh b/music-sync.sh index 4283ebb..b33f835 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -123,7 +123,7 @@ ConvertFiles() { if [[ "/tmp/converted/$line" = */* ]]; then mkdir -p "/tmp/converted/${line%/*}"; fi; - lame -q 0 -b 192 $source/$line /tmp/converted/$line 1>/dev/null 2>/dev/null + lame -b 192 $source/$line /tmp/converted/$line 1>/dev/null 2>/dev/null VerboseOutput "Info" "Converted $line" done < "/tmp/music-sync-filelist" } From 2ed4a037ff6b3b4c3ab0bf8f7ae3a2d02cc75c62 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 14:42:20 +0100 Subject: [PATCH 13/15] Showing help if no parameter is given --- music-sync.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/music-sync.sh b/music-sync.sh index b33f835..ed6b4ab 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -71,6 +71,9 @@ GetOptions() { esac done + if [[ $dest == "" ]] || [[ $source == "" ]]; then + help=true + fi } Usage() { @@ -148,7 +151,7 @@ CleanUp() { } GetOptions $@ -if [[ "$help" == true ]]; then +if [[ $help == true ]]; then Usage exit fi From 6994bdbbb01d0b1cc46702bfe29aefe3a5c6a253 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 14:45:25 +0100 Subject: [PATCH 14/15] Updated readme --- README.MD | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.MD b/README.MD index b4f36da..83ec5f5 100644 --- a/README.MD +++ b/README.MD @@ -2,3 +2,27 @@ (c) 2018 Jeroen De Meerleer Script to sync music from one folder to another + +## Usage: + +``` +music-sync -s|--source -d|--dest +music-sync + +Syncronises music from one folder to another. + +Options: + -s, --source The source folder of the music + -d, --dest The destionation folder of the music + -v, --verbose Enable verbose output + -h, --help Display this help text +``` + +## Licence +Copyright 2018 Jeroen De Meerleer + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. From ce8dd10596916115ab0450b3772e97733dc3c1e7 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 30 Nov 2018 14:52:51 +0100 Subject: [PATCH 15/15] Oops --- music-sync.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/music-sync.sh b/music-sync.sh index ed6b4ab..ff117a8 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -138,7 +138,7 @@ CopyFiles() { if [[ "$dest/$line" = */* ]]; then mkdir -p "$dest/${line%/*}"; fi; - cp -f $source/$line $dest/$line # 1>/dev/null 2>/dev/null + cp -f $source/$line $dest/$line 1>/dev/null 2>/dev/null done < "/tmp/music-sync-filelist" VerboseOutput "Info" "Copied $line" }