abinfo "Setting up build environment ..."
RPATH="/usr/lib/postfix"
if ! ab_match_archgroup retro; then
    abinfo "Getting PostgreSQL include and library paths ..."
    PGINC="$(pg_config --includedir)"
    PGLIB="$(pg_config --libdir)"
    export CCARGS='
           -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl
           -DHAS_LDAP
           -DUSE_TLS
           -DHAS_PGSQL '"-I$PGINC"'
           -DHAS_SQLITE
           -DHAS_CDB
           -DDEF_COMMAND_DIR=\"/usr/lib/postfix/alternatives\"
           -DDEF_DAEMON_DIR=\"/usr/lib/postfix/bin\"
           -DDEF_SENDMAIL_PATH=\"/usr/lib/postfix/alternatives/sendmail\"
           -DDEF_README_DIR=\"/usr/share/doc/postfix\"
           -DDEF_MANPAGE_DIR=\"/usr/share/man\"
    '
    RPATH="$RPATH:$PGLIB"
    export AUXLIBS="-L$PGLIB -lsasl2 -lldap -llber -lssl -lcrypto -lz -lm -lpq -lsqlite3 -lpthread -lcdb"
    if [[ "${CROSS:-$ARCH}" != "riscv64" ]]; then
        export CCARGS+=' -DHAS_MYSQL -I/usr/include/mysql'
        export AUXLIBS+=" -lmysqlclient"
    fi
else
    export CCARGS='
           -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl
           -DUSE_TLS
           -DHAS_CDB
           -DDEF_COMMAND_DIR=\"/usr/lib/postfix/alternatives\"
           -DDEF_DAEMON_DIR=\"/usr/lib/postfix/bin\"
           -DDEF_SENDMAIL_PATH=\"/usr/lib/postfix/alternatives/sendmail\"
           -DDEF_README_DIR=\"/usr/share/doc/postfix\"
           -DDEF_MANPAGE_DIR=\"/usr/share/man\"
    '
    # Since the build system doesn't accept CPPFLAGS ...
    if ab_match_archgroup 32bit ; then
        export CCARGS="$CCARGS -D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64"
    fi
    export AUXLIBS="-lsasl2 -lssl -lcrypto -lz -lm -lpthread -lcdb"
fi

abinfo "Generating Makefile ..."
make makefiles \
	DEBUG='' \
	shared=yes \
	CCARGS="${CCARGS//$'\n'/}" \
	AUXLIBS="${AUXLIBS//$'\n'/}" \
	OPT="${CFLAGS} ${LDFLAGS}" \
        SHLIB_RPATH="-Wl,-rpath,$RPATH"

abinfo "Building Postfix ..."
make

abinfo "Installing Postfix ..."
sed -e 's:bin/postconf -dhx:bin/postconf -c conf -dhx:g' \
    -i "$SRCDIR"/postfix-install
LD_LIBRARY_PATH=lib:$LD_LIBRARY_PATH \
sh "$SRCDIR"/postfix-install \
    -non-interactive install_root="$PKGDIR"

abinfo "Installing man pages ..."
for name in posttls-finger {smtp,qmqp}-{sink,source}; do
    install -Dvm644 "$SRCDIR"/man/man1/${name}.1 \
        "$PKGDIR/usr/share/man/man1/${name}.1"
    install -Dvm755 "$SRCDIR"/bin/${name} \
        "$PKGDIR/usr/lib/postfix/alternatives/${name}"
done

abinfo "Tweaking post-install script for further configuration..."
sed -r \
    -e '/override=1/d' \
    -e '/absolute path name/d' \
    -e 's/\$POSTCONF -/$POSTCONF -c .\/etc\/postfix -/g' \
    -e '/(if \[|chmod|chown|find|mkdir|test)/s/\$path/.\/$path/g' \
    "$SRCDIR"/libexec/post-install \
    > "$SRCDIR"/post-install

abinfo "Creating and tweaking default Postfix configuration ..."
(
    cd "$PKGDIR"
    LD_LIBRARY_PATH=usr/lib/postfix:$LD_LIBRARY_PATH \
    sh "$SRCDIR"/post-install \
        command_directory=usr/lib/postfix/alternatives \
        config_directory=etc/postfix \
        meta_directory=etc/postfix \
        setgid_group=75 \
        mail_owner=73 \
        set-permissions
)
sed -e 's/^\(\$manpage[^:]*\):/\1.gz:/' -i "$PKGDIR"/etc/postfix/postfix-files

abinfo "Preparing for executable alternatives ..."
rm -rv "$PKGDIR"/usr/bin
for i in mailq newaliases; do
    ln -sv ../alternatives/sendmail \
        "$PKGDIR"/usr/lib/postfix/alternatives/$i
done
