#!/bin/sh
# $OpenBSD: INSTALL,v 1.3 2000/03/04 11:57:45 camield Exp $
#
# Written by Camiel Dobbelaar <cd@sentia.nl>, 2000
# This file is in the public domain.

PATH=/bin:/usr/bin:/sbin:/usr/sbin
QMAILDIR=${PREFIX:-$PKG_PREFIX}
NOSHELL=/sbin/nologin

# These may clash with already installed uids/gids.
# They MUST be fixed though, because qmail hardwires them.

QMAILGID=2850
NOFILESGID=32750

ALIASUID=2849
QMAILDUID=2850
QMAILLUID=2851
QMAILPUID=2852
QMAILQUID=2853
QMAILRUID=2854
QMAILSUID=2855

confirm() {
	set -o noglob
	echo -n "[Y] "
	read resp
	case "$resp" in
		y*|Y*|"")
			return
			;;
		*)
			echo "Aborting"
			exit 1
			;;
	esac
	set +o noglob
}

# create_group and create_user work like this:
# 1) if user or group already exist: return (success)
# 2) if uid or gid already exist: abort with exitcode 1 (fail)
# 3) create user or group: abort with exitcode 1 if this operation fails,
#    otherwise return (success) 
# BUGS:
# - no argument checking
# - step 1) could do additional checking on the other password/group field(s)

create_group()
{
        GROUP=$1
        GID=$2

        echo -n "Checking group '$GROUP' with gid '$GID': "

        if grep -q "^$GROUP:" /etc/group; then
                echo "OK, group already exists." >&2
                return
        fi
        cut -f3 -d':' /etc/group | grep -qw $GID
        if [ $? == 0 ]; then
                echo "ERR, gid taken." >&2
                exit 1
        fi
	echo -n "group does not exist. Create? "
	confirm
        echo "$GROUP:*:$GID:" 2>/dev/null >>/etc/group || {
                echo "ERR, cannot append to /etc/group" >&2
                exit 1
	}
        echo "OK, created succesfully." >&2
        return
}

create_user()
{
        NAME=$1;  UID=$2;  GID=$3
        GECOS=$4; HOME=$5; SHELL=$6
        
        echo -n "Checking user '$NAME' with uid '$UID': "
        
        if grep -q "^$NAME:" /etc/passwd; then
                echo "OK, user already exists." >&2
                return
        fi
        cut -f3 -d':' /etc/passwd | grep -qw $UID
        if [ $? == 0 ]; then
                echo "ERR, uid taken." >&2
                exit 1
        fi
	echo -n "user does not exist. Create? "
	confirm
        chpass -l -a "$NAME:*:$UID:$GID::::$GECOS:$HOME:$SHELL" 2>/dev/null || {
                echo "ERR, cannot add user to database" >&2
                exit 1
	}
        echo "OK, created successfully." >&2
        return
}

do_advice()
{
	echo "----------------"
	echo "qmail is installed"
	echo "----------------"
	echo
	echo "qmail does NOT automatically work yet at this point."
	echo
	echo "To activate qmail, please read $QMAILDIR/doc/INSTALL"
	echo "Steps 1, 2, 3 and 5 have been done by this port/package."
	echo "The config command from step 4 can be found in $QMAILDIR/setup"
	echo 
	echo "It is recommended to use tcpserver instead of inetd for qmail-smtpd"
	echo "and/or qmail-pop3d. It is installed as a dependency."
	echo
	echo "If you want to replace Sendmail on your system, be sure to look"
	echo "at mailwrapper(8)."
	echo "Here's a sample /etc/mailer.conf:"
	echo
	echo "sendmail        $QMAILDIR/bin/sendmail"
	echo "send-mail       $QMAILDIR/bin/sendmail"
	echo "mailq           $QMAILDIR/bin/qmail-qread"
	echo " # you will need fastforward for newaliases to work"
	echo "newaliases      $QMAILDIR/bin/newaliases"
	echo " # not sure about these, mail corrections to this port's maintainer"
	echo "hoststat        /usr/bin/true"
	echo "purgestat       /usr/bin/true"
	echo
	echo "Enjoy qmail!"
}

do_aliases()
{
	cd $QMAILDIR/alias
	touch .qmail-postmaster	.qmail-mailer-daemon .qmail-root	
	chmod 644 .qmail-postmaster .qmail-mailer-daemon .qmail-root	
}
		
case $2 in
    PRE-INSTALL)
	create_group qmail   $QMAILGID
	create_group nofiles $NOFILESGID
	create_user alias  $ALIASUID  $NOFILESGID qmail $QMAILDIR/alias $NOSHELL
	create_user qmaild $QMAILDUID $NOFILESGID qmail $QMAILDIR       $NOSHELL
	create_user qmaill $QMAILLUID $NOFILESGID qmail $QMAILDIR       $NOSHELL
	create_user qmailp $QMAILPUID $NOFILESGID qmail $QMAILDIR       $NOSHELL
	create_user qmailq $QMAILQUID $QMAILGID   qmail $QMAILDIR       $NOSHELL
	create_user qmailr $QMAILRUID $QMAILGID   qmail $QMAILDIR       $NOSHELL
	create_user qmails $QMAILSUID $QMAILGID   qmail $QMAILDIR       $NOSHELL
	;;
    POST-INSTALL)
	# Install will abort because it cannot install all files.
	# It will make qmail/queue though, which is what we want.
	sh -c "$QMAILDIR/setup/install >/dev/null 2>&1"
	do_aliases
	do_advice
	;;
    *)
	echo "Usage: `basename $0` distname <PRE-INSTALL|POST-INSTALL>" >&2
	exit 1
	;;
esac

exit 0
