#!/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 netqmail-1.05.tar.gz || cleanupandexit
cd $INSTALL_DIR/netqmail-1.05 || cleanupandexit
./collate.sh || cleanupandexit
cd $INSTALL_DIR/netqmail-1.05/netqmail-1.05 || cleanupandexit
echo "*** Applying netqmail-big-ext-todo-20030101.patch ***"
patch -p1 < $INSTALL_DIR/patches/netqmail-big-ext-todo-20030101.patch || cleanupandexit
#echo "*** Applying qmail-toaster-contegix-0.7.2.patch ***"
#patch -p1 < $INSTALL_DIR/../patches/qmail-toaster-contegix-0.7.2.patch
echo "*** Applying netqmail-1.05-tls-smtpauth-20060105-post-ext-todo.patch ***"
patch -p0 < $INSTALL_DIR/patches/netqmail-1.05-tls-smtpauth-20060105-post-ext-todo.patch || cleanupandexit
echo "*** Applying qmail-tarpit.patch ***"
patch -p0 < $INSTALL_DIR/patches/netqmail-tarpit-post-tls-smtpauth.patch || cleanupandexit
#echo "*** Applying qmail-big-concurrency.patch ***"
#patch -p1 < $INSTALL_DIR/patches/qmail-big-concurrency.patch
#echo "*** Applying qmail-oversized-dns.patch ***"
#patch -p1 < $INSTALL_DIR/patches/qmail-oversized-dns.patch

mkdir /opt/qmail || cleanupandexit
ln -s /opt/qmail /var/qmail || cleanupandexit

groupadd -g 300 nofiles
useradd -u 300 -g nofiles -d /var/qmail/alias alias
useradd -u 301 -g nofiles -d /var/qmail qmaild
useradd -u 302 -g nofiles -d /var/qmail qmaill
useradd -u 303 -g nofiles -d /var/qmail qmailp
groupadd -g 301 qmail
useradd -u 304 -g qmail -d /var/qmail qmailq
useradd -u 305 -g qmail -d /var/qmail qmailr
useradd -u 306 -g qmail -d /var/qmail qmails

# 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

make setup check || cleanupandexit

# host=$(./dnsfq `./hostname` 2>/dev/null) || host=`echo -en "Unable to determine fully qualified hostname.\nHostname: " > /dev/stderr; read host; echo $host`

host=$(./dnsfq `./hostname` 2>/dev/null) || host=`host=$(./hostname); echo -en "Unable to determine fully qualified hostname.  Defaulting to local hostname:\n$host\nPress Enter to continue..." > /dev/stderr; read; echo $host`

./config-fast $host || cleanupandexit
(cd ~alias; touch .qmail-postmaster .qmail-mailer-daemon .qmail-root) || cleanupandexit
chmod 644 ~alias/.qmail* || cleanupandexit
make cert || cleanupandexit
make tmprsadh || cleanupandexit

cp $INSTALL_DIR/misc/rc /var/qmail/ || cleanupandexit
chgrp qmail /var/qmail/rc || 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 "Install Inter7's Vpopmail if you need virtual mail domains which you"
echo "probably will (and even if you don't, it makes life much simpler)"
echo ""
echo "QMail Installation Complete."


