Added some features

* Set temp-folder
* Better way of handling style without -d or -s
* Added progress to converting and syncing
This commit is contained in:
Jeroen De Meerleer 2018-12-01 00:43:17 +01:00
parent ead372dd65
commit ad6bc38c02
2 changed files with 51 additions and 26 deletions

View File

@ -6,7 +6,7 @@ Script to sync music from one folder to another
## Usage: ## Usage:
``` ```
music-sync <options> -s|--source <source> -d|--dest <destination> music-sync <options> -s|--source <source> -d|--dest <destination> -t|--temp <folder>
music-sync <options> <source> <destination> music-sync <options> <source> <destination>
Syncronises music from one folder to another. Syncronises music from one folder to another.
@ -14,17 +14,18 @@ Syncronises music from one folder to another.
Options: Options:
-s, --source <source> The source folder of the music -s, --source <source> The source folder of the music
-d, --dest <destination> The destionation folder of the music -d, --dest <destination> The destionation folder of the music
-v, --verbose <0-6> Set log level -t, --temp <folder> The temporary cache for converted files (default: /tmp/converted)
-v, --verbose <0-6> Set log level (default: 2)
-h, --help Display this help text -h, --help Display this help text
Log levels: Log levels:
0 | Verbose 0 | Verbose
1 | Debug 1 | Debug
2 | Info (Default) 2 | Info
3 | Warning 3 | Warning
4 | Error 4 | Error
5 | Fatal 5 | Fatal
6 | No logging 6 | Silence
Exit Codes: Exit Codes:
1 Dependencies not met 1 Dependencies not met

View File

@ -7,6 +7,7 @@ source="-"
dest="-" dest="-"
verbose=2 verbose=2
help=false help=false
temp="/tmp/converted"
CheckDeps() { CheckDeps() {
# Check getopt # Check getopt
@ -31,13 +32,9 @@ CheckDeps() {
GetOptions() { GetOptions() {
# https://stackoverflow.com/a/29754866 # https://stackoverflow.com/a/29754866
# Parsing style without -s or -d OPTIONS=s:d:t:v::h
source=${*: -2:1} LONGOPTS=source:,dest:,temp:,verbose::,help
dest=${*: -1:1}
OPTIONS=s:d:v::h
LONGOPTS=source:,dest:,verbose::,help
# -use ! and PIPESTATUS to get exit code with errexit set # -use ! and PIPESTATUS to get exit code with errexit set
# -temporarily store output to be able to check for errors # -temporarily store output to be able to check for errors
@ -77,6 +74,10 @@ GetOptions() {
dest="$2" dest="$2"
shift 2 shift 2
;; ;;
-t|--temp)
temp="$2"
shift 2
;;
--) --)
shift shift
break break
@ -88,7 +89,15 @@ GetOptions() {
esac esac
done 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 help=true
fi fi
} }
@ -104,17 +113,18 @@ Usage() {
echo "Options:" echo "Options:"
echo " -s, --source <source> The source folder of the music" echo " -s, --source <source> The source folder of the music"
echo " -d, --dest <destination> The destionation folder of the music" echo " -d, --dest <destination> The destionation folder of the music"
echo " -v, --verbose <0-6> Set log level" echo " -t, --temp <folder> 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 " -h, --help Display this help text"
echo "" echo ""
echo "Log levels:" echo "Log levels:"
echo " 0 | Verbose" echo " 0 | Verbose"
echo " 1 | Debug" echo " 1 | Debug"
echo " 2 | Info (Default)" echo " 2 | Info"
echo " 3 | Warning" echo " 3 | Warning"
echo " 4 | Error" echo " 4 | Error"
echo " 5 | Fatal" echo " 5 | Fatal"
echo " 6 | No logging" echo " 6 | Silence"
echo "" echo ""
echo "Exit Codes:" echo "Exit Codes:"
echo " 1 Dependencies not met" echo " 1 Dependencies not met"
@ -173,21 +183,28 @@ CreateFileList() {
} }
ConvertFiles() { ConvertFiles() {
mkdir -p /tmp/converted curline=0
percentage=0
while read -r line while read -r line
do do
if [[ ! -f "${source}/$file" ]]; then if [[ ! -f "${source}/$line" ]]; then
VerboseOutput 5 "Source-file ${source}/$file Unreachable" VerboseOutput 5 "Source-file ${source}/$line Unreachable"
ExecTime ExecTime
exit 3 exit 3
fi 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" VerboseOutput 1 "Converting: $line"
if [[ "/tmp/converted/$line" = */* ]]; then VerboseOutput 2 "Progress: $curline / $total (${percentage%00}%) Step 1 of 2"
mkdir -p "/tmp/converted/${line%/*}";
if [[ "$temp/$line" = */* ]]; then
mkdir -p "$temp/${line%/*}";
fi; fi;
if [[ ! -f "/tmp/converted/$line" || "${source}/$file" -nt "/tmp/converted/$line" ]]; then if [[ ! -f "$temp/$line" || "${source}/$line" -nt "$temp/$line" ]]; then
lame -b 192 $source/$line /tmp/converted/$line 1>/dev/null 2>/dev/null lame -b 192 $source/$line $temp/$line 1>/dev/null 2>/dev/null
VerboseOutput 2 "Converted: $line" VerboseOutput 2 "Converted: $line"
else else
VerboseOutput 3 "$line already converted" VerboseOutput 3 "$line already converted"
@ -197,6 +214,8 @@ ConvertFiles() {
} }
CopyFiles() { CopyFiles() {
curline=0
percentage=0
while read -r line while read -r line
do do
if [[ ! -f "$dest" ]]; then if [[ ! -f "$dest" ]]; then
@ -206,16 +225,22 @@ CopyFiles() {
fi fi
if [[ ! -f "$line" ]]; then if [[ ! -f "$line" ]]; then
VerboseOutput 5 "Source-file ${source}/$file Unreachable" VerboseOutput 5 "Source-file ${temp}/$line Unreachable"
ExecTime ExecTime
exit 3 exit 3
fi 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 1 "Copying: $line"
VerboseOutput 2 "Progress: $curline / $total (${percentage%00}%) Step 2 of 2"
if [[ "$dest/$line" = */* ]]; then if [[ "$dest/$line" = */* ]]; then
mkdir -p "$dest/${line%/*}"; mkdir -p "$dest/${line%/*}";
fi; 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" done < "/tmp/music-sync-filelist"
VerboseOutput 2 "Copied: $line" VerboseOutput 2 "Copied: $line"
} }
@ -223,7 +248,6 @@ CopyFiles() {
CleanUp() { CleanUp() {
VerboseOutput 1 "Cleaning Up" VerboseOutput 1 "Cleaning Up"
rm "/tmp/music-sync-filelist" rm "/tmp/music-sync-filelist"
rm -rf "/tmp/converted"
VerboseOutput 1 "Done" VerboseOutput 1 "Done"
} }
@ -250,4 +274,4 @@ fi
ConvertFiles ConvertFiles
CopyFiles CopyFiles
CleanUp CleanUp
ExecTime ExecTime