#!/bin/sh

# Simple script to configure/install postfix non-interactively
# on (my) FreeBSD systems.
# This script can be used with Postfix+TLS+IPv6.
# You can add support for DB3, TLS etc. by uncommenting
# lines below. Other options are also available of course.
# Note: PCRE doesn't need to be specified anymore.
# Make sure that system account 'postfix:postfix' and group maildrop
# exist.
# -- Dean Strik <dean@stack.nl>

# TLS -- I don't specify these manually
#PF_CCARGS="$PF_CCARGS -DHAS_SSL -I/usr/include/openssl"
#PF_AUXLIBS="$PF_AUXLIBS -lssl -lcrypto"

# DB3
#PF_CCARGS="$PF_CCARGS -DHAS_DB3 -I/usr/local/include/db3"
#PF_AUXLIBS="$PF_AUXLIBS -L/usr/local/lib -ldb3"

# SASL
#PF_CCARGS="$PF_CCARGS -DUSE_SASL_AUTH -I/usr/local/include/sasl"
#PF_AUXLIBS="$PF_AUXLIBS -L/usr/local/lib -lsasl2 -lpam -lcrypt -ldes"

# PostgreSQL
#PF_CCARGS="$PF_CCARGS -DHAS_PGSQL -I/usr/local/include"
#PF_AUXLIBS="$PF_AUXLIBS -L/usr/local/lib -lpq"

# MySQL
#PF_CCARGS="$PF_CCARGS -DHAS_MYSQL -I/usr/local/include/mysql"
#PF_AUXLIBS="$PF_AUXLIBS -L/usr/local/lib/mysql -lmysqlclient -lz -lm"

MAKE=make
#MAKE=/usr/pkg/bin/gmake
#MAKE=gmake

if [ ! -f postfix-install ] ; then
	echo "Chdir to postfix src root directory first"
	exit 1
fi
PFROOT="`pwd`"

sfatal() {
	echo "Fatal error"
	exit 1
}

case "$1" in
build)
	${MAKE} -f Makefile.init makefiles \
		CCARGS="${PF_CCARGS}" AUXLIBS="${PF_AUXLIBS}" || sfatal
	${MAKE} || sfatal
	;;
install)
	sh postfix-install -non-interactive install_root=/ \
		tempdir=/tmp \
		config_directory=/etc/postfix \
		daemon_directory=/usr/local/libexec/postfix \
		command_directory=/usr/local/sbin \
		queue_directory=/var/spool/postfix \
		sendmail_path=/usr/local/sbin/sendmail \
		newaliases_path=/usr/local/bin/newaliases \
		mailq_path=/usr/local/bin/mailq \
		mail_owner=postfix \
		setgid_group=maildrop \
		manpage_directory=/usr/local/man \
		sample_directory=/etc/postfix/samples \
		readme_directory=/usr/local/share/doc/postfix ||
		sfatal
	;;
upgrade)
	${MAKE} upgrade || sfatal
	;;
replace)
	# This is meant for BSDs that use a mailwrapper
	;;
srcman)
	MANPDIR="${HOME}/man/manp"
	umask 022
	mkdir -p "${MANPDIR}" >/dev/null 2>/dev/null
	if [ ! -d "${MANPDIR}" ] ; then
		echo "Unable to access ${MANPDIR}"
		sfatal
	fi
	cd src || sfatal
	for i in * ; do
		if [ -d "${i}" ] ; then
			cd "${i}" || sfatal
			for j in *.c ; do
				echo "Creating ${MANPDIR}/${j%%.c}.p"
				${PFROOT}/mantools/srctoman ${j} > \
					${MANPDIR}/${j%%.c}.p
			done
			cd .. || sfatal
		fi
	done
	;;
*)
	echo 'Syntax: make.sh { build | install | upgrade | srcman }'
	exit 1
	;;
esac

