#!/bin/sh

INSTALL_DIR=`pwd`
SOFTWARE=netqmail
VERSION=1.05

function cleanupandexit {
  echo "Installation failed.  Look above for errors."
  cd $INSTALL_DIR
  rm -rf ${SOFTWARE}-${VERSION}
  exit 1
}

tar zxf ${SOFTWARE}-${VERSION}.tar.gz || cleanupandexit
cd $INSTALL_DIR/${SOFTWARE}-${VERSION} || cleanupandexit
./collate.sh || cleanupandexit
cd $INSTALL_DIR/${SOFTWARE}-${VERSION}/${SOFTWARE}-${VERSION} || cleanupandexit

# turn on SPF checking
echo 3 > /var/qmail/control/spfbehavior

# turn on mfcheck
echo 1 > /var/qmail/control/mfcheck

cd $INSTALL_DIR/${SOFTWARE}-${VERSION}/${SOFTWARE}-${VERSION}
bunzip2 -cd  ../../patches/qmail-toaster-contegix-0.8.1.patch.bz2 | patch -p0 || cleanupandexit

# Red Hat Kerberos Issue
if test -f /etc/redhat-release; then
  echo "Fixing Red Hat Kerberos Issue"
  sed -e 's/^\(cc.*\)$/\1 -I\/usr\/kerberos\/include/' -i conf-cc
fi

svc -d /service/qmail || cleanupandexit
svc -d /service/smtpd || cleanupandexit

make || cleanupandexit
make setup check || cleanupandexit
make cert || cleanupandexit
make tmprsadh || cleanupandexit
echo ""
echo "Verify the contents of the files in /var/qmail/control at this time"
echo ""
echo "Run install-fake-mta.sh if you want to replace your Sendmail with QMail's"
echo ""
echo "Run install-qmail-conf.sh and the applicable qmail-conf-xxxxx.sh scripts"
echo "to set up the Daemontools services under /service"
echo ""
echo "QMail Installation Complete."


