#!/bin/sh
# WARNING: This file was auto-generated. Do not edit!
EZPATH='/usr/local/bin'
QMPATH='/var/qmail'

# [should have a bin/sh line and EZPATH/QMPATH added above by make]
#
# script to diagnose ezmlm lists
# call as is for environment only, or as ezmlm-check 'DIR' for more info

QINJECT="${QMPATH}/bin/qmail-inject"
EZLIST="${EZPATH}/ezmlm-list"
MYNAME='ezmlm-check'
MYDTLINE="Delivered-To: ${MYNAME}"
FATAL="${MYNAME}: fatal:"
EZERR="--- ERROR:"
GREP='grep'
CAT='cat'
ECHO='echo'
HEAD='head'
# should mark executables with '*' and list one file per line
LS='ls -F1'

if [ ! -x "$QINJECT" ]; then
  ${ECHO} "$FATAL edit script to for path to qmail-inject"
  exit 100
fi

if [ -z "$SENDER" ] ; then
	RCP="${CAT}"
else
	RCP="$QINJECT"
	if ${CAT} - | ${GREP} -q "${MYDTLINE}"
	  then
	    ${ECHO} "FATAL this message is looping: it already has my Delivered-To line (#5.4.6)"
	    exit 100
	fi
fi

if [ -n "$1" -a ! -d "$1" ]; then
  ${ECHO} "$FATAL $1 is not a directory"; exit 100
fi

(
	${ECHO} "Delivered-To: ezmlm-check"
	${ECHO} "To: $SENDER"
	${ECHO} "Subject: ${MYNAME} results"
	${ECHO}
	${ECHO} "Important environment variables:"
	${ECHO}
	${ECHO} "SENDER ='$SENDER'"
	${ECHO} "NEWSENDER ='$NEWSENDER'"
	${ECHO} "RECIPIENT ='$RECIPIENT'"
	${ECHO} "USER ='$USER'"
	${ECHO} "LOCAL ='$LOCAL'"
	${ECHO} "HOST ='$HOST'"
	${ECHO} "---------------------------------------------------"
	${ECHO} "Checking basic list setup:"
	${ECHO}
	FLAGARCH='0'
	FLAGIND='0'
	if [ -n "$1" ]; then
	  if [ ! -r "$1/mailinglist" ]; then
	    ${ECHO} "$EZERR $1/mailinglist does not exist"
	  fi
	  if [ ! -w "$1/num" ]; then
	    ${ECHO} "$EZERR $1/num is not writable to $USER"
	  else
	    NUM=`${CAT} "$1/num" | ${HEAD} -1`
	    ${ECHO} "... latest message was message # $NUM"
          fi
	  ${ECHO}
	  if [ ! -w "$1/lock" ]; then
	    ${ECHO} "$EZERR User $USER does not have write premission to $1/lock"
	  fi
	  if [ ! -w "$1/lockbounce" ]; then
	    ${ECHO} "$EZERR User $USER does not have write premission to $1/lockbounce"
	  fi
	  if [ ! -r "$1/inlocal" ]; then
	    ${ECHO} "$EZERR $1/inlocal does not exist"
	  elif [ ! -r "$1/inhost" ]; then
	      ${ECHO} "$EZERR $1/inhost does not exist"
	  else
	    INLOCAL=`${CAT} "$1/inlocal"| ${HEAD} -1`
            INHOST=`${CAT} "$1/inhost"| ${HEAD} -1`
	    if [ "$HOST" != "$INHOST" ]; then
	      ${ECHO} "$EZERR HOST does not match $1/inhost"
	    else
	      ${ECHO} "... $1/inhost OK"
	    fi
	    if ! ${ECHO} "$LOCAL" | ${GREP} -G -q "^$INLOCAL"
            then
	      ${ECHO} "$EZERR LOCAL does not begin with contents of $1/inlocal"
	    else
	      ${ECHO} "... $1/inlocal OK"
	    fi
	  fi
	  if [ ! -r "$1/outlocal" ]; then
	    ${ECHO} "$EZERR $1/outlocal does not exist"
	  fi
	  if [ ! -r "$1/outhost" ]; then
	      ${ECHO} "$EZERR $1/outhost does not exist"
	  fi
	  if [ ! -r "$1/editor" ]; then
	      ${ECHO} "$EZERR $1/editor does not exist"
	  else
	    ${ECHO}
	    ${ECHO} "$1/editor:"
	    ${ECHO} "============================"
	    ${CAT} "$1/editor"
	    ${ECHO} "============================"
	    ${ECHO}
	  fi
	  if [ ! -r "$1/manager" ]; then
	      ${ECHO} "$EZERR $1/manager does not exist"
	  else
	    ${ECHO} "$1/manager:"
	    ${ECHO} "============================"
	    ${CAT} "$1/manager"
	    ${ECHO} "============================"
	    ${ECHO}
	  fi
	  if [ ! -r "$1/bouncer" ]; then
	      ${ECHO} "$EZERR $1/bouncer does not exist"
	  else
	    ${ECHO} "$1/bouncer:"
	    ${ECHO} "============================"
	    ${CAT} "$1/bouncer"
	    ${ECHO} "============================"
	    ${ECHO}
	  fi
	  if [ ! -r "$1/owner" ]; then
	      ${ECHO} "$EZERR $1/manager does not exist"
	  else
	    ${ECHO} "$1/owner:"
	    ${ECHO} "============================"
	    ${CAT} "$1/owner"
	    ${ECHO} "============================"
	    ${ECHO}
	  fi
	  if [ ! -r "$1/headeradd" ]; then
	      ${ECHO} "$EZERR $1/headeradd does not exist"
	  else
	    ${ECHO} "$1/headeradd:"
	    ${ECHO} "============================"
	    ${CAT} "$1/headeradd"
	    ${ECHO} "============================"
	    ${ECHO}
	  fi
	  if [ ! -r "$1/headerremove" ]; then
	      ${ECHO} "$EZERR $1/headerremove does not exist"
	  else
	    ${ECHO} "$1/headerremove:"
	    ${ECHO} "============================"
	    ${CAT} "$1/headerremove"
	    ${ECHO} "============================"
	    ${ECHO}
	  fi
	  if [ -r "$1/public" ]; then
	    ${ECHO} "... public"
	  else
	    ${ECHO} "... not public"
	  fi
	  if [ -r "$1/archived" ]; then
	    FLAGARCH='1'
	    ${ECHO} "... archived"
	  else
	    ${ECHO} "... not archived"
	  fi
	  if [ -r "$1/indexed" ]; then
	    FLAGARCH='1'
	    FLAGIND='1'
	    ${ECHO} "... indexed"
	  else
	    ${ECHO} "... not indexed"
	  fi
	  if [ "$FLAGARCH" = '1' ]; then
            if [ ! -d "$1/archive" ]; then
	      ${ECHO} "$EZERR $1/archive is not a directory"
	    else
	      if [ ! -r "$1/archive/0/index" ]; then
	        ${ECHO} "$EZERR list is archived, but there is no index."
	        ${ECHO} "    Please run ezmlm-idx!"
	      fi
            fi
          fi
          if [ ! -d "$1/bounce" ]; then
	    ${ECHO} "$EZERR $1/bounce is not a directory"
	  fi
	  if [ -r "$1/prefix" ]; then
	    ${ECHO} "... using $1/prefix as subject prefix:"
	    ${HEAD} -1 < "$1/prefix"
	    ${ECHO}
	  fi
	  if [ -r "$1/sublist" ]; then
	    ${ECHO} "... this is a sublist for:"
	    ${HEAD} -1 < "$1/sublist"
	  else
	    ${ECHO} "... not a sublist. [A digest lists should be"
	    ${ECHO} "    a sublist of the main list.]"
	  fi
          if [ ! -d "$1/text" ]; then
	    ${ECHO} "$EZERR $1/text is not a directory"
	  fi
	  ${ECHO} "... Contents of $1/text not checked"
	  ${ECHO}
	  ${ECHO} "... Links not checked. Should be:"
	  ${ECHO} "    ~/.qmail-{list} -> $1/editor"
	  ${ECHO} "    ~/.qmail-{list}-default -> $1/manager"
	  ${ECHO} "    ~/.qmail-{list}-owner -> $1/owner"
	  ${ECHO} "    ~/.qmail-{list}-return-default -> $1/bouncer"
	  ${ECHO}
	  ${ECHO} "--------------------------------------------------"
	  ${ECHO} "Checking subscribers:"
	  ${ECHO}
          if [ ! -d "$1/subscribers" ]; then
	    ${ECHO} "$EZERR $1/subscribers is not a directory"
	  else
            if [ ! -x "$EZLIST" ]; then
	      ${ECHO} "$EZLIST is not available for listing"
	    else
              if ! ${EZLIST} "$1" | ${GREP} '@'
	      then
	        ${ECHO} "??? No subscribers!"
              fi
            fi
	  fi
	  ${ECHO}
	  ${ECHO} "---------------------------------------------------"
	  ${ECHO} "Checking for subscription moderation/remote admin:"
	  ${ECHO}
	  FLAGMOD='0'
	  if [ -r "$1/remote" ]; then
	    FLAGMOD='1'
	    ${ECHO} "... set up for remote administration"
	    REMOTE=`${CAT} "$1/remote"| ${HEAD} -1`
            if ${ECHO} "$REMOTE" | ${GREP} -q -G "^/"
              then
	        MODDIR="$REMOTE"
	      else
	        MODDIR="$1/mod"
	    fi
	  else
	    ${ECHO} "... no remote admin"
          fi
	  if [ -r "$1/modsub" ]; then
	    FLAGMOD='1'
	    ${ECHO} "... subscription moderated"
	    MODSUB=`${CAT} "$1/modsub"| ${HEAD} -1`
            if ${ECHO} "$MODSUB" | ${GREP} -q -G "^/"
	    then
              MODDIR="$MODSUB"
	    elif [ -z "$MODDIR" ]; then
	      MODDIR="$1/mod"
	    fi
	  else
	    ${ECHO} "... no subscription moderation"
	  fi
	  if [ "$FLAGMOD" = '1' ]; then
	    ${ECHO}
	    ${ECHO} "Mods/remote admins stored based in $MODDIR:"
	    ${ECHO}
	    if [ ! -d "$MODDIR" ]; then
	      ${ECHO} "$EZERR moderator dir $MODDIR doesn't exist!"
	    elif [ ! -w "$MODDIR/lock" ]; then
	      ${ECHO} "$EZERR $MODDIR/lock is not writable to user $USER"
	    elif [ ! -x "$EZLIST" ]; then
	      ${ECHO} "${EZLIST} not available for listing"
	    else
	      if ! ${EZLIST} "$MODDIR" | ${GREP} '@'
	        then
	          ${ECHO} "$EZERR No subscription moderators/remote admins!"
	      fi
	    fi
	    ${ECHO}
	  fi
	  ${ECHO} "---------------------------------------------------"
	  ${ECHO} "Checking for message moderation:"
	  ${ECHO}
	  FLAGMOD='0'
	  if [ -r "$1/modpost" ]; then
	    FLAGMOD='1'
	    MODPOST=`${CAT} "$1/modpost" | ${HEAD} -1`
            if ${ECHO} "$MODPOST" | ${GREP} -q -G "^/"
              then
	      MODDIR="$MODPOST"
	    else
	      MODDIR="$1/mod"
	    fi
	  fi
	  if ${GREP} -q 'ezmlm-store' < "$1/editor" 
          then
	    ${ECHO} "??? it looks from $1/editor like the list is set up"
	    ${ECHO} "    for message moderation. However, since $1/modpost"
            ${ECHO} "    doesn't exist, ezmlm-store posts them directly. If"
            ${ECHO} "    this is not intended, please create $1/modpost."
	    ${ECHO}
	    MODDIR="$1/mod"
	    if [ "$FLAGMOD" = '1' ]; then
	      ${ECHO} "??? $1/modpost exists, leading me to think you'd like"
	      ${ECHO} "    message moderation, but I can't find any call to"
	      ${ECHO} "    ezmlm-store in $1/editor."
	      ${ECHO}
	    fi
	    FLAGMOD='1'
          fi
	  if [ "$FLAGMOD" = '1' ]; then
	    ${ECHO} "... message moderated"
	    ${ECHO}
	    ${ECHO} "Message moderators based in $MODDIR:"
	    ${ECHO}
	    if [ ! -d "$MODDIR" ]; then
	      ${ECHO} "$EZERR moderator dir $MODDIR doesn't exist!"
	    elif [ ! -w "$MODDIR/lock" ]; then
	      ${ECHO} "$EZERR $MODDIR/lock is not writable to user $USER"
	    elif [ ! -x "$EZLIST" ]; then
	      ${ECHO} "${EZLIST} not available for listing"
	    else
	      if ! ${EZLIST} "$MODDIR" | ${GREP} '@'
	        then
	          ${ECHO} "$EZERR No message moderators!"
	      fi
	    fi
	    ${ECHO}
	    MT="120"
	    if [ -r "$1/modtime" ]; then
	      MODTIME=`${CAT} "$1/modtime" | ${HEAD} -1`
	      if [ "$MODTIME" -eq 0 ]; then
		MT="120"
	      elif [ "$MODTIME" -lt 24 ]; then
		MT="24"
	      elif [ "$MODTIME" -gt 240 ]; then
		MT="240"
	      else
		MT="${MODTIME}"
	      fi
	    fi
	    ${ECHO} "... Messages awaiting moderation time out after $MT hours"
	    if [ ! -d "$1/mod/pending" ]; then
	      ${ECHO} "$EZERR $MODDIR/pending is not a directory"
	    else
	      MODNUM=`${LS} "$1/mod/pending" | ${GREP} -c '*'`
	      ${ECHO} "... there are $MODNUM messages awaiting moderator action"
	    fi
	    if [ ! -d "$1/mod/accepted" ]; then
	      ${ECHO} "$EZERR $MODDIR/accepted is not a directory"
	    fi
	    if [ ! -d "$1/mod/rejected" ]; then
	      ${ECHO} "$EZERR $MODDIR/rejected is not a directory"
	    fi
	    if [ ! -r "$1/moderator" ]; then
	      ${ECHO} "$EZERR $1/moderator is not readable to user $USER"
	    else
	      if ! ${GREP} -q 'ezmlm-moderate' < "$1/moderator"
	      then
	        ${ECHO} "$EZERR $1/moderator lacks ezmlm-moderate entry"
	      fi
	      ${ECHO}
	      ${ECHO} "$1/moderator:"
	      ${ECHO} "============================"
	      ${CAT} "$1/moderator"
	      ${ECHO} "============================"
	      ${ECHO} 
	    fi
	    ${ECHO}
	    ${ECHO} "... Links not checked. Should be:"
	    ${ECHO} "    ~/.qmail-{list}-accept-default -> $1/moderator"
	    ${ECHO} "    ~/.qmail-{list}-reject-default -> $1/moderator"
	    ${ECHO}
          else
	    ${ECHO} "... no message moderation"
          fi   
	fi
	${ECHO} "---------------------------------------------------"
	${ECHO}
	${ECHO} "EXT ='$EXT'"
	${ECHO} "EXT1 ='$EXT1'"
	${ECHO} "EXT2 ='$EXT2'"
	${ECHO} "EXT3 ='$EXT3'"
	${ECHO} "EXT4 ='$EXT4'"
	${ECHO} "DTLINE = $DTLINE"
	${ECHO} "RPLINE = $RPLINE"
	${ECHO} "UFLINE = $UFLINE"
	${ECHO} "---------------------------------------------------"
	${ECHO}
	${ECHO} "Hope that helps!"
) | "$RCP" || exit 100

exit 99

