summaryrefslogtreecommitdiff
path: root/custom/uucp-mediaserver/scripts/queue
blob: ca76ae265e51cb9b581f046c5aab1e9d17bc5300 (plain)
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
#!@zsh@/bin/zsh

logTag=${0:t}
exec 1> >(logger -t "$logTag" -p news.notice)
exec 2> >(logger -t "$logTag" -p news.error)

doDebug=true
debug() {
    if $doDebug; then
        logger -t "$logTag" -p news.debug
    else
        cat >/dev/null
    fi
}
verbose() { $@ | debug }

warn() { cat >&2 }

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
noCall=false
prepend=false

typeset -A opts
zparseopts -A opts -D -- r p f::

force=${opts[-f]:-${+opts[-f]}}
[[ -v opts[-p] ]] && prepend=true
[[ -v opts[-r] ]] && noCall=true

printf "force: %d, noCall: %s, prepend: %s\n" ${force} ${noCall} ${prepend} | debug

for f (${@}); do
    f=$(readlink -f ${f})
    if [[ ! -f ${f} ]]; then
        printf "‘%s’ is not a regular file\n" ${f:t} | warn
        continue
    fi
    
    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

    if ${prepend}; then
        cat =(print -r ${f}; cat @queueDir@/${suffix}.queue) > @queueDir@/${suffix}.queue
    else
        print -r ${f} >> @queueDir@/${suffix}.queue
    fi
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 uucp \
              && 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 -r -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

${noCall} || systemctl --no-block start uucico@${suffix}.service