#!/bin/bash
. /usr/share/preupgrade/common.sh

#END GENERATED SECTION


rm -f solution.txt
touch solution.txt

pkg_chks=0
for pkg in rsyslog rsyslog7
do
    if is_pkg_installed "$pkg";then
        if ! is_dist_native "$pkg";then
            log_info "The $pkg package is not signed by Red Hat."
            pkg_chks=$((pkg_chks+1))
        fi
    else
       log_info "The $pkg package is not installed."
       pkg_chks=$((pkg_chks+1)) 
    fi
done
if [ "$pkg_chks" -gt 1 ];then
      exit $RESULT_NOT_APPLICABLE
fi

print_info() {
  echo "
See the following pages for more information about a new logging system in Red Hat Enterprise Linux 7, and for solutions of possible compatibility problems.

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/s1-interaction_of_rsyslog_and_journal.html
http://www.rsyslog.com/doc/v7-stable/compatibility/index.html" >> solution.txt
}

check_spool_files(){
  ttmp="$(grep -E '^[[:space:]]*\$WorkDirectory'  /etc/rsyslog.conf |\
    sed -r 's/^[[:space:]]\$WorkDirectory[[:space:]]+([^[:space:]#]+).*$/\1/')"
  [ -n "$ttmp" ] || ttmp="/var/lib/rsyslog"

  [ -d "$ttmp" ] || return 0
  [ -n "$(ls -A "$ttmp")" ] || return 0

  # some spool files in work directory
  log_high_risk "Some spool files were found in the $ttmp directory."
  echo -e "Some spool files were found in the $ttmp directory. The upgrade might
fail because of them. Remove these files if the data inside is not important
for you, or process them before the upgrade.\n" >> solution.txt

  return 1
}

tmp="$(grep rsyslog "$VALUE_CONFIGCHANGED")" &&
declare -a tmp=$(echo "$tmp" | awk '{print $3}')

[ $? -eq 0 ] && {
  log_medium_risk "Some rsyslog configuration files are changed. Certain manual action will be needed."
  echo -e "The original configuration files are not compatible with the new format and options.
The files printed below are changed, and they cannot be
updated automatically:

${tmp[@]}

The original versions of the configuration files have been backed up to the $VALUE_TMP_PREUPGRADE/dirtyconf/ directory.
" >> solution.txt

printf '%s\n' ${tmp[@]} | while IFS=' ' read -r config
do
 if [[ -e "$config" ]];then
    cp -a --parents "$config" "$VALUE_TMP_PREUPGRADE/dirtyconf"
 fi
done

  check_spool_files
  print_info

  exit $RESULT_FAIL
}

check_spool_files || { print_info; exit $RESULT_FAIL; }

print_info
exit $RESULT_INFORMATIONAL
