# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../qmail/qmail.conf
# Copyright (C) 2006 - 2013 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

controldir="${controldir:-var/qmail/control}"
sysconfdir="${sysconfdir:-var/qmail/supervise}"
queuedir="${queuedir:-var/qmail/queue}"
logdir="${logdir:-var/qmail/log}"

if [ "$prefix_auto" == "1" ]; then
	prefix="var/qmail"
	set_confopt
	sysconfdir=var/qmail/supervise
fi

pkg_qmail_symlinks() {
	if [ "$1" != "$2" -a ! -e "$root/$2" ]; then
		set -x
		mkdir -vp "$root/$1"
		ln -vs $( echo $2 | sed -e 's,[^/]*/,../,g' | tr -d [a-z]  )$1 $root/$2
		set +x
	fi
}

# compatibility symlinks if needed
#
hook_add preconf 2 'pkg_qmail_symlinks "$prefix"	"var/qmail"'
hook_add preconf 4 'pkg_qmail_symlinks "$controldir"	"var/qmail/control"'
hook_add preconf 4 'pkg_qmail_symlinks "$sysconfdir"	"var/qmail/supervise"'
hook_add preconf 4 'pkg_qmail_symlinks "$queuedir"	"var/qmail/queue"'
hook_add preconf 4 'pkg_qmail_symlinks "$logdir"	"var/qmail/log"'

# the author loves to use head -1 and tail -1
#
hook_add preconf 5 "sed -i \
	-e 's,head -,head -n ,g' \
	-e 's,tail -,tail -n ,g' Makefile"

# make setup
#
hook_add inmake 5 'eval "$MAKE $makeopt man"'

makeinstopt=
hook_add postmake 1 './install'

# install qmail's 'sendmail' wrappers
#
pkg_qmail_sendmailwrappers() {
	ln -sf /$prefix/bin/sendmail $root/usr/bin/sendmail_qmail
	ln -sf /$prefix/bin/qmail-qread $root/usr/bin/mailq_qmail

	# this is part of fastforward but fits better here
	ln -sf /$prefix/bin/newaliases $root/usr/bin/newaliases_qmail

	install_setmailer qmail
}
hook_add postinstall 5 'pkg_qmail_sendmailwrappers'

# profile.d
#
pkg_qmail_profiled() {
	local rc=$root/var/qmail/boot/multi

	cat <<- EOT > $root/etc/profile.d/qmail
	export MANPATH=\$MANPATH:$mandir
	#export PATH=\$PATH:$bindir
	EOT

	if [ ! -e $rc ]; then
		# and multilog rc
		cat <<- EOT > $rc
		#!/bin/sh

		# Without logger, to use multilog outside
		# Using qmail-local to deliver messages according to control/defaultdelivery

		exec env - PATH="/$prefix/bin:\$PATH" \\
		qmail-start \$( cat /$controldir/defaultdelivery )
		EOT
		chmod +x $rc
	fi
	[ ! -e $root/var/qmail/rc ] && \
		ln -vs boot/multi $root/var/qmail/rc || true

}
hook_add postmake 5 'pkg_qmail_profiled'

# fifo is not flisted
hook_add postmake 6 'add_flist $prefix/queue/lock/trigger'

createdoc=0

# WARNING! Patched copies of qmail can't be re-distributed and they are unsupported
#

[ "$SDECFG_PKG_QMAIL_LDAP" == "1" ] && . $confdir/qmail-ldap.conf
[ "$SDECFG_PKG_QMAIL_QUEUE" == "1" ] && var_append patchfiles ' ' "$( match_source_file -p qmailqueue )"
[ "$SDECFG_PKG_QMAIL_QMTP" == "1" ] && var_append patchfiles ' ' "$( match_source_file -p qmtpc )"
[ "$SDECFG_PKG_QMAIL_HOLD" == "1" ] && var_append patchfiles ' ' "$( match_source_file -p hold )"

if [ "$SDECFG_PKG_QMAIL_MILKYPATCH" == "1" ]; then
	var_append patchfiles ' ' "$( match_source_file -p milky )"
	pkg_qmail_no_local_patches=true
fi

if [ ! $pkg_qmail_no_local_patches ]; then
	var_append patchfiles ' ' "$confdir/errno.diff"
	var_append patchfiles ' ' "$confdir/hotfixes.diff"
fi