-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshuffle-playlist.sh.in
executable file
·232 lines (187 loc) · 4.52 KB
/
shuffle-playlist.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
#!/usr/bin/env bash
usage() {
local old_xtrace
old_xtrace="$(shopt -po xtrace || :)"
set +o xtrace
{
echo "${script_name} - Randomly shuffle an M3U playlist."
echo "Usage: ${script_name} [flags] <M3U playlist>"
echo "Option flags:"
echo " -o --output-file - Shuffle list output file. Default: '${out_file}'."
echo " -h --help - Show this help and exit."
echo " -v --verbose - Verbose execution. Default: '${verbose}'."
echo " -g --debug - Extra verbose execution. Default: '${debug}'."
echo 'Input:'
echo " '${playlist_file}'"
echo "Info:"
print_project_info
} >&2
eval "${old_xtrace}"
}
process_opts() {
local short_opts="o:hvg"
local long_opts="output-file:,help,verbose,debug"
local opts
opts=$(getopt --options ${short_opts} --long ${long_opts} -n "${script_name}" -- "$@")
eval set -- "${opts}"
while true ; do
# echo "${FUNCNAME[0]}: (${#}) '${*}'"
case "${1}" in
-o | --output-file)
out_file="${2}"
shift 2
;;
-h | --help)
usage=1
shift
;;
-v | --verbose)
verbose=1
shift
;;
-g | --debug)
verbose=1
debug=1
keep_tmp_dir=1
set -x
shift
;;
--)
shift
playlist_file="${1:-}"
if [[ ${playlist_file} ]]; then
shift
fi
extra_args="${*}"
break
;;
*)
echo "${script_name}: ERROR: Internal opts: '${*}'" >&2
exit 1
;;
esac
done
}
#===============================================================================
export PS4='\[\e[0;33m\]+ ${BASH_SOURCE##*/}:${LINENO}:(${FUNCNAME[0]:-main}):\[\e[0m\] '
SECONDS=0
start_time="$(date +%Y.%m.%d-%H.%M.%S)"
real_source="$(realpath "${BASH_SOURCE}")"
SCRIPT_TOP="$(realpath "${SCRIPT_TOP:-${real_source%/*}}")"
script_name="${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"
out_file="/tmp/audx-shufflelist-${start_time}.m3u"
usage=''
verbose=''
debug=''
playlist_file=''
process_opts "${@}"
if [[ ${usage} ]]; then
usage
trap - EXIT
exit 0
fi
print_project_banner
if [[ ${extra_args} ]]; then
set +o xtrace
echo "${script_name}: ERROR: Got extra args: '${extra_args}'" >&2
usage
exit 1
fi
shuf="${shuf:-shuf}"
check_program "shuf" "${shuf}"
if [[ ! ${playlist_file} ]]; then
set +o xtrace
echo "${script_name}: ERROR: No input file." >&2
usage
exit 1
fi
if [[ ! -f ${playlist_file} ]]; then
set +o xtrace
echo "${script_name}: ERROR: Bad input file '${playlist_file}'" >&2
usage
exit 1
fi
playlist_file="$(realpath -e "${playlist_file}")"
if [[ ${verbose} ]]; then
echo "${script_name}: INFO: playlist = '${playlist_file}'"
fi
readarray -t lines_array < "${playlist_file}"
line_count="${#lines_array[@]}"
declare -a header_array=()
declare -a data_array=()
in_header=1
for (( id = 1; id <= ${line_count}; id++ )); do
line="${lines_array[$(( id - 1 ))]}"
if [[ "${line::1}" = '#' ]]; then
if [[ ${in_header} ]]; then
if [[ ${verbose} ]]; then
echo "${id}: (header) = '${line}'" >&2
fi
header_array=("${header_array[@]}" "${line}")
continue
fi
if [[ ${verbose} ]]; then
echo "${id}: (comment) = '${line}'" >&2
fi
continue
fi
in_header=''
if [[ ! "${line}" ]]; then
if [[ ${verbose} ]]; then
echo "${id}: (blank) = '${line}'" >&2
fi
continue
fi
if [[ ${verbose} ]]; then
echo "${id}: (data) = '${line}'" >&2
fi
data_array=("${data_array[@]}" "${line}")
done
if (( ${#header_array[@]} == 0 )); then
set +o xtrace
echo "${script_name}: ERROR: No M3U header found in '${playlist_file}'" >&2
exit 1
fi
if (( ${#data_array[@]} == 0 )); then
set +o xtrace
echo "${script_name}: ERROR: No data found in '${playlist_file}'" >&2
exit 1
fi
header_count="${#header_array[@]}"
data_count="${#data_array[@]}"
if [[ ${verbose} ]]; then
{
echo "data_count = '${data_count}'"
echo
} >&2
fi
echo -n '' > "${out_file}"
for (( id = 1; id <= ${header_count}; id++ )); do
header="${header_array[$(( id - 1 ))]}"
echo "${header}" >> "${out_file}"
done
echo "#COMMENT:AUDX shuffle-playlist - ${start_time}" >> "${out_file}"
echo >> "${out_file}"
declare -a random_array
fill_random_array "${data_count}" data_array random_array
for (( i = 0; i < ${data_count}; i++ )); do
echo "${random_array[i]}" >> "${out_file}"
done
if [[ ${verbose} ]]; then
{
echo '-------------------------'
cat "${out_file}"
echo '-------------------------'
} >&2
fi
echo "INFO: Wrote ${data_count} lines." >&2
echo "INFO: Playlist in '${out_file}'." >&2
trap "on_exit 'Success'" EXIT
exit 0