#!/bin/bash

. /usr/share/preupgrade/common.sh
check_applies_to "sendmail"
check_rpm_to "" ""
#END GENERATED SECTION

function solution()
{
  printf '%s\n\n' "$@" | fold -s | sed 's/ \+$//' >> "$SOLUTION_FILE" || exit_error
}

# Copy your config file from RHEL6 (in case of scenario RHEL6_7) 
# to Temporary Directory
CONFIG_FILE="/etc/sysconfig/sendmail"

[ -f "$CONFIG_FILE" ] ||
  exit_not_applicable





mkdir -p $VALUE_TMP_PREUPGRADE/cleanconf/$(dirname $CONFIG_FILE)
cp $CONFIG_FILE $VALUE_TMP_PREUPGRADE/cleanconf/$CONFIG_FILE

#workaround to openscap buggy missing PATH
export PATH=$PATH:/usr/bin

ret=$RESULT_PASS

if rpm -qV sendmail | grep ".*5.*\s$CONFIG_FILE\$"; then
  solution "You made custom changes to the $CONFIG_FILE file. This configuration file \
will not be automatically upgraded. A new default configuration file will be \
installed as $CONFIG_FILE.rpmnew. If you are satisfied with it, rename \
it to $CONFIG_FILE."
  ret=$RESULT_INFORMATIONAL

  grep -q "DAEMON=" "$CONFIG_FILE" &&
    solution "The DAEMON variable is no longer supported in the $CONFIG_FILE file. Sendmail \
will always run as a daemon."

  grep "DAEMON=" "$CONFIG_FILE" | cut -d"=" -f2 | grep -qi "no" && {
    ret=$RESULT_FAIL
    log_slight_risk "The 'DAEMON=no' option is not supported anymore."
  }

  grep -q "QUEUE=" "$CONFIG_FILE" &&
    solution "The QUEUE variable is no longer supported in the $CONFIG_FILE file. The queue \
processing interval is now specified by SENDMAIL_OPTS in the $CONFIG_FILE file. \
To setup queue processing interval to, for example, one hour, use SENDMAIL_OPTS=\"-q1h\"."

  if ! grep -q "SENDMAIL_OPTS=.*-q[0-9smhdw]\+.*" "$CONFIG_FILE"; then
    log_medium_risk "The mail queue processing interval is not set."
    solution "Your $CONFIG_FILE file does not contain SENDMAIL_OPTS=-qTIME. If you do \
not specify the queue processing interval, the mail queue will be processed only once \
during the sendmail startup, and further emails will not be processed or \
delivered. Update your $CONFIG_FILE file to have SENDMAIL_OPTS=\"-q1h\"."
    ret=$RESULT_FAIL
  fi
fi

solution "To run sendmail with special parameters, you can add \
them to the SENDMAIL_OPTS variable in the $CONFIG_FILE file. Do not remove the queue \
processing interval (for example '-q1h') from the SENDMAIL_OPTS, otherwise your \
further emails will not be processed or delivered."

exit $ret
