#!/bin/sh

#Author:   john@stilen.com
#Title:    qmail_generate_rcpthosts.sh
#
#Purpuse:  to generate the qmail control/rcpthosts file,
#          to include secondary mx domains from a flat file.
#
# Each time the script is run, the old rcpthosts is backed up to /tmp/rcpthosts.`date`
#
#Dependentcy veriables: 
  qmail_base_dir="/var/qmail"
  hosts_to_second_flat_file="/full/path/to/my_flat_file.txt"
  new_rcpthosts_temp_file="/tmp/rcpthosts.temp"


####################################
# Sanity checking 
####################################
  # check to see if they configured the variables
  if [ $hosts_to_second_flat_file = "/full/path/to/my_flat_file.txt" ]; then
      echo "please set variables at top of script for your environment.  Exiting"
      exit; 
  fi
  # check to see if qmail_base_dir exists.
  if [ ! -d "$qmail_base_dir" ]; then
      echo "Qmail base dir not set correctly or missing ( $qmail_base_dir ). Exiting"
      exit;
  fi
  # check to see if the control/virtualdomains file exists and readable.
  if [ ! -r "$qmail_base_dir/control/virtualdomains" ]; then
      echo "Cannnot read $qmail_base_dir/control/virtualdomains. Exiting"
      exit;
  fi
  # check to see if the control/locals file exists and readable.
  if [ ! -r "$qmail_base_dir/control/locals" ]; then
      echo "Cannnot read $qmail_base_dir/control/locals. Exiting"
      exit;
  fi
  # check to see if control/rcpthosts file exists and is writeable.  
  if [ ! -w "$qmail_base_dir/control/rcpthosts" ]; then
      echo "Cannnot write to $qmail_base_dir/control/rcpthosts. Exiting"
      exit;
  fi
####################################
# Move into qmail control directory
####################################
pushd $qmail_base_dir/control > /dev/null
####################################
# Sanity checking 
####################################
  # check to see if the $hosts_to_second_flat_file file exists and readable.
  if [ ! -r "$hosts_to_second_flat_file" ]; then
      echo  "Cannot read $hosts_to_second_flat_file. Check path and permissions. Exiting"
      exit;
  fi  
####################################
# Make the new_rcpthosts_temp_file
####################################
  # If new _rcpthosts_temp_file does not exist, create it.
  if [ ! -f  "$new_rcpthosts_temp_file" ]; then
      touch $new_rcpthosts_temp_file
  fi
####################################
# Sanity checking 
####################################
  # check to see if $new_rcpthosts_temp_file file is writeable. 
  if [ ! -w "$new_rcpthosts_temp_file" ]; then
      echo "Cannnot write to $new_rcpthosts_temp_file. Exiting"
      exit;
  fi  
####################################
# Create new rcpthosts file.
####################################
  sed 's/:.*//' < virtualdomains     | \
  cat - locals                       | \
  cat - $hosts_to_second_flat_file   | \
  grep -v '^$'                       | \
  sort > $new_rcpthosts_temp_file

####################################
# More Sanity checking
####################################
  # check to see if the new  file is non-zero
  if [ ! -s "$new_rcpthosts_temp_file" ]; then
      echo "New rcpthosts file is 0 bites in size. Exiting"
      exit;
  fi
  
####################################
# Finally put it in place
####################################
  # Backup old file
  cp $qmail_base_dir/control/rcpthosts /tmp/rcpthosts.`date +%Y-%m-%e-%H:%M`
  # Set permissions on new rcpthosts file
  chmod 644 $new_rcpthosts_temp_file
  # Move new rcpthosts into place
  mv $new_rcpthosts_temp_file $qmail_base_dir/control/rcpthosts

  
