#!/bin/sh
#
# This script is called when ppp connects to the network.
#        
# Here is where we'll start sendmail if needed, and will
# run the queue in either case.
#
# Written By Richard Nelson <cowboy@debian.org>
#
# NOTE: The following lines (without the #) must be in /etc/mail/sendmail.mc:
# include(`/etc/mail/dialup.m4')dnl
# include(`/etc/mail/provider.m4')dnl
#
# ALSO: make sure ipparam is specified somewhere in the ppp options;
#       it will be used as the name of a file in /etc/mail/peers
#
# Exit by default, check for validity before commenting out the next line:
exit 0

# Define our domain name (from PPP) for sendmail.

# These variables are for the use of the scripts run by run-parts
#PPP_IFACE="$1"
#PPP_TTY="$2"
#PPP_SPEED="$3"
#PPP_LOCAL="$4"
#PPP_REMOTE="$5"
#PPP_IPPARAM="$6"

if [ ! -z "$PPP_LOCAL" ]; then
   addr=$PPP_LOCAL
   provider=$PPP_IPPARAM
else
   addr=$1
   provider=$2
   fi

# Determine our fqdn from our ISP
maxloop=20
cntr=0
name=""
until (test ! -z "$name");  do
   cntr=$(($cntr+1))
   name=$(nslookup $addr | grep '^Name:' | awk '{print $2}')
   test=$(echo $name | cut -d ' ' -f 1)
   if [ "$name" != "**" ]; then
      break
   elif (($cntr > $maxloop)); then
      name=""
      break
      fi
   done

if [ ! -z "$name" ]; then
   file="/etc/mail/dialup.m4"
   echo "LOCAL_CONFIG" >> $file
   echo "#------------------------------------------------------------" >> $file
   echo "#" > $file
   echo "# Dynamic updates from $0" >> $file
   echo "#" >> $file
   echo "# Define our true hostname (from our ISP) becomes \$j" >> $file
   echo "define(\`confDOMAIN_NAME', \`$name')dnl" >> $file 
   echo "# Make sure we accept mail as this name (for bounces, etc)" >> $file
   echo "Cw$name" >> $file
   echo "# Add our hostname to class G for genericstable support" >> $file
   echo "CG$name" >> $file
   echo "#------------------------------------------------------------" >> $file
   fi

# Add smarthost information (if any)... But not if provider.m4 is a link !
if [ -f /etc/mail/peers/$provider -a ! -L /etc/mail/provider.m4 ]; then
   file="/etc/mail/provider.m4"
   echo "LOCAL_CONFIG" >> $file
   echo "#------------------------------------------------------------" >> $file
   echo "#" > $file
   echo "# Dynamic updates from $0" >> $file
   echo "#" >> $file
   echo "# Provider information from /etc/mail/peers/$provider" >> $file
   cat /etc/mail/peers/$provider >> $file
   echo "#------------------------------------------------------------" >> $file
   fi

# Build a new sendmail.cf from sendmail.mc, including our address.
# NOTE: The following lines (without the #) must be in /etc/mail/sendmail.mc:
# include(`/etc/mail/dialup.m4')dnl
# include(`/etc/mail/provider.m4')dnl
m4 /usr/share/sendmail/sendmail.cf/m4/cf.m4 /etc/mail/sendmail.mc \
        >/etc/mail/sendmail.cf.pnew
chmod 0640 /etc/mail/sendmail.cf.pnew
chown root.mail /etc/mail/sendmail.cf.pnew
mv -f /etc/mail/sendmail.cf.pnew /etc/mail/sendmail.cf

# Purge any latent host status that might cause us to *NOT* send mail
purgestat

# Start/reload sendmail as needed
/etc/init.d/sendmail reload        # may be up, or down

# Process the sendmail queue (background so as to not defer other ip-up work)
runq &
