if echo -n | grep n > /dev/null 2>&1; then
	prompt() {
		echo "$*\c"
	}
else
	prompt() {
		echo -n "$*"
	}
fi

if ps auxw > /dev/null 2>&1; then
	PS='ps auxw'
else
	PS='ps -ef'
fi

if (whoami) > /dev/null 2>&1; then
	USER=`whoami`
elif (id) > /dev/null 2>&1; then
	USER=`id | cut -d'(' -f2 | cut -d')' -f1`
# the remaining two tests work only if "su -" was used
# perhaps delete them
elif (logname) > /dev/null 2>&1; then
	USER=`logname`
elif (who am i) > /dev/null 2>&1; then
	USER=`who am i | cut -d' ' -f1 | cut -d'!' -f2`
else
	echo "Could not determine current user name, using 'nobody'"
	USER='nobody'
fi

SQLUSER=''	# must be empty
# Set if we've found bug from old version
BUG=''
# Use subdb plugin
SUBDB=''
# process arguments

while [ ! -z "$1" ]; do		# not everyone has getopt :-(
	case "$1" in
		-s)
			SUBDB="$2"; shift; shift;;
		-d)
			DB="$2"; shift; shift;;
		-h)
			SQLHOST="$2"; shift; shift;;
		-p)
			PW="$2"; shift; shift;;
		-u)
			SQLUSER="$2"; shift; shift;;
		-t)
			TABLE="$2"; shift; shift;;
		--)
			shift; break;;
		*)
			echo "$i"
			echo "Usage: ezmlm-test [-/]" \
				"[-s plugin [-p sqlpassword] [-u sqluser] [-d sqldb]" \
				"[-t sqltable] [-h sqlhost]]"
			echo
			echo "args have to be separated from switches!"
			echo
			echo "defaults: -s [empty - use standard subdb]"
			echo "          -d ezmlm"
			echo "          -h [empty => plugin default]"
			echo "          -p [empty]"
			echo "          -s 1 [run test from beginning]"
			echo "          -t list"
			echo "          -u eztest"
			echo
			exit 99;;
	esac
done

# calculate position in LIST where [normally] default starts
LISTLEN=`echo "$LIST-" | wc -c | sed 's/ //g'`
REJLEN=`echo "$LIST-reject-" | wc -c | sed 's/ //g'`
ACCLEN=`echo "$LIST-accept-" | wc -c | sed 's/ //g'`

DOT="$DIR/dot.qmail-$LIST"

HOST="example.com"

# test addresses.
SND="sender"
MOD="moderator"
DIG="digest"
MAN="manager"
BNC="bouncer"

if [ ! -x "${EZBIN}/ezmlm-make" ]; then
	echo "can't execute ${EZBIN}/ezmlm-make. Most likely, you"
	echo "doen't have execute permission to files in directory"
	echo "``${EZBIN}''. Adjust permissions or execute the script"
	echo "as a user with the appropriate permissions."
	exit 99
fi

########################
# remove old test list #
########################
if [ -n "$SUBDB" ]; then
	${EZBIN}/ezmlm-unsub "$DIR" digest "${MAN}@$HOST" "${DIG}@$HOST" \
		>"$ERR" 2>&1
	${EZBIN}/ezmlm-unsub "$DIR" mod "${MOD}@$HOST" \
		>"$ERR" 2>&1
	${EZBIN}/ezmlm-unsub "$DIR" allow "aaa@bbb" "ccc@ddd" "eee@fff" \
		>"$ERR" 2>&1
fi
rm -rf "$DIR" "$ERR" >/dev/null 2>&1
test -e "$DIR" \
&& fatal "Could not remove old list"

#################
# check version #
#################

THIS_VER=`${EZBIN}/ezmlm-make -V 2>&1` || \
	fatal "This program only works with ezmlm-idx"
THIS_VER=`echo "$THIS_VER" | cut -d' ' -f3`
if [ "$THIS_VER" != "$VER" ]; then
  echo "Warning: These tests were built for $VER but the programs being"
  echo "         tested are version $THIS_VER.  Some errors may result from"
  echo "         this version mis-match."
fi

# Set BADUNSET if unset doesn't do the job

A='a'
export A
unset A
[ -z "$A" ] || BADUNSET='y'

unset DEFAULT

QMAILHOME="$DIR"
QMAILQUEUE="$QQTEST"
EZMLM_BIN="$EZBIN"
EZMLM_ETC="$EZBIN"/lang
EZMLM_LIB="$EZBIN"

export DEFAULT LOCAL QMAILHOME QMAILQUEUE SENDER EZMLM_BIN EZMLM_ETC EZMLM_LIB
