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:
```
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>
Syncronises music from one folder to another.
@ -14,17 +14,18 @@ Syncronises music from one folder to another.
Options:
-s, --source <source> The source 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
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

View File

@ -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 <source> The source 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 ""
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
ExecTime