diff options
Diffstat (limited to 'custom/uucp-mediaserver/scripts/queue')
-rwxr-xr-x | custom/uucp-mediaserver/scripts/queue | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/custom/uucp-mediaserver/scripts/queue b/custom/uucp-mediaserver/scripts/queue new file mode 100755 index 00000000..0039b3b8 --- /dev/null +++ b/custom/uucp-mediaserver/scripts/queue | |||
@@ -0,0 +1,134 @@ | |||
1 | #!@zsh@/bin/zsh | ||
2 | |||
3 | logTag=${0:t} | ||
4 | exec 1> >(logger -t "$logTag" -p news.notice) | ||
5 | exec 2> >(logger -t "$logTag" -p news.error) | ||
6 | |||
7 | doDebug=false | ||
8 | debug() { | ||
9 | if $doDebug; then | ||
10 | logger -t "$logTag" -p news.debug | ||
11 | else | ||
12 | cat >/dev/null | ||
13 | fi | ||
14 | } | ||
15 | verbose() { $@ | debug } | ||
16 | |||
17 | function mungefilename() { | ||
18 | print ${@} | tr -s $';&*|<> \t!_' '_' | ||
19 | } | ||
20 | |||
21 | typeset -a cleanupCmds | ||
22 | cleanupCmds=() | ||
23 | |||
24 | function doCleanup { | ||
25 | for cmd (${cleanupCmds}); do | ||
26 | eval ${cmd} | debug | ||
27 | done | ||
28 | } | ||
29 | |||
30 | trap doCleanup EXIT | ||
31 | |||
32 | function cleanup() { | ||
33 | local cmd | ||
34 | cmd="" | ||
35 | for arg ($@); do | ||
36 | [[ -n "${cmd}" ]] && cmd="${cmd} " | ||
37 | cmd="${cmd}${(qq)arg}" | ||
38 | done | ||
39 | |||
40 | cleanupCmds+=(${cmd}) | ||
41 | } | ||
42 | |||
43 | base=$(basename $0) | ||
44 | suffix=${base##*.} | ||
45 | |||
46 | force=0 | ||
47 | if [[ "$1" == "-f" ]]; then | ||
48 | shift | ||
49 | force=1 | ||
50 | if [[ -n "$1" && "$1" -eq "$1" ]] 2>/dev/null; then | ||
51 | force="$1" | ||
52 | shift | ||
53 | fi | ||
54 | fi | ||
55 | |||
56 | noCall=false | ||
57 | if [[ "$1" == "-r" ]]; then | ||
58 | shift | ||
59 | noCall=true | ||
60 | fi | ||
61 | |||
62 | for f (${@}); do | ||
63 | f=$(readlink -f ${f}) | ||
64 | if grep -q ${f} @queueDir@/${suffix}.queue; then | ||
65 | printf "‘%s’ is already in queue file\n" ${f:t} | warn | ||
66 | continue | ||
67 | fi | ||
68 | if uustat | grep -q ${f:t}; then | ||
69 | printf "‘%s’ is already in uucp queue\n" ${f:t} | warn | ||
70 | fi | ||
71 | print -r ${f} >> @queueDir@/${suffix}.queue | ||
72 | done | ||
73 | |||
74 | offset=1 | ||
75 | |||
76 | advance() { | ||
77 | cat =(head -n $((offset - 1)) @queuedir@/${suffix}.queue) =(tail -n +$((offset + 1)) @queueDir@/${suffix}.queue) >@queueDir@/${suffix}.queue | ||
78 | } | ||
79 | |||
80 | while true; do | ||
81 | [[ $(wc -l @queueDir@/${suffix}.queue | cut -d ' ' -f 1) -lt $offset ]] && break | ||
82 | file=$(tail -n +${offset} @queueDir@/${suffix}.queue | head -n 1) | ||
83 | printf "Considering ‘%s’" ${file} | debug | ||
84 | if [[ -z "${file}" || ! -e "${file}" ]]; then | ||
85 | if [[ -n "${file}" ]]; then | ||
86 | printf "‘%s’ does not exist, skipping\n" "${file}" >&2 | ||
87 | printf "Subject: Missing file in %s\n\n%s" $logTag ${file} \ | ||
88 | | sendmail gkleen \ | ||
89 | && echo "Sent mail." | ||
90 | fi | ||
91 | advance | ||
92 | continue | ||
93 | fi | ||
94 | |||
95 | space=$(($(cat @queueDir@/${suffix}.space) - $(queuesize.$suffix))) | ||
96 | printf "%s left on %s\n" $(numfmt --suffix=B --to=iec-i -- $space) $suffix | debug | ||
97 | |||
98 | size=$(stat -c "%s" "${file}") | ||
99 | printf "‘%s’ is %s\n" ${file:t} $(numfmt --suffix=B --to=iec-i -- $size) | debug | ||
100 | if [[ "${size}" -le "${space}" || "${force}" -gt 0 ]]; then | ||
101 | printf "queuing ‘%s’ for %s\n" ${file:t} ${suffix} | debug | ||
102 | function send() { | ||
103 | typeset -a cmd | ||
104 | cmd=(uux $(if ${noCall}; then echo "-r"; fi) -g z) | ||
105 | |||
106 | compatibleName=${file} | ||
107 | function munge() { [[ $(mungefilename ${file}) != ${file} ]] } | ||
108 | |||
109 | if munge; then | ||
110 | compatibleName=${file:h}/$(mungefilename ${file:t}) | ||
111 | [[ -e ${compatibleName} ]] || verbose ln -vs ${file} ${compatibleName} | ||
112 | cmd+=(-C) | ||
113 | cleanup rm -v ${compatibleName} | ||
114 | fi | ||
115 | |||
116 | cmd+=("${suffix}!recv-media" "!${compatibleName}" "$(date --date=@$(stat -c '%Y' "${file}") "+%Y%m%d%H%M.%S")") | ||
117 | |||
118 | if munge; then | ||
119 | cmd+=("$(print -- ${file:t} | base64 -w0)") | ||
120 | fi | ||
121 | $cmd && printf "Queued ‘%s’ for %s\n" ${file:t} $suffix | ||
122 | } | ||
123 | |||
124 | function skip() { | ||
125 | offset=$((offset + 1)) | ||
126 | printf "Failed to queue ‘%s’\n" ${file} >&2 | ||
127 | } | ||
128 | |||
129 | { { [[ ! -e "${file}" ]] || send } && advance && update-queuesize.$suffix } || skip | ||
130 | force=$(($force - 1)) | ||
131 | else | ||
132 | break | ||
133 | fi | ||
134 | done | ||