-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaylist
More file actions
executable file
·90 lines (80 loc) · 1.42 KB
/
Copy pathplaylist
File metadata and controls
executable file
·90 lines (80 loc) · 1.42 KB
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
#!/usr/bin/env bash
err() {
echo "$(color red)$@$(color off)" >&2
}
warn() {
echo "$(color blue)$@$(color off)" >&2
}
die() {
err $@
exit 1
}
usage="playlist <command> [queued actions] < [queued actions]"
while [ $# -gt 0 ]; do
case "$1" in
-q|--quiet)
warn() {
return
}
shift
;;
-n|--name)
shift
set -u
cmd_name="$1"
set +u
shift
;;
-f|--foreground)
foreground=true
logfile="/dev/stdout"
shift
;;
-*)
die "unknown flag: $1"
;;
*)
break
;;
esac
done
test -n "$1" || die "$usage"
cmd="$1"; shift
: ${cmd_name:="$(echo $cmd | cut -d ' ' -f 1)"}
plist="$cmd_name.plist"
: ${logfile:=$cmd_name.log}
if test -e "$plist"; then
if ! test -f $plist
then die "$plist exists and is not a regular file"
fi
warn "using existing playlist: $plist"
fi
# add args to playlist
while [ $# -gt 0 ]; do
echo "$1" >> $plist
shift
done
# add stdin to playlist
if read -t 0
then cat >> $plist
fi
not_locked() {
exec 4<>"$1.lock"; flock -n 4
}
plist_runner() {
if not_locked "$plist"; then
warn "started runner"
while [ -s "$plist" ] ; do
echo "$cmd $(head -1 "$plist")"
$cmd "$(head -1 "$plist")" || echo $(head -1 "$plist") >> "failed-$plist"
sed -i 1d "$plist"
done > "$logfile"
rm "$plist"
trash-put "$logfile" 2>/dev/null &&
warn "Playlist completed. use \`trash-restore\` to retrieve log"
fi
}
if [ -n "$foreground" ]
then plist_runner
else plist_runner&
fi