#!/bin/bash

. /usr/share/preupgrade/common.sh

#END GENERATED SECTION

[ ! -f "$VALUE_RPM_RHSIGNED" ] || [ ! -r "$COMMON_DIR" ] && {
  log_error "Generic common part of the module is missing."
  exit $RESULT_ERROR
}

tmp_log_risk=$([ $UPGRADE -eq 1 ] && echo "log_high_risk" || echo "log_medium_risk")
found=0
rm -f solution.txt >/dev/null
echo \
"Applications developed in the C programming language may use dynamic libraries (.so files) to reuse the
common functions or symbols in the binary. If the library is missing, the applications
will not run. Some of the libraries were removed between Red Hat Enterprise Linux 6 and Red Hat Enterprise Linux 7.

The following libraries from your installed Red Hat Enterprise Linux 6 packages are not present in Red Hat Enterprise Linux 7:
" >solution.txt

RemovedLibs=$(mktemp .removedpkgsXXX --tmpdir=/tmp)
removed_tmp=$(mktemp .removedpkgsXXX --tmpdir=/tmp)
cat "$COMMON_DIR"/default*_so*-removed  | sed -e 's/ removed /:/g' \
  | sort | uniq >"$RemovedLibs"

[ ! -r "$RemovedLibs" ] || [ ! -r "$removed_tmp" ] && {
  rm -f "$RemovedLibs" "$removed_tmp"
  log_error "Generic part of the module is missing."
  exit $RESULT_ERROR
}

#Check for soname removals and report them for RH packages installed
#on the system
while read line; do
  soname_lib="$(echo $line | cut -d':' -f1)"
  for pkg in $(echo $line | cut -d':' -f2 | sed -e 's/,/ /g')
  do
    #skip non-rh and unavailable packages
    is_pkg_installed "$pkg" && is_dist_native "$pkg" || continue
    rq_msg=" (required by packages not signed by Red Hat:"
    for l in $(rpm -q --whatrequires $pkg | grep -v "no package requires" | \
     rev | cut -d'-' -f3- | rev)
    do
      is_pkg_installed "$l" && is_dist_native "$l" || rq_msg="$rq_msg$l "
    done
    rq_msg="${rq_msg% })"


    #### form/text of log messages: ####
    # The <lib> library from the <pkg> package was removed between Red Hat Enterprise Linux 6 and Red Hat Enterprise Linux 7
    # The <lib> library from the <pkg> package (required by packages not signed by Red Hat: <pkg>[, pkg ...]) was removed between Red Hat Enterprise Linux 6 and Red Hat Enterprise Linux 7

    #### text of items in produced list in solution file ###
    # <lib> from <pkg>
    # <lib> from <pkg> (required by packages not signed by Red Hat: <pkg>[, pkg ...])

    [ "$rq_msg" == " (required by packages not signed by Red Hat:)" ] && rq_msg=""
    [ -n "$rq_msg" ] && $tmp_log_risk "The $soname_lib library from the $pkg package$rq_msg was removed between Red Hat Enterprise Linux 6 and Red Hat Enterprise Linux 7."

    echo "$soname_lib from the $pkg package$rq_msg" >>solution.txt

    # do not touch the line below
    echo "$soname_lib from $pkg$rq_msg" >>"$removed_tmp"
    found=1
  done
done < "$RemovedLibs"

grep required "$removed_tmp" | sort | uniq >>"$KICKSTART_DIR/RemovedLibs-required"
grep -v required "$removed_tmp" | grep -v "^$" | sort | uniq >> "$KICKSTART_DIR/RemovedLibs-optional"

rm -f "$removed_tmp" "$RemovedLibs"

echo -n "
 * RemovedLibs-required: This file contains all Red Hat Enterprise Linux 6 libraries that were removed in Red Hat Enterprise Linux 7. As some of your packages depend on these libraries, check for alternative solutions.
 * RemovedLibs-optional: This file is similar to the RemovedLibs-required file, but in this case no package not signed by Red Hat requires these libraries. It is more of an informational thing for you, so that you can deal with the unavailability of these libraries.
 " >>"$KICKSTART_README"

echo \
"
The requirements in packages not signed by Red Hat were checked, but for the non
rpm-packaged binaries check the compatibility list yourself
by using, for example, the 'ldd <binary>' command.
If some of your applications use libraries from the list above, get the .so library from a different place, or search for an alternative.
" >>solution.txt

[ $found -eq 1 ] && log_medium_risk \
 "Some .so libraries installed on the system were removed between Red Hat Enterprise Linux 6 and Red Hat Enterprise Linux 7. This might break the functionality of some of your third-party applications." \
 && exit $RESULT_FAIL

rm -f solution.txt && touch solution.txt

exit $RESULT_PASS
