#############
# ezmlm-get #
#############

prompt "ezmlm-get (digest):   "

######### digests
# we use headeradd to label them since trigger headers aren't propagated
echo "X-num: not_propagated" > "$TMP"

# dig1 from manager will go to DIG@HOST
# dig2 from editor
# dig3 from command line
# dig4 -fr format check from command line. We check only that they get there.
# dig5 -fn
# dig6 -fx
# dig7 -fv
# we check that dignum is created and digissue is updated 

# now -dig in the manager position:
qqclean
mv -f "${DIR}/headeradd" "${DIR}/headeradd.bak"
echo "X-num: dig1" > "${DIR}/headeradd"
SENDER="${BNC}@$HOST"
LOCAL="$LIST-dig.code"
DEFAULT='dig.code'
${EZBIN}/ezmlm-get "$DIR" < "$TMP" >/dev/null 2>&1 && \
	fatal "failed to reject -dig when no digest code was on cmd-line"
if [ -r "${DIR}/dignum" ]; then
	fatal "script error: dignum exists"
fi
msgexists && \
	fatal "dig1 from non-manager was delivered"

qqclean
${EZBIN}/ezmlm-get "$DIR" 'code' < "$TMP" >"$ERR" 2>&1
if [ "$?" != "99" ]; then
	fatal "failed to exit 99 after digest in manager position"
fi
if [ ! -r "${DIR}/dignum" ]; then
	fatal "failed to generate dignum"
fi
if [ ! -r "${DIR}/digissue" ]; then
	fatal "failed to generate digissue"
fi
msgexists || \
	fatal "dig1 from manager wasn't delivered"
checkenv dig1 "$LIST-digest-return-.*@$HOST-@\\[\\]" "$DIG@$HOST" "$MAN@$HOST"

qqclean
rm "$DIR"/dignum
echo code >"$DIR"/digestcode
${EZBIN}/ezmlm-get "$DIR" < "$TMP" >"$ERR" 2>&1
if [ "$?" != "99" ]; then
	fatal "failed to exit 99 after digest in manager position with file"
fi
if [ ! -r "${DIR}/dignum" ]; then
	fatal "failed to generate dignum"
fi
if [ ! -r "${DIR}/digissue" ]; then
	fatal "failed to generate digissue"
fi
msgexists || \
	fatal "dig1 from manager wasn't delivered"
checkenv dig1 "$LIST-digest-return-.*@$HOST-@\\[\\]" "$DIG@$HOST" "$MAN@$HOST"
echo >"$DIR"/digestcode

qqclean
${EZBIN}/ezmlm-get "$DIR" 'code' < "$TMP" >/dev/null 2>&1
if [ "$?" != "99" ]; then
	fatal "failed to exit 99 when nothing to digest in manager position"
fi
msgexists && \
	fatal "empty dig1 from manager was delivered"

qqclean
${EZBIN}/ezmlm-get "$DIR" 'coden' < "$TMP" >/dev/null 2>&1 && \
	fatal "failed to reject -dig with bad digest code 'coden'"
msgexists && \
	fatal "delivered -dig with bad digest code 'coden'"

qqclean
${EZBIN}/ezmlm-get "$DIR" 'cod' < "$TMP" >/dev/null 2>&1 && \
	fatal "failed to reject -dig with bad digest code 'cod'"
msgexists && \
	fatal "delivered -dig with bad digest code 'code'"

qqclean
echo coden >"$DIR"/digestcode
${EZBIN}/ezmlm-get "$DIR" < "$TMP" >/dev/null 2>&1 && \
	fatal "failed to reject -dig with bad digest code file 'coden'"
msgexists && \
	fatal "delivered -dig with bad digest code file 'coden'"

qqclean
echo cod >"$DIR"/digestcode
${EZBIN}/ezmlm-get "$DIR" < "$TMP" >/dev/null 2>&1 && \
	fatal "failed to reject -dig with bad digest code file 'cod'"
msgexists && \
	fatal "delivered -dig with bad digest code file 'cod'"

# now in the editor position:
qqclean
rm -f "${DIR}/dignum"
LOCAL="$LIST"
unset DEFAULT
echo "X-num: dig2" > "${DIR}/headeradd"
${EZBIN}/ezmlm-get "$DIR" < "$TMP" >"$ERR" 2>&1 || \
	fatal "failed to exit 0 after digest in editor"
msgexists || \
	fatal "dig2 from editor wasn't delivered"
checkenv dig2 "$LIST-digest-return-.*@$HOST-@\\[\\]" "$DIG@$HOST" "$MAN@$HOST"

# This causes an error on systems where 'unset' doesn't work
# For these, we skip this test.
  if [ -z "$BADUNSET" ]; then
    qqclean
    if [ ! -r "${DIR}/dignum" ]; then
	fatal "failed to generate dignum after digest in editor"
    fi

    grep "^3:" "${DIR}/digissue" >/dev/null 2>&1 || \
	fatal "failed to update digissue after digest in editor"
    ${EZBIN}/ezmlm-get "$DIR" < "$TMP" >"$ERR" 2>&1 || \
	fatal "failed to exit 0 when nothing to digest in editor"
    msgexists && \
	fatal "empty dig2 from editor was delivered"
  fi

# now from the command line with formats ...
qqclean
rm -f "${DIR}/dignum"
LOCAL=''
echo "X-num: dig3" > "${DIR}/headeradd"
${EZBIN}/ezmlm-get "$DIR" < "$TMP" >/dev/null 2>&1 || \
	fatal "failed to exit 0 after cmd line digest"
msgexists || \
	fatal "dig3 from command line wasn't delivered"
checkenv dig3 "$LIST-digest-return-.*@$HOST-@\\[\\]" "$DIG@$HOST" "$MAN@$HOST"

qqclean
grep "^4:" "${DIR}/digissue" >/dev/null 2>&1 || \
	fatal "failed to update digissue after cmd line digest"
${EZBIN}/ezmlm-get "$DIR" < "$TMP" >/dev/null 2>&1 || \
	fatal "failed to exit 0 when nothing to digest from cmd line"
msgexists && \
	fatal "empty dig3 from command line was delivered"

qqclean
rm -f "${DIR}/dignum"
echo "X-num: dig4" > "${DIR}/headeradd"
${EZBIN}/ezmlm-get -fr "$DIR" < "$TMP" >/dev/null 2>&1 || \
	fatal "-fr failed for digest"
msgexists || \
	fatal "dig4 format 'r' wasn't delivered"
checkenv dig4 "$LIST-digest-return-.*@$HOST-@\\[\\]" "$DIG@$HOST" "$MAN@$HOST"

qqclean
rm -f "${DIR}/dignum"
echo "X-num: dig5" > "${DIR}/headeradd"
${EZBIN}/ezmlm-get -fn "$DIR" < "$TMP" >/dev/null 2>&1 || \
	fatal "-fn failed for digest"
msgexists || \
	fatal "dig5 format 'n' wasn't delivered"
checkenv dig5 "$LIST-digest-return-.*@$HOST-@\\[\\]" "$DIG@$HOST" "$MAN@$HOST"

qqclean
rm -f "${DIR}/dignum"
echo "X-num: dig6" > "${DIR}/headeradd"
${EZBIN}/ezmlm-get -fv "$DIR" < "$TMP" >/dev/null 2>&1 || \
	fatal "-fv failed for digest"
msgexists || \
	fatal "dig6 format 'v' wasn't delivered"
checkenv dig6 "$LIST-digest-return-.*@$HOST-@\\[\\]" "$DIG@$HOST" "$MAN@$HOST"

rm -f "${DIR}/dignum"
echo "X-num: dig7" > "${DIR}/headeradd"
${EZBIN}/ezmlm-get -fx "$DIR" < "$TMP" >/dev/null 2>&1 || \
	fatal "-fx failed for digest"
msgexists || \
	fatal "dig7 format 'x' wasn't delivered"
checkenv dig7 "$LIST-digest-return-.*@$HOST-@\\[\\]" "$DIG@$HOST" "$MAN@$HOST"

rm -f "${DIR}/dignum"
echo "X-num: dig8" > "${DIR}/headeradd"
echo r > "$DIR"/digformat
${EZBIN}/ezmlm-get "$DIR" < "$TMP" >/dev/null 2>&1 || \
	fatal "digformat=r failed for digest"
msgexists || \
	fatal "dig8 format 'r' wasn't delivered"
checkenv dig8 "$LIST-digest-return-.*@$HOST-@\\[\\]" "$DIG@$HOST" "$MAN@$HOST"
test $(grep -c Content-Type: "$QQMSG") = 1 \
|| fatal "ezmlm-get didn't read digformat"

# restore headeradd
mv -f "${DIR}/headeradd.bak" "${DIR}/headeradd"

echo "OK"
export DEFAULT
