#!/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
}

function createusersgroups {
  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
}

function prepare_libdomainkeys {
  tar zxf $INSTALL_DIR/libdomainkeys-0.68.tar.gz
  cd libdomainkeys-0.68
  make
  cd $INSTALL_DIR
}

function preparesource {
  tar zxf $INSTALL_DIR/netqmail-1.05.tar.gz || cleanupandexit
  cd netqmail-1.05 || cleanupandexit
  ./collate.sh || cleanupandexit
  cd netqmail-1.05 || cleanupandexit
  export NETQMAIL_DIR=`pwd`
  echo "*** Applying netqmail-big-ext-todo-20030101.patch ***"
  patch -p1 < $INSTALL_DIR/patches/netqmail-big-ext-todo-20030101.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
  echo "*** Applying qmail-send-concurrencyperip-post-big-ext-todo ***"
  patch -p1 < $INSTALL_DIR/patches/qmail-send-concurrencyperip-post-big-ext-todo.patch

  # Tune conf-spawn
  if test -f $NETQMAIL_DIR/conf-spawn; then
    echo "Tuning conf-spawn"
    sed -e 's/^1000$/509/' -i conf-spawn
  fi

  # Tune conf-split
  if test -f $NETQMAIL_DIR/conf-split; then
    echo "Tuning conf-split"
    sed -e 's/^23$/53/' -i conf-split
  fi
}

function prepare_libdomainkeys_source {
  cd $INSTALL_DIR
  mv $INSTALL_DIR/netqmail-1.05/netqmail-1.05 $INSTALL_DIR/libdomainkeys-0.68/
  cd $INSTALL_DIR/libdomainkeys-0.68/netqmail-1.05
  echo "*** Applying qmail-1.03-dk-0.54-post-big-ext-todo ***"
  patch -p1 < $INSTALL_DIR/patches/qmail-1.03-dk-0.54-post-big-ext-todo.patch
}

function createdir {
  mkdir /opt/qmail
  ln -s /opt/qmail /var/qmail
}

function buildqmail {
  make setup check
}

function build_libdomainkeys {
  make qmail-dk
  cp qmail-dk /var/qmail/bin/
  cp qmail-dk.8 /var/qmail/man/man8/
  chown qmailq /var/qmail/bin/qmail-dk
  chmod 4711 /var/qmail/bin/qmail-dk
}


function configureqmail {
  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
  cp $INSTALL_DIR/misc/rc /var/qmail/ || cleanupandexit
  chgrp qmail /var/qmail/rc || cleanupandexit

  echo 30 >  /var/qmail/control/concurrencyperip
}

prepare_libdomainkeys || cleanupandexit
preparesource || cleanupandexit
prepare_libdomainkeys_source || cleanupandexit
createdir || cleanupandexit
createusersgroups || cleanupandexit
buildqmail || cleanupandexit
build_libdomainkeys || cleanupandexit
configureqmail || 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."




