#! /bin/sh

# local options:  ac_help is the help message that describes them
# and LOCAL_AC_OPTIONS is the script that interprets them.  LOCAL_AC_OPTIONS
# is a script that's processed with eval, so you need to be very careful to
# make certain that what you quote is what you want to quote.

ac_help='
--newaliases[=PATH]	name of newaliases program ($execdir/newaliases)
--vuser[=user.group]	user & group id of virtual host manager (mail)
--vhost[=PATH]		virtual host etcdir ($confdir/virtual)
--vspool=PATH		virtual host mailspool ($spooldir/virtual)'

LOCAL_AC_OPTIONS='
    case "$1" in
    --postoffice=*)	PATH_POSTOFFICE=`echo "$1" | sed -e 's/--postoffice==//'`
			shift;;
    --postoffice)	PATH_POSTOFFICE="$2"
			shift 2;;
    --newaliases=*)	PATH_NEWALIASES=`echo "$1" | sed -e 's/--newaliases=//'`
			shift;;
    --newaliases)	PATH_NEWALIASES="$2"
			shift 2;;
    --vuser=*)		VUSER=`echo "$1" | sed -e 's/--vuser=//'`
			shift;;
    --vuser)		VUSER="$2"
			shift 2;;
    --vhost=*)		PATH_VHOST=`echo "$1" | sed -e 's/--vhost=//'`
			shift;;
    --vhost)		PATH_VHOST="$2"
			shift 2;;
    --vspool=*)		PATH_VSPOOL=`echo "$1" | sed -e 's/--vspool=//'`
			shift;;
    --vspool)		PATH_VSPOOL="$2"
			shift 2;;
    *)			ac_error=T ;;
    esac
'

# load in the configuration file
#
TARGET=vpasswd
. ./configure.inc

AC_INIT $TARGET

AC_PROG_CC

AC_CHECK_HEADERS limits.h || AC_DEFINE "INT_MAX" "1<<((sizeof(int)*8)-1)"

AC_CHECK_FUNCS mmap || AC_FAIL "$TARGET requires mmap()"

AC_CHECK_FUNCS statfs
AC_CHECK_HEADERS sys/vfs.h || AC_CHECK_HEADERS sys/param.h sys/mount.h
AC_CHECK_TYPE socklen_t netdb.h
AC_TYPE_PID_T


# compile a little test program that can handle the many permutations
# of a user/group combo, since there doesn't seem to be a clean way of
# doing it using just system level stuff.
#             username  (uid,gid of this user)
#             user.group (uid of user, gid of group)
#             user.number (uid of user, specified gid)
#             number.group (specified uid, gid of group)
#             number.number (specified uid, gid)
#
cat << \EOF > $$.c
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>
#include <ctype.h>

main(int argc, char **argv)
{
    struct passwd *pwd;
    struct group *grp;

    char *p, *q;

    fprintf(stderr, "%s: UID/GID dumper for configure.sh\n", argv[0]);
    printf("av_UID=; av_GID=;\n");
    if (argc <= 1)
	exit(1);

    for (p = argv[1]; *p && (*p != ':') && (*p != '.'); ++p)
	;

    if (*p) {
	*p++ = 0;
	if ( pwd = getpwnam(argv[1]) )
	    printf("av_UID=%d;\n", pwd->pw_uid);
	else {
	    for (q=argv[1]; isdigit(*q); ++q)
		;
	    if (*q == 0)
		printf("av_UID=%s;\n", argv[1]);
	    else
		exit(1);
	}

	if ( grp = getgrnam(p) )
	    printf("av_GID=%d;\n", grp->gr_gid);
	else {
	    for (q=p; isdigit(*q); ++q)
		;
	    if (*q == 0)
		printf("av_GID=%s;\n", q);
	    else
		exit(1);
	}
    }
    else if (pwd = getpwnam(argv[1]) )
	printf("av_UID=%d;\nav_GID=%d;\n", pwd->pw_uid, pwd->pw_gid);
    else
	exit(1);
    exit(0);
}
EOF

$AC_CC -o uid $$.c
status=$?
rm -f $$.c
test $? -eq 0 || AC_FAIL "Could not compile UID/GID dumper"


VUSER=${VUSER:-mail}

eval `./uid $VUSER`

rm -f ./uid

if [ "$av_UID" -a "$av_GID" ]; then
    AC_DEFINE VUSER_UID $av_UID
    AC_SUB    VUSER_UID $av_UID
    AC_DEFINE VUSER_GID $av_GID
    AC_SUB    VUSER_GID $av_GID
else
    AC_FAIL "Virtual host spool owner $VUSER does not exist"
fi


DB=
if [ -z "$USE_GDBM" ]; then
    if AC_CHECK_HEADERS ndbm.h; then
	if QUIET AC_CHECK_FUNCS dbm_open || AC_LIBRARY dbm_open -ldb; then
	    LOG "Found dbmopen()" ${AC_LIBS:+in ${AC_LIBS}}
	    AC_SUB NDBM ndbm
	    DB=ndbm
	fi
    fi
fi

if [ -z "$DB" ]; then
    if AC_CHECK_HEADERS gdbm.h; then
	if QUIET AC_CHECK_FUNCS gdbm_open || AC_LIBRARY gdbm_open -lgdbm; then
	    LOG "Found gdbm_open()" ${AC_LIBS:+in ${AC_LIBS}}
	    AC_SUB NDBM gdbm
	    DB=gdbm
	fi
    fi
fi

test -z "$DB" && AC_FAIL "$TARGET requires ndbm"

AC_CHECK_FLOCK || AC_DEFINE NO_FLOCK

AC_CHECK_HEADERS pwd.h grp.h ctype.h

if [ -z "$PATH_VHOST" ]; then
    PATH_VHOST=$AC_CONFDIR/virtual
fi
if [ -z "$PATH_VSPOOL" ]; then
    PATH_VSPOOL=$AC_SPOOLDIR/virtual
fi

test -d $PATH_VHOST || LOG "WARNING! vhost directory $PATH_VHOST does not exist"
test -d $PATH_VSPOOL || LOG "WARNING! vhost directory $PATH_VSPOOL does not exist"

AC_DEFINE PATH_VSPOOL  \"$PATH_VSPOOL\"
AC_SUB    PATH_VSPOOL  $PATH_VSPOOL
AC_DEFINE PATH_VHOST   \"$PATH_VHOST\"
AC_SUB    PATH_VHOST   $PATH_VHOST

AC_PROG_LN_S

if test -z "$PATH_NEWALIASES" ; then
    AC_DEFINE PATH_NEWALIASES	\"$AC_EXECDIR/newaliases\"
    AC_SUB    PATH_NEWALIASES	$AC_EXECDIR/newaliases
else
    AC_DEFINE PATH_NEWALIASES   \"$PATH_NEWALIASES\"
    AC_SUB    PATH_NEWALIASES   $PATH_NEWALIASES
fi

AC_OUTPUT Makefile vpasswd.1 valias.1 vpasswd.5

