-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-inventory.sh.in
executable file
·379 lines (318 loc) · 8.72 KB
/
make-inventory.sh.in
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env bash
usage() {
local old_xtrace
old_xtrace="$(shopt -po xtrace || :)"
set +o xtrace
{
echo "${script_name} - Make inventory lists of an album collection."
echo "Usage: ${script_name} [flags] src-directory [src-directory]..."
echo "Option flags:"
echo " -o --output-dir - Output directory. Default: '${output_dir}'."
# echo " -m --mtime - Print file modification time. Default: '${mtime}'."
echo " -a --age-sort - Sort list by age. Default: '${age_sort}'."
echo " -r --genre - Sort list by genre. Default: '${genre_sort}'."
echo " -t --tracks - Output album tracks. Default: '${tracks}'."
# echo " -T --use-tags - Use metadata tags to generate lists. Default: '${use_tags}'."
echo " -n --canonical - Output full canonical paths to lists. Default: '${canonical}'."
echo " -c --config - Configuration file. Default: '${config_file}'."
echo " -h --help - Show this help and exit."
echo " -v --verbose - Verbose execution."
echo " -g --debug - Extra verbose execution."
echo "Info:"
print_project_info
} >&2
eval "${old_xtrace}"
}
process_opts() {
local short_opts="o:martTnc:hvg"
local long_opts="output-dir:,mtime,age-sort,genre,tracks,use-tags,canonical,config:,help,verbose,debug"
local opts
opts=$(getopt --options ${short_opts} --long ${long_opts} -n "${script_name}" -- "$@")
eval set -- "${opts}"
while : ; do
# echo "${FUNCNAME[0]}: (${#}) '${*}'"
case "${1}" in
-o | --output-dir)
output_dir="${2}"
shift 2
;;
-m | --mtime)
mtime=1
shift
;;
-a | --age-sort)
age_sort=1
shift
;;
-r | --genre)
genre_sort=1
shift
;;
-t | --tracks)
tracks=1
shift
;;
-T | --use-tags)
use_tags=1
shift
;;
-n | --canonical)
canonical=1
shift
;;
-c | --config)
config_file="${2}"
shift 2
;;
-h | --help)
usage=1
shift
;;
-v | --verbose)
verbose=1
shift
;;
-g | --debug)
verbose=1
debug=1
set -x
shift
;;
--)
shift
if [[ ${1:-} ]]; then
src_dirs=("$@")
fi
break
;;
*)
echo "${script_name}: ERROR: Internal opts: '${*}'" >&2
exit 1
;;
esac
done
}
generate_inventory_list() {
local list_type=${1}
local src_dir
local src_path
local output_file
local album_path
local -a album_array
local -a artist_array
local -a sorted_array
for src_dir in "${src_dirs[@]}"; do
src_dir="${src_dir%/}"
src_path="$(realpath "${src_dir}")"
output_file="${output_dir}/${src_dir##*/}-${list_type}.lst"
if [[ -n "$(find "${src_dir}" -maxdepth 0 -type d -empty 2>/dev/null)" ]]; then
echo "${script_name}: INFO: Empty directory: '${src_dir}'" >&2
continue
fi
if [[ -z "$(find "${src_dir}" -type f -name 'album.m3u' -print -quit 2>/dev/null)" ]]; then
echo "${script_name}: INFO: No files found: '${src_dir}'" >&2
continue
fi
album_array=()
readarray -t album_array < <( \
find "${src_path}" -type f -name 'album.m3u' | sort \
|| { echo "${script_name}: ERROR: album_array find failed, function=${FUNCNAME[0]:-main}, line=${LINENO}, result=${?}" >&2; \
kill -SIGUSR1 $$; } )
artist_array=()
for (( i = 0; i < ${#album_array[@]}; i++ )); do
local album
local artist_path
local artist
local genre_path
local genre
album_path="${album_array[i]%/album.m3u}"
album="${album_path##*/}"
artist_path="${album_path%/*}"
artist="${artist_path##*/}"
genre_path="${artist_path%/*}"
genre="${genre_path##*/}"
if [[ ${genre_sort} && ! ${genre} ]]; then
echo "${script_name}: WARNING: No genre: '${album_array[i]}'" >&2
fi
if [[ ! ${artist} ]]; then
echo "${script_name}: WARNING: No artist: '${album_array[i]}'" >&2
fi
if [[ ! ${album} ]]; then
echo "${script_name}: WARNING: No album: '${album_array[i]}'" >&2
fi
local ftime_regex='^([[:digit:]]{4})-([[:digit:]]{2})-([[:digit:]]{2})'
local ftime=''
local first
if [[ ${age_sort} ]]; then
first="$(find "${album_path}" -type f \( -name '*.flac' -o -name '*.m4a' \) -print -quit)"
if [[ ! -f "${first}" ]]; then
echo "${script_name}: WARNING: No files: '${album_path}'" >&2
fi
ftime="$(stat --format='%y' "$(realpath "${first}")")"
if [[ ! "${ftime}" =~ ${ftime_regex} ]]; then
echo "${FUNCNAME[0]}: ERROR: No ftime_regex match '${ftime}'" >&2
exit 1
fi
ftime="${BASH_REMATCH[1]}.${BASH_REMATCH[2]}.${BASH_REMATCH[3]}"
fi
if [[ ${genre_sort} ]]; then
artist_array+=("${ftime}@@@${genre}@@@${artist}/${album}@@@${album_path}")
else
artist_array+=("${ftime}@@@@@@${artist}/${album}@@@${album_path}")
fi
done
IFS=$'\n'
sorted_array=($(sort <<<"${artist_array[*]}"))
unset IFS
{
echo "# ${script_name} (@PACKAGE_NAME@) - ${start_time}"
echo "# ${src_dir}: ${#sorted_array[@]} albums."
echo ''
} > "${output_file}"
local data_regex='^(.*)@@@(.*)@@@(.*)@@@(.*)$'
for (( i = 0; i < ${#sorted_array[@]}; i++ )); do
if [[ ! "${sorted_array[i]}" =~ ${data_regex} ]]; then
echo "${FUNCNAME[0]}: ERROR: No regex match '${sorted_array[i]}'" >&2
exit 1
fi
local artist_album
ftime="${BASH_REMATCH[1]}"
genre="${BASH_REMATCH[2]}"
artist_album="${BASH_REMATCH[3]}"
album_path="${BASH_REMATCH[4]}"
# {
# echo "[$(( i + 1 ))] in = @${sorted_array[i]}@"
# echo "[$(( i + 1 ))] ftime = @${ftime}@"
# echo "[$(( i + 1 ))] a-a = @${artist_album}@"
# echo "[$(( i + 1 ))] path = @${album_path}@"
# echo
# } >&2
local display
if [[ ${canonical} ]]; then
display="${album_path}"
else
display="${artist_album}"
fi
if [[ ${age_sort} ]]; then
if [[ ${genre_sort} ]]; then
echo "[$(( i + 1 ))] ${ftime} [${genre}] '${display}'" >> "${output_file}"
else
echo "[$(( i + 1 ))] ${ftime} '${display}'" >> "${output_file}"
fi
else
if [[ ${genre_sort} ]]; then
echo "[$(( i + 1 ))] [${genre}] '${display}'" >> "${output_file}"
else
echo "[$(( i + 1 ))] '${display}'" >> "${output_file}"
fi
fi
if [[ "${list_type}" == 'tracks'* ]]; then
local -a track_array
local track
local j
track_array=()
readarray -t track_array < <( \
find "${album_path}" -type f -name '*.flac' -o -name '*.m4a' | sort \
|| { echo "${script_name}: ERROR: track_array find failed, function=${FUNCNAME[0]:-main}, line=${LINENO}, result=${?}" >&2; \
kill -SIGUSR1 $$; } )
for (( j = 0; j < ${#track_array[@]}; j++ )); do
track="${track_array[j]##*/}"
track="${track%.*}"
echo " '${track}'" >> "${output_file}"
done
fi
done
if [[ ${verbose} && -f "${output_file}" ]]; then
echo "# ${output_file}"
cat "${output_file}"
echo ''
fi
done
}
#===============================================================================
export PS4='\[\e[0;33m\]+ ${BASH_SOURCE##*/}:${LINENO}:(${FUNCNAME[0]:-main}):\[\e[0m\] '
script_name="${0##*/}"
SECONDS=0
start_time="$(date +%Y.%m.%d-%H.%M.%S)"
real_source="$(realpath "${BASH_SOURCE}")"
SCRIPT_TOP="$(realpath "${SCRIPT_TOP:-${real_source%/*}}")"
trap "on_exit 'Failed'" EXIT
trap 'on_err ${FUNCNAME[0]:-main} ${LINENO} ${?}' ERR
trap 'on_err SIGUSR1 ? 3' SIGUSR1
set -eE
set -o pipefail
set -o nounset
source "${SCRIPT_TOP}/audx-lib.sh"
declare -a src_dirs=()
output_dir="/tmp/audx-inventory-${start_time}"
canonical=''
mtime=''
tracks=''
use_tags=''
age_sort=''
genre_sort=''
config_file_default="${HOME}/.audx.conf"
config_file="${config_file_default}"
usage=''
verbose=''
debug=''
collection_top=''
process_opts "${@}"
if [[ "${config_file}" != "${config_file_default}" ]]; then
check_file '--config file' "${config_file}"
fi
if [[ -f "${config_file}" ]]; then
source "${config_file}"
fi
if [[ ${use_tags} ]]; then
echo "${script_name}: ERROR: --use-tags: TODO" >&2
usage
exit 1
fi
if [[ ${usage} ]]; then
usage
trap - EXIT
exit 0
fi
if [[ ${#src_dirs[@]} == 0 ]]; then
if [[ ! ${collection_top} ]]; then
echo "${script_name}: ERROR: No source directories given and no collection_top defined." >&2
usage
exit 1
fi
items=( "${collection_top}"/* )
{
echo "${script_name}: INFO: No source directories given. Using:"
for item in "${items[@]}"; do
if [[ -d "${item}" ]]; then
src_dirs+=("${item}")
echo " '${item}'"
fi
done
echo
} >&2
fi
output_dir="$(realpath --canonicalize-missing "${output_dir}")"
check_src_dirs "${src_dirs[@]}"
mkdir -p "${output_dir}"
case "${tracks}-${age_sort}" in
'-')
generate_inventory_list 'albums'
;;
'-1')
generate_inventory_list 'albums-age'
;;
'1-')
generate_inventory_list 'tracks'
;;
'1-1')
generate_inventory_list 'tracks-age'
;;
*)
echo "${script_name}: ERROR: Internal opts: '${tracks}-${age_sort}'" >&2
exit 1
;;
esac
echo "${script_name}: Lists generated in '${output_dir}'." >&2
trap $'on_exit "Success"' EXIT
exit 0