#!/bin/sh
# $OpenBSD: INSTALL,v 1.4 2000/09/15 12:08:20 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()
{
        GROUP=$1
        GID=$2

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

	groupinfo -e $GROUP && {
               	echo "OK, group already exists, but gid not checked." >&2
               	return
	}
	groupinfo -e $GID && {
                echo "ERR, gid taken." >&2
                exit 1
	}
	echo -n "group does not exist. Create? "
	confirm
	groupadd -g $GID $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': "
       
	userinfo -e $NAME && {
                echo "OK, user already exists, but uid not checked." >&2
                return
	}
	userinfo -e $UID && {
                echo "ERR, uid taken." >&2
                exit 1
	}
	echo -n "user does not exist. Create? "
	confirm
        useradd -c $GECOS -d $HOME -g $GID -s $SHELL -u $UID $NAME || {
                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). 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 "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
