summaryrefslogtreecommitdiff
path: root/custom/uucp-mediaserver/scripts/queue
blob: 0039b3b8d1bb0a8afd889d99e4c3459c396ab962 (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
#!@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