summaryrefslogtreecommitdiff
path: root/custom/uucp-mediaserver/scripts/queue
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2018-04-05 22:05:05 +0200
committerGregor Kleen <gkleen@yggdrasil.li>2018-04-05 22:05:05 +0200
commitaf359b71b000651812e1ee70638b480502eba0ec (patch)
tree8f246fa120b54c29f3bdf64e8e3b389ec5311851 /custom/uucp-mediaserver/scripts/queue
parentfbc53fdb0e012524b1a22083d19a6928cc87870e (diff)
downloadnixos-af359b71b000651812e1ee70638b480502eba0ec.tar
nixos-af359b71b000651812e1ee70638b480502eba0ec.tar.gz
nixos-af359b71b000651812e1ee70638b480502eba0ec.tar.bz2
nixos-af359b71b000651812e1ee70638b480502eba0ec.tar.xz
nixos-af359b71b000651812e1ee70638b480502eba0ec.zip
media-server on odin
Diffstat (limited to 'custom/uucp-mediaserver/scripts/queue')
-rwxr-xr-xcustom/uucp-mediaserver/scripts/queue134
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
3logTag=${0:t}
4exec 1> >(logger -t "$logTag" -p news.notice)
5exec 2> >(logger -t "$logTag" -p news.error)
6
7doDebug=false
8debug() {
9 if $doDebug; then
10 logger -t "$logTag" -p news.debug
11 else
12 cat >/dev/null
13 fi
14}
15verbose() { $@ | debug }
16
17function mungefilename() {
18 print ${@} | tr -s $';&*|<> \t!_' '_'
19}
20
21typeset -a cleanupCmds
22cleanupCmds=()
23
24function doCleanup {
25 for cmd (${cleanupCmds}); do
26 eval ${cmd} | debug
27 done
28}
29
30trap doCleanup EXIT
31
32function 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
43base=$(basename $0)
44suffix=${base##*.}
45
46force=0
47if [[ "$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
54fi
55
56noCall=false
57if [[ "$1" == "-r" ]]; then
58 shift
59 noCall=true
60fi
61
62for 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
72done
73
74offset=1
75
76advance() {
77 cat =(head -n $((offset - 1)) @queuedir@/${suffix}.queue) =(tail -n +$((offset + 1)) @queueDir@/${suffix}.queue) >@queueDir@/${suffix}.queue
78}
79
80while 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
134done