From ad6bc38c0231f2020ff58003d073fb037b1a4a3d Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 1 Dec 2018 00:43:17 +0100 Subject: [PATCH] Added some features * Set temp-folder * Better way of handling style without -d or -s * Added progress to converting and syncing --- README.MD | 9 ++++--- music-sync.sh | 68 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/README.MD b/README.MD index 3be5cda..0e9cdbb 100644 --- a/README.MD +++ b/README.MD @@ -6,7 +6,7 @@ Script to sync music from one folder to another ## Usage: ``` -music-sync -s|--source -d|--dest +music-sync -s|--source -d|--dest -t|--temp music-sync Syncronises music from one folder to another. @@ -14,17 +14,18 @@ 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 <0-6> Set log level + -t, --temp The temporary cache for converted files (default: /tmp/converted) + -v, --verbose <0-6> Set log level (default: 2) -h, --help Display this help text Log levels: 0 | Verbose 1 | Debug - 2 | Info (Default) + 2 | Info 3 | Warning 4 | Error 5 | Fatal - 6 | No logging + 6 | Silence Exit Codes: 1 Dependencies not met diff --git a/music-sync.sh b/music-sync.sh index c76f4eb..2ba2a45 100755 --- a/music-sync.sh +++ b/music-sync.sh @@ -7,6 +7,7 @@ source="-" dest="-" verbose=2 help=false +temp="/tmp/converted" CheckDeps() { # Check getopt @@ -31,13 +32,9 @@ CheckDeps() { GetOptions() { - # https://stackoverflow.com/a/29754866 - # Parsing style without -s or -d - source=${*: -2:1} - dest=${*: -1:1} - - OPTIONS=s:d:v::h - LONGOPTS=source:,dest:,verbose::,help + # https://stackoverflow.com/a/29754866 + OPTIONS=s:d:t:v::h + LONGOPTS=source:,dest:,temp:,verbose::,help # -use ! and PIPESTATUS to get exit code with errexit set # -temporarily store output to be able to check for errors @@ -77,6 +74,10 @@ GetOptions() { dest="$2" shift 2 ;; + -t|--temp) + temp="$2" + shift 2 + ;; --) shift break @@ -88,7 +89,15 @@ GetOptions() { esac done - if [[ $dest == "" ]] || [[ $source == "" ]]; then + if [[ ! -z ${1+x} ]]; then + source=$1 + fi + + if [[ ! -z ${2+x} ]]; then + dest=$2 + fi + + if [[ $dest == "-" ]] || [[ $source == "-" ]]; then help=true fi } @@ -104,17 +113,18 @@ Usage() { echo "Options:" echo " -s, --source The source folder of the music" echo " -d, --dest The destionation folder of the music" - echo " -v, --verbose <0-6> Set log level" + echo " -t, --temp The temporary cache for converted files (default: /tmp/converted)" + echo " -v, --verbose <0-6> Set log level (default: 2)" echo " -h, --help Display this help text" echo "" echo "Log levels:" echo " 0 | Verbose" echo " 1 | Debug" - echo " 2 | Info (Default)" + echo " 2 | Info" echo " 3 | Warning" echo " 4 | Error" echo " 5 | Fatal" - echo " 6 | No logging" + echo " 6 | Silence" echo "" echo "Exit Codes:" echo " 1 Dependencies not met" @@ -173,21 +183,28 @@ CreateFileList() { } ConvertFiles() { - mkdir -p /tmp/converted + curline=0 + percentage=0 while read -r line do - if [[ ! -f "${source}/$file" ]]; then - VerboseOutput 5 "Source-file ${source}/$file Unreachable" + if [[ ! -f "${source}/$line" ]]; then + VerboseOutput 5 "Source-file ${source}/$line Unreachable" ExecTime exit 3 fi + curline=$(expr ${curline} + 1) + total=$(cat /tmp/music-sync-filelist | wc -l) + percentage=$(echo "scale=4;${curline}/${total}" | bc) + percentage=$(echo "scale=2;${percentage}*100" | bc) VerboseOutput 1 "Converting: $line" - if [[ "/tmp/converted/$line" = */* ]]; then - mkdir -p "/tmp/converted/${line%/*}"; + VerboseOutput 2 "Progress: $curline / $total (${percentage%00}%) Step 1 of 2" + + if [[ "$temp/$line" = */* ]]; then + mkdir -p "$temp/${line%/*}"; fi; - if [[ ! -f "/tmp/converted/$line" || "${source}/$file" -nt "/tmp/converted/$line" ]]; then - lame -b 192 $source/$line /tmp/converted/$line 1>/dev/null 2>/dev/null + if [[ ! -f "$temp/$line" || "${source}/$line" -nt "$temp/$line" ]]; then + lame -b 192 $source/$line $temp/$line 1>/dev/null 2>/dev/null VerboseOutput 2 "Converted: $line" else VerboseOutput 3 "$line already converted" @@ -197,6 +214,8 @@ ConvertFiles() { } CopyFiles() { + curline=0 + percentage=0 while read -r line do if [[ ! -f "$dest" ]]; then @@ -206,16 +225,22 @@ CopyFiles() { fi if [[ ! -f "$line" ]]; then - VerboseOutput 5 "Source-file ${source}/$file Unreachable" + VerboseOutput 5 "Source-file ${temp}/$line Unreachable" ExecTime exit 3 fi + curline=$(expr ${curline} + 1) + total=$(cat /tmp/music-sync-filelist | wc -l) + percentage=$(echo "scale=4;${curline}/${total}" | bc) + percentage=$(echo "scale=2;${percentage}*100" | bc) VerboseOutput 1 "Copying: $line" + VerboseOutput 2 "Progress: $curline / $total (${percentage%00}%) Step 2 of 2" + if [[ "$dest/$line" = */* ]]; then mkdir -p "$dest/${line%/*}"; fi; - cp -f $source/$line $dest/$line 1>/dev/null 2>/dev/null + cp -f $temp/$line $dest/$line 1>/dev/null 2>/dev/null done < "/tmp/music-sync-filelist" VerboseOutput 2 "Copied: $line" } @@ -223,7 +248,6 @@ CopyFiles() { CleanUp() { VerboseOutput 1 "Cleaning Up" rm "/tmp/music-sync-filelist" - rm -rf "/tmp/converted" VerboseOutput 1 "Done" } @@ -250,4 +274,4 @@ fi ConvertFiles CopyFiles CleanUp -ExecTime \ No newline at end of file +ExecTime