###############################
# ezmlm-sub/unsub/list/issubn #
###############################

  prompt "ezmlm-[un|is]sub[n]:  "

  SENDER="XYZZY@HOst"

# With mysql testing, there may be junk left from earlier testing that
# gives false positives in testing. Make sure it's detected
  ${EZBIN}/ezmlm-list "$DIR" >/dev/null || \
	fatal "ezmlm-list: failed"

  ${EZBIN}/ezmlm-list "$DIR" | grep '@' >/dev/null 2>&1 && \
	fatal "already addresses in table - please remove and start again"

  ${EZBIN}/ezmlm-list "$DIR" digest | grep '@' >/dev/null 2>&1 && \
	fatal "already addresses in table - please remove and start again"

  ${EZBIN}/ezmlm-list "$DIR" allow | grep '@' >/dev/null 2>&1 && \
	fatal "already addresses in table - please remove and start again"

# not subscriber. Test default
  ${EZBIN}/ezmlm-issubn "$DIR" "$DIR"/allow && \
	fatal "ezmlm-issubn: failed: exit 0 on non-subscriber (absolute)"
  ${EZBIN}/ezmlm-issubn "$DIR" . allow && \
	fatal "ezmlm-issubn: failed: exit 0 on non-subscriber (relative)"

# not subscriber. Test -n
  ${EZBIN}/ezmlm-issubn -n "$DIR" "$DIR"/allow || \
	fatal "ezmlm-issubn: -n failed for non-subscriber (absolute)"
  ${EZBIN}/ezmlm-issubn -n "$DIR" . allow || \
	fatal "ezmlm-issubn: -n failed for non-subscriber (relative)"

# add subscriber
  ${EZBIN}/ezmlm-sub "$DIR" "xyZZy@hoSt" || \
	fatal "ezmlm-sub: failed to add subscriber"

# is subscriber. Test default
  ${EZBIN}/ezmlm-issubn "$DIR" . allow || \
	fatal "ezmlm-issubn: failed: exit false for subscriber"

# is subscriber. Test -n
  ${EZBIN}/ezmlm-issubn -n "$DIR" . allow && \
	fatal "ezmlm-issubn: -n failed for subscriber"

# is subscriber, but not in alternate lists
  ${EZBIN}/ezmlm-issubn "$DIR" deny && \
	fatal "ezmlm-issubn: exit true for non-subscriber (alt absolute+relative)"
  ${EZBIN}/ezmlm-issubn "$SUBDIR" deny && \
	fatal "ezmlm-issubn: exit true for non-subscriber (alt relative+relative)"

# add to allow
  ${EZBIN}/ezmlm-sub "$DIR" allow "ZZtop@hoSt" || \
	fatal "ezmlm-sub: failed to add address to ${DIR}/allow"
  ${EZBIN}/ezmlm-list "$DIR" allow | grep 'ZZtop@host' >/dev/null 2>&1 || \
	fatal "ezmlm-sub: failed to find address in ${DIR} allow"

# list subscribers
  ${EZBIN}/ezmlm-list "$DIR" | grep "xyZZy" >"$ERR" 2>&1 || \
	fatal "ezmlm-list: failed to list subscribers"

# remove subscriber
  ${EZBIN}/ezmlm-unsub "$DIR" "xYzZy@hOst" || \
	fatal "ezmlm-unsub: failed to remove subscriber"

# see that it was removed
  ${EZBIN}/ezmlm-list "$DIR" | grep "xyZZy" >"$ERR" 2>&1 && \
	fatal "ezmlm-unsub: failed to remove address"

  SENDER="zztop@HOst"

# check for address in allow
  ${EZBIN}/ezmlm-issubn "$DIR" "$DIR"/allow || \
	fatal "ezmlm-sub/issubn: failed to add/look in 2nd db (absolute+absolute)"
  ${EZBIN}/ezmlm-issubn "$DIR" allow || \
	fatal "ezmlm-sub/issubn: failed to add/look in 2nd db (absolute+relative)"
  ${EZBIN}/ezmlm-issubn "$SUBDIR" allow || \
	fatal "ezmlm-sub/issubn: failed to add/look in 2nd db (relative+relative)"

# remove (multiple) (non)existing addresses from allow
  ${EZBIN}/ezmlm-unsub "$DIR" allow "xYzZy@hOst" "zZToP@HOSt" || \
	fatal "ezmlm-unsub: failed to remove subscriber"

# verify removal
  ${EZBIN}/ezmlm-issubn "$DIR" . allow && \
	fatal "ezmlm-unsub/issubn: failed to remove address"

# test wildcard addresses
  ${EZBIN}/ezmlm-sub "$DIR" deny @badexample.com || \
	fatal "ezmlm-sub: failed to add wildcard"
  SENDER=@badexample.com
  ${EZBIN}/ezmlm-issubn "$DIR" deny || \
	fatal "ezmlm-sub/issubn: failed to add wildcard address"

  SENDER=someone@badexample.com
  ${EZBIN}/ezmlm-issubn "$DIR" deny test@badexample.com || \
	fatal "ezmlm-sub/issubn: failed to match wildcard address"

  ${EZBIN}/ezmlm-unsub "$DIR" deny @badexample.com || \
	fatal "ezmlm-unsub: failed to remove wildcard"
  ${EZBIN}/ezmlm-issubn "$DIR" deny && \
	fatal "ezmlm-sub/issubn: failed to remove wildcard address"

# check tags written to the Log
  grep -i '[0-9]\+ +manual xyzzy@host' "$DIR"/Log >/dev/null \
      || fatal "Log is missing manual subscribe tag"
  grep -i '[0-9]\+ -manual xyzzy@host' "$DIR"/Log >/dev/null \
      || fatal "Log is missing manual unsubscribe tag"
  ${EZBIN}/ezmlm-sub -t tag1 "$DIR" xyzzy@host
  grep -i '[0-9]\+ +tag1 xyzzy@host' "$DIR"/Log >/dev/null \
      || fatal "ezmlm-sub did not alter subscribe tag"
  ${EZBIN}/ezmlm-unsub -t tag2 "$DIR" xyzzy@host
  grep -i '[0-9]\+ -tag2 xyzzy@host' "$DIR"/Log >/dev/null \
      || fatal "ezmlm-sub did not alter subscribe tag"

# clean up
  LOCAL=''

  echo "OK"
