From af359b71b000651812e1ee70638b480502eba0ec Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Thu, 5 Apr 2018 22:05:05 +0200 Subject: media-server on odin --- custom/uucp-mediaserver/scripts/queue | 134 ++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100755 custom/uucp-mediaserver/scripts/queue (limited to 'custom/uucp-mediaserver/scripts/queue') 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 @@ +#!@zsh@/bin/zsh + +logTag=${0:t} +exec 1> >(logger -t "$logTag" -p news.notice) +exec 2> >(logger -t "$logTag" -p news.error) + +doDebug=false +debug() { + if $doDebug; then + logger -t "$logTag" -p news.debug + else + cat >/dev/null + fi +} +verbose() { $@ | debug } + +function mungefilename() { + print ${@} | tr -s $';&*|<> \t!_' '_' +} + +typeset -a cleanupCmds +cleanupCmds=() + +function doCleanup { + for cmd (${cleanupCmds}); do + eval ${cmd} | debug + done +} + +trap doCleanup EXIT + +function cleanup() { + local cmd + cmd="" + for arg ($@); do + [[ -n "${cmd}" ]] && cmd="${cmd} " + cmd="${cmd}${(qq)arg}" + done + + cleanupCmds+=(${cmd}) +} + +base=$(basename $0) +suffix=${base##*.} + +force=0 +if [[ "$1" == "-f" ]]; then + shift + force=1 + if [[ -n "$1" && "$1" -eq "$1" ]] 2>/dev/null; then + force="$1" + shift + fi +fi + +noCall=false +if [[ "$1" == "-r" ]]; then + shift + noCall=true +fi + +for f (${@}); do + f=$(readlink -f ${f}) + if grep -q ${f} @queueDir@/${suffix}.queue; then + printf "‘%s’ is already in queue file\n" ${f:t} | warn + continue + fi + if uustat | grep -q ${f:t}; then + printf "‘%s’ is already in uucp queue\n" ${f:t} | warn + fi + print -r ${f} >> @queueDir@/${suffix}.queue +done + +offset=1 + +advance() { + cat =(head -n $((offset - 1)) @queuedir@/${suffix}.queue) =(tail -n +$((offset + 1)) @queueDir@/${suffix}.queue) >@queueDir@/${suffix}.queue +} + +while true; do + [[ $(wc -l @queueDir@/${suffix}.queue | cut -d ' ' -f 1) -lt $offset ]] && break + file=$(tail -n +${offset} @queueDir@/${suffix}.queue | head -n 1) + printf "Considering ‘%s’" ${file} | debug + if [[ -z "${file}" || ! -e "${file}" ]]; then + if [[ -n "${file}" ]]; then + printf "‘%s’ does not exist, skipping\n" "${file}" >&2 + printf "Subject: Missing file in %s\n\n%s" $logTag ${file} \ + | sendmail gkleen \ + && echo "Sent mail." + fi + advance + continue + fi + + space=$(($(cat @queueDir@/${suffix}.space) - $(queuesize.$suffix))) + printf "%s left on %s\n" $(numfmt --suffix=B --to=iec-i -- $space) $suffix | debug + + size=$(stat -c "%s" "${file}") + printf "‘%s’ is %s\n" ${file:t} $(numfmt --suffix=B --to=iec-i -- $size) | debug + if [[ "${size}" -le "${space}" || "${force}" -gt 0 ]]; then + printf "queuing ‘%s’ for %s\n" ${file:t} ${suffix} | debug + function send() { + typeset -a cmd + cmd=(uux $(if ${noCall}; then echo "-r"; fi) -g z) + + compatibleName=${file} + function munge() { [[ $(mungefilename ${file}) != ${file} ]] } + + if munge; then + compatibleName=${file:h}/$(mungefilename ${file:t}) + [[ -e ${compatibleName} ]] || verbose ln -vs ${file} ${compatibleName} + cmd+=(-C) + cleanup rm -v ${compatibleName} + fi + + cmd+=("${suffix}!recv-media" "!${compatibleName}" "$(date --date=@$(stat -c '%Y' "${file}") "+%Y%m%d%H%M.%S")") + + if munge; then + cmd+=("$(print -- ${file:t} | base64 -w0)") + fi + $cmd && printf "Queued ‘%s’ for %s\n" ${file:t} $suffix + } + + function skip() { + offset=$((offset + 1)) + printf "Failed to queue ‘%s’\n" ${file} >&2 + } + + { { [[ ! -e "${file}" ]] || send } && advance && update-queuesize.$suffix } || skip + force=$(($force - 1)) + else + break + fi +done -- cgit v1.2.3