#!/bin/sh -e

action=$1

shout() { echo "$0: $@" >&2; }
barf() { shout "fatal: $@"; exit 111; }
safe() { "$@" || barf "cannot $@"; }

here=`env - PATH=$PATH pwd`
mypwd=${here%package}
mypwd=${mypwd%/}
home=`head -1 ${mypwd}/conf-home`
qmaill=`grep Log ${mypwd}/conf-ids | awk -F: '{print $2}'`

QMQ_HOME=""
if [ -f ${mypwd}/conf-home ]
then
  export QMQ_HOME=`head -1 ${mypwd}/conf-home`
fi
[ -d "${QMQ_HOME}" ] || barf "s/qmail home dir '${QMQ_HOME}' not available."

QMQ_LOGS=""
if [ -f ${mypwd}/conf-log ]
then
  export QMQ_LOGS=`head -1 ${mypwd}/conf-log`
fi
[ -d "${QMQ_LOGS}" ] || barf "s/qmail log dir '${QMQ_LOGS}' not available."

SVC_DIR=""
if [ -f ${mypwd}/conf-svcdir ]
then
  export SVC_DIR=`head -1 ${mypwd}/conf-svcdir`
fi
[ -d "${SVC_DIR}" ] || barf "supervise dir '${SVC_DIR}' not available."

export QMQ_ME=`hostname`

instances=""
if [ -f ${mypwd}/conf-instances ]
then
  myinstances=`grep -v "^#" ${mypwd}/conf-instances | cut -d'#' -f1`
  shout "Setting s/qmail QMQ instances:"
  shout "-------------------------------------------------------------------"
  echo "${myinstances}"
  shout "-------------------------------------------------------------------"
else
  barf "No QMQ instances defined."
fi

if [ -f ${mypwd}/conf-qmq ]
then
  . ${mypwd}/conf-qmq 
  shout "Setting s/qmail QMQ skeleton environment:"
  safe env | grep SKELETON
else 
  barf "No QMQ environment available."
fi


if [ "X${myinstances}" != "X" ] 
then
  shout "--> Use '$0 build' to setup the instances."
  shout "--> Use '$0 conf' to deploy the instances."
  shout "--> Use '$0 all' to setup and deploy the instances."
  shout "-------------------------------------------------------------------"
  shout "Note (1): s/qmail will be installed at '${QMQ_HOME}'." 
  shout "Note (2): s/qmail-logs will be installed at '${QMQ_LOGS}/[qmail|qmtp]-INSTANCEID' ...."
  shout "Note (3): 'service' base directory is '${SVC_DIR}'."
  shout "Note (4): 'qmail-send' will be initially touched 'down' at every instance."
  shout "Note (5): Initial configuration is: 'queuelifetime=${SKELETON_QUEUELIFETIME}', concurrencyremote=${SKELETON_CONCURRENCYREMOTE}'."
  shout "Note (6): Communication from the primary qmail instance to the secondaries is based on 'QMTP'."
  shout "-------------------------------------------------------------------"
  shout "Enter 'ctl-c' to abort; otherwise '$0 $action' will continue in 10 secs."
  sleep 10
fi

if [ "X$action" = "Xbuild" -o "X$action" = "Xall" ] 
then
  safe cp ${mypwd}/conf-home ${mypwd}/conf-home.org

  for mapping in ${myinstances}
  do
     instance=${mapping%%:*}
     ipaddress=$(echo ${mapping} | cut -d':' -f3)
     [ "X${ipaddress}" = "X" ] && ipaddress=0

     QMQ_INST="${QMQ_HOME%/*}/qmail-${instance}"
     shout "Setting up QMQ instance: ${QMQ_INST} (IP: $ipaddress)"

     safe mkdir -p ${QMQ_INST}
     safe mkdir -p ${QMQ_LOGS}/qmail-${instance} 
     safe chown $qmaill ${QMQ_LOGS}/qmail-${instance} 
     safe mkdir -p ${QMQ_LOGS}/qmtp-${instance} 
     safe chown $qmaill ${QMQ_LOGS}/qmtp-${instance} 

     safe cd ${mypwd}/compile
     safe echo "${QMQ_INST}" > ${mypwd}/conf-home
     safe make
     safe ./install
     safe echo "${SKELETON_ME}" > ${QMQ_INST}/control/me
     safe echo "${SKELETON_CONCURRENCYREMOTE}" > ${QMQ_INST}/control/concurrencyremote
     safe echo "#!@:localhost;bounceport -- typically 1090" > ${QMQ_INST}/control/qmtproutes
     [ "${ipaddress}" != "0" ]  && safe echo "*:${ipaddress}" > ${QMQ_INST}/control/domainips
  done

  safe mv ${mypwd}/conf-home.org ${mypwd}/conf-home

  shout "All s/qmail QMQ instances build."
fi

if [ "X$action" = "Xconf" -o "X$action" = "Xall" ] 
then
  for mapping in ${myinstances}
  do
     instance=${mapping%%:*}
     ipaddress=$(echo ${mapping} | cut -d':' -f3)
     [ "X${ipaddress}" = "X" ] && ipaddress=0

     QMQ_INST="${QMQ_HOME%/*}/qmail-${instance}"
     shout "Configurating initially QMQ instance: ${QMQ_INST} (IP address: $ipaddress)"

     port=$((${SKELETON_PORT}+${instance}))
     shout "Selecting port ${port} for instance ${instance} ..."
#
## qmail-send/qmail-start
#
     safe mkdir -p ${QMQ_INST}/svc/qmail-${instance}/log
     safe cp ${mypwd}/service/run_log ${QMQ_INST}/svc/qmail-${instance}/log/run 
     safe chmod +x ${QMQ_INST}/svc/qmail-${instance}/log/run

     safe eval sed -e 's!/var/qmail!${QMQ_INST}!g' \
                    ${mypwd}/service/run_send \
                    > ${QMQ_INST}/svc/qmail-${instance}/run
     safe chmod +x ${QMQ_INST}/svc/qmail-${instance}/run
     safe touch ${QMQ_INST}/svc/qmail-${instance}/down
#
## qmail-qmtpd
#
     safe mkdir -p ${QMQ_INST}/svc/qmtp-${instance}/log
     safe cp ${mypwd}/service/run_log ${QMQ_INST}/svc/qmtp-${instance}/log/run 
     safe chmod +x ${QMQ_INST}/svc/qmtp-${instance}/log/run

     safe eval sed -e 's!/var/qmail!${QMQ_INST}!g' \
                   -e 's!0\ qmtp!$ipaddress\ $port!g' \
                    ${mypwd}/service/run_qmtpd \
                    > ${QMQ_INST}/svc/qmtp-${instance}/run
     safe chmod +x ${QMQ_INST}/svc/qmtp-${instance}/run
     safe touch ${QMQ_INST}/svc/qmtp-${instance}/down

#
## link to /svc
# 
#     safe ln -s ${QMQ_INST}/svc/qmtp-${instance} ${SVC_DIR}/qmail-${instance}-qmtpd
#     safe ln -s ${QMQ_INST}/svc/qmail-${instance} ${SVC_DIR}/qmail-${instance}-send
  done

  shout "All QMQ instances configured, available unter ${SVC_DIR} but toched 'down'."
else
  barf "Please provide either 'build' and 'conf' for individual actions; or 'all' for all-inclusive."
fi

exit 0
