#############
# 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="$LOC-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 "$LOC-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 "$LOC-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="$LOC"
${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 "$LOC-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 "$LOC-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 "$LOC-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 "$LOC-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 "$LOC-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 "$LOC-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 "$LOC-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
