#!/bin/sh

### Defaults
DB='ezmlm'
TABLE='list'

### Switches
while [ ! -z "$1" ]; do		# not everyone has getopt :-(
	case "$1" in
		-d)
			DB="$2"; shift; shift;;
		-t)
			TABLE="$2"; shift; shift;;
		--)
			shift; break;;
		*)
			break;;
	esac
done;

if [ -z "$3" -o "$1" = "-?" ]; then
	  echo "usage: ezmlm-grant [-d database] [-t table] host user password"
	  echo "       args have to be separated from switches!"
	  exit 100
fi


### Arguments
HOST="$1"
USR="$2"
PASS="$3"

cat <<EOF

use ${DB};
grant usage on ezmlm.* to ${USR}@${HOST};
set password for '${USR}'@'${HOST}' = password('${PASS}');
grant SELECT,DELETE on ${DB}.${TABLE} to '${USR}'@'${HOST}';
grant SELECT,DELETE on ${DB}.${TABLE}_allow to '${USR}'@'${HOST}';
grant SELECT,DELETE on ${DB}.${TABLE}_digest to '${USR}'@'${HOST}';
grant INSERT on ${DB}.${TABLE}_slog to '${USR}'@'${HOST}';
grant INSERT on ${DB}.${TABLE}_allow_slog to '${USR}'@'${HOST}';
grant INSERT on ${DB}.${TABLE}_digest_slog to '${USR}'@'${HOST}';
grant SELECT,INSERT on ${DB}.${TABLE}_cookie to '${USR}'@'${HOST}';
grant SELECT,INSERT on ${DB}.${TABLE}_mlog to '${USR}'@'${HOST}';
grant SELECT,INSERT on ${DB}.${TABLE}_digest_cookie to '${USR}'@'${HOST}';
grant SELECT,INSERT on ${DB}.${TABLE}_digest_mlog to '${USR}'@'${HOST}';

EOF

exit 0
