#!/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
}

BumpedLibs=$(mktemp .BumpedLibsXXX --tmpdir=/tmp)
sonamed_tmp=$(mktemp .sonamed_tmpXXX --tmpdir=/tmp)
cat $(ls "$COMMON_DIR"/default*_soversioned*bumped* | grep -v debug) \
  | sort | uniq  > "$BumpedLibs"

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

# when migrate only - set just medium risk
# NOTE: May it should be  always only medium risk in both cases, but don't know
# if some troubles can appear during upgrade in some unknwown-special cases
# e.g. some pkg from 3rd party repository needs old lib and break transaction
# - should be discussed/tested before this next change
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 a library bumped its soname (
changed major version, API/ABI incompatibility), the applications that depend on
the library might not run.
Some of the libraries changed their soname version between Red Hat Enterprise
Linux 6 and Red Hat Enterprise Linux 7.

The following libraries from your Red Hat Enterprise Linux 6 packages changed their soname in Red Hat Enterprise Linux 7:
" >solution.txt


#Check for soname bumps and report them for RH packages installed on the system
while read line; do
  npkgs="$(echo "$line" | cut -d ":" -f2 | cut -sd "|" -f2)"
  old_lib="$(echo "$line" | cut -d':' -f1)"
  new_lib="$(echo "$line" | cut -d':' -f3)"

  for pkg in $(echo $line | cut -d':' -f2 | cut -d "|" -f1 | sed -e 's/,/ /g')
  do
    #skip non-rh and unavailable packages
    is_pkg_installed "$pkg" && is_dist_native "$pkg" || {
      rpm -q $pkg >/dev/null 2>&1 \
        && log_debug "The $pkg package was skipped - installed but not signed." \
        || log_debug "The $pkg package was skipped."
      continue
    }
    pkgs_msg=""

    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" || continue
      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 changed its soname between Red Hat Enterprise Linux 6 and Red Hat Enterprise Linux 7
    # The <lib> library from the <pkg> package changed its soname between Red Hat Enterprise Linux 6 and Red Hat Enterprise Linux 7 (in Red Hat Enterprise Linux 7 available in: <pkg>[, pkg ...])
    # The <lib> library from the <pkg> package (required by packages not signed by Red Hat: <pkg>[, pkg ...]) changed its soname 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 ...]) changed its soname between Red Hat Enterprise Linux 6 and Red Hat Enterprise Linux 7 (in Red Hat Enterprise Linux 7 available in: <pkg>[, pkg ...])

    #### text of items in produced list in solution file ###
    # <lib> from the <pkg> package changed to <lib>
    # <lib> from the <pkg> package changed to <lib> (in Red Hat Enterprise Linux 7 available in: <pkg>[, pkg ...])
    # <lib> from the <pkg> package  (required by packages not signed by Red Hat: <pkg>[, pkg ...]) changed to <lib>
    # <lib> from the <pkg> package  (required by packages not signed by Red Hat: <pkg>[, pkg ...]) changed to <lib> (in Red Hat Enterprise Linux 7 available in: <pkg>[, pkg ...])

    [ -n "$npkgs" ] && [[ "$pkg" !=  "$npkgs" ]] \
      && pkgs_msg=" (in Red Hat Enterprise Linux 7 available in: $npkgs)"
    [ "$rq_msg" == " (required by packages not signed by Red Hat:)" ] && rq_msg=""
    [ -n "$rq_msg" ] && $tmp_log_risk "The $old_lib library from the $pkg package$rq_msg changed its soname between Red Hat Enterprise Linux 6 and Red Hat Enterprise Linux 7${pkgs_msg}"
    echo "$old_lib from the $pkg package$rq_msg changed to $new_lib$pkgs_msg" >>solution.txt

    # do not touch the line below
    echo "$old_lib from $pkg$rq_msg changed to $new_lib$pkgs_msg" >>"$sonamed_tmp"
    found=1
  done
done < "$BumpedLibs"

grep required "$sonamed_tmp" >>"$KICKSTART_DIR/SonameBumpedLibs-required"
grep -v required "$sonamed_tmp" | grep -v "^$" >> "$KICKSTART_DIR/SonameBumpedLibs-optional"

rm -f "$sonamed_tmp" "$BumpedLibs"

echo -n "
 * SonameBumpedLibs-required: This file contains all Red Hat Enterprise Linux 6 libraries from your system where the soname version changed. As some of your packages depend on these libraries, rebuild these packages against their corresponding library.
 * SonameBumpedLibs-optional: This file is similar to the SonameBumpedLibs-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 a potential required rebuild.
 " >>"$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 the library on the list above, rebuild such a package or application against the new library.
Applications available in Red Hat Enterprise Linux 7 will handle these bumps automatically because they were built against these libraries already.
" >>solution.txt

[ $found -eq 1 ] && log_medium_risk "\
 Some soname bumps in the libraries installed on the system were detected, which might break the functionality of some of your third-party applications. They might need to be rebuilt, so check their requirements." && \
 exit $RESULT_FAIL

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

exit $RESULT_PASS
