#!@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