-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcvcbulkcreate
executable file
·160 lines (133 loc) · 4.9 KB
/
cvcbulkcreate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
################################################################################
# Loading configuration
################################################################################
CVCINTERNAL_SCRIPTROOT=$(dirname "$0")
source "${CVCINTERNAL_SCRIPTROOT}/config"
################################################################################
# Documentation
################################################################################
# Return code reference
# 1 Syntax error in invocation
# 2 Missing argument
# 3 Target directory does not exist
# 16 Internal error
# Show usage
function usage {
echo "This script runs cvccommit for all the audio files of a directory, thus"
echo "allowing to quickly create CVC files for whole albums."
echo ""
echo "Usage: cvcbulkcreate [-f] [-h] [-t <template>] [-v] <directory>"
echo ""
echo "Options:"
echo "-f Overwrite current cvc file."
echo "-h Show this message."
echo "-t <template> The name of the CVC template to use, without the .cvt extension."
echo " CVC templates are used to prestage CVC files with static or"
echo " dynamic content".
echo "-v Verbose mode."
echo ""
echo "<directory> A directory containing audio files."
}
################################################################################
# Option and argument parsing
################################################################################
# Default settings
extraArgs=""
# Calling option parser
while getopts ":fht:v" option; do
case "${option}" in
# -f: Force mode
f)
extraArgs+="-f "
;;
# -h: Help
h)
usage
exit
;;
# -t: CVC template
t)
extraArgs+="-t ${OPTARG}"
;;
# -v: Verbose mode
v)
VERBOSE=1
extraArgs+="-v "
;;
# Syntax error
\?)
>&2 echo "ERROR: Invalid option: -${OPTARG}"
usage
[[ "${BASH_SOURCE[0]}" != "${0}" ]] && return 1 || exit 1
;;
# Syntax error
:)
>&2 echo "ERROR: Option -${OPTARG} requires an argument."
usage
[[ "${BASH_SOURCE[0]}" != "${0}" ]] && return 1 || exit 1
;;
# Any other value is an internal error
*)
>&2 echo "ERROR: Internal getopts error."
[[ "${BASH_SOURCE[0]}" != "${0}" ]] && return 16 || exit 16
;;
esac
done
shift $((OPTIND-1))
# Checking whether a directory was supplied as an argument.
if [ -z "$1" ]
then
>&2 echo "ERROR: No directory specified."
usage
[[ "${BASH_SOURCE[0]}" != "${0}" ]] && return 2 || exit 2
fi
################################################################################
# Additional checks
################################################################################
# Abstracting supplied arguments. We remove the trailing /
targetDirectory="${1%/}"
# Checking whether the supplied directory does exist
if [ ! -d "${targetDirectory}" ]
then
>&2 echo "ERROR: Directory '${targetDirectory}' does not exist."
[[ "${BASH_SOURCE[0]}" != "${0}" ]] && return 3 || exit 3
fi
################################################################################
# Detecting tracklist properties
################################################################################
# Detect the total number of discs, the total number of tracks per disc,
# and the total number of tracks.
################################################################################
# Creating CVC files
################################################################################
# Initializing file counter
fileCount=0
for audioFile in "${targetDirectory}/"*.${CFG_FileExtensions_Audio}
do
((fileCount+=1))
[ "${VERBOSE}" ] && echo "Processing file #${fileCount}: ${audioFile}..."
# Setting up useful variables
audioFileBasename=$(basename "${audioFile}" ".${CFG_FileExtensions_Audio}")
# Detecting disk number
discNumberPrefix=$(echo "${audioFileBasename}" | grep -oE "^[0-9]?[0-9]-")
if [ "${discNumberPrefix}" ]
then
discNumber="${discNumberPrefix%-}"
[ "${VERBOSE}" ] && echo "Detected disc number: ${discNumber}."
else
discNumber=1
[ "${VERBOSE}" ] && echo "Detected disc number: None. Assuming ${discNumber}."
fi
# Detecting track number
trackNumberPrefix=$(echo "${audioFileBasename#${discNumberPrefix}}" | grep -oE "^[0-9]?[0-9]")
if [ "${trackNumberPrefix}" ]
then
trackNumber="${trackNumberPrefix}"
[ "${VERBOSE}" ] && echo "Detected track number: ${trackNumber}."
else
[ "${VERBOSE}" ] && echo "Detected disc number: None. Assuming none."
fi
# Calling cvccreate
"${CFG_CVCTools_CVCCreate}" ${extraArgs} "${audioFile}"
done