# functions for handling ASCEDS_POSTFIX=/etc/postfix/main.cf
# 
# asceds-postfix-check "<hostname>"
# checks if <hostname> is configured into main.cf
#    returns 0 if configured, 1 if not configured
# 
# asceds-postfix-add "<hostname>"
# adds <hostname> to the mydestination list in main.cf
#    returns 0 if successful, 1 if not
# 
# asceds-postfix-del "<hostname>"
# deletes <hostname> from the mydestination list in main.cf
#    returns 0 if successful, 1 if not
# 
# 
# 
# 
##################################################################
# checks if <hostname> is configured into main.cf
#    returns 0 if configured, 1 if not configured

# Usage: asceds-postfix-check "<hostname>"
asceds-postfix-check()
{
   local HSTNAME="$1"
   if [ -z "${HSTNAME}" ] ; then
      asceds-error "Empty value for the hostname"
   fi

   if [ -z "${ASCEDS_POSTFIX}" ] ; then
      asceds-echo "No postfix on this system"
      return 0
   fi

   if [ ! -r "${ASCEDS_POSTFIX}" ] ; then
      asceds-warning "Postfix ${ASCEDS_POSTFIX} file not found"
      return 1
   fi

   # nonzero if <hostname> appears in the mydestination= line
   TESTHST1=$( sed -e "s/#.*//g" ${ASCEDS_POSTFIX} | \
            ${GREP} -E "(=|,| )${HSTNAME}( |,|$)" | \
            ${GREP} "^[[:space:]]*mydestination[[:space:]]*=" )
   # nonzero if <hostname> appears in the myhostname= line
   TESTHST2=$( sed -e "s/#.*//g" ${ASCEDS_POSTFIX} | \
            ${GREP} -E "(=|,| )${HSTNAME}( |,|$)" | \
            ${GREP} "^[[:space:]]*myhostname[[:space:]]*=" )
   # nonzero if $myhostname appears in the mydestination= line
   TESTHST3=$( sed -e "s/#.*//g" ${ASCEDS_POSTFIX} | \
            ${GREP} "^[[:space:]]*mydestination[[:space:]]*=" | \
            ${GREP} "[[:space:]]*$myhostname[[:space:]]*" )

   if [ -n "${TESTHST1}" ] ; then
      asceds-echo "${HSTNAME} configured into ${ASCEDS_POSTFIX}"
      return 0
   fi
   if [ -n "${TESTHST2}" ] && [ -n "${TESTHST3}" ] ; then
      asceds-echo "${HSTNAME} configured into ${ASCEDS_POSTFIX}"
      return 0
   else
      asceds-echo "${HSTNAME} not configured into ${ASCEDS_POSTFIX}"
      return 1
   fi
}

##################################################################
# adds <hostname> to the mydestination list in main.cf
#    returns 0 if successful, 1 if not

# Usage: asceds-postfix-add "<hostname>"
# should be used together with asceds-postfix-check to avoid multiple 
# appearances, e.g.
# asceds-postfix-check "<hostname>" || asceds-postfix-add "<hostname>"
asceds-postfix-add()
{
   local HSTNAME="$1"
   if [ -z "${HSTNAME}" ] ; then
      asceds-error "Empty value for the hostname"
   fi

   if [ ! -r "${ASCEDS_POSTFIX}" ] ; then
      asceds-warning "No ${ASCEDS_POSTFIX} file on this system"
      return 1
   fi

   # short hostname
   SHORTHSTNAME=${HSTNAME%%.*}

   # backup the original
   ${CP} -af ${ASCEDS_POSTFIX} ${ASCEDS_POSTFIX}.bak

   # ${HSTNAME} will be added to mydestination
   # no checks if it exists already are performed
   sed -i -e "/^[[:space:]]*mydestination[[:space:]]*=/ s/$/, ${HSTNAME}/" \
      ${ASCEDS_POSTFIX} || return 1
 
   # ${SHORTHSTNAME} will be added to mydestination
   sed -i -e "/^[[:space:]]*mydestination[[:space:]]*=/ s/$/, ${SHORTHSTNAME}/" \
      ${ASCEDS_POSTFIX} || return 1

   postfix reload
   asceds-postfix-check "${HSTNAME}" || return 0
   return 1
}

##################################################################
# deletes <hostname> from the mydestination list in main.cf
#    returns 0 if successful, 1 if not

# Usage: asceds-postfix-del "<hostname>"
asceds-postfix-del()
{
   local HSTNAME="$1"
   if [ -z "${HSTNAME}" ] ; then
      asceds-error "Empty value for the hostname"
   fi

   if [ ! -r "${ASCEDS_POSTFIX}" ] ; then
      asceds-warning "No ${ASCEDS_POSTFIX} file on this system"
      return 1
   fi

   # short hostname
   SHORTHSTNAME=${HSTNAME%%.*}

   # backup the original
   ${CP} -af ${ASCEDS_POSTFIX} ${ASCEDS_POSTFIX}.bak

   # eliminate full hostname
   sed -i -e "/^[[:space:]]*mydestination[[:space:]]*=/ \
      s/,[[:space:]]*${HSTNAME}\b[[:space:]]*//" \
      ${ASCEDS_POSTFIX} || return 1

   # eliminate short hostname
   #sed -i -e "/^\s*mydestination\s*=/ s/,\s*\b${SHORTHSTNAME}\b\s*//g" \
   #   ${ASCEDS_POSTFIX} || return 1

   postfix reload
   asceds-postfix-check "${HSTNAME}" || return 1
   return 0
}

##################################################################
