#!/bin/bash


#Preupgrade Assistant performs system upgradability assessment
#and gathers information required for successful operating system upgrade.
#Copyright (C) 2013 Red Hat Inc.
#Ondrej Vasik <ovasik@redhat.com>, Petr Stodulka <pstodulk@redhat.com>
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
. /usr/share/preupgrade/common.sh
check_rpm_to "" ""
#END GENERATED SECTION

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

FILENAME_BASIS="SonameBumpedLibs"
BumpedLibs=$(mktemp /tmp/.BumpedLibsXXX)
sonamed_tmp=$(mktemp /tmp/.sonamed_tmpXXX)
found=0

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
}


rm -f solution.txt >/dev/null
echo \
"Applications developed in C may use dynamic libraries (.so files) to reuse the
common functions or symbols in the binary. If the library bumped its soname (
changed its 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 5 and Red Hat Enterprise Linux 7.

The lists of bumped or downgraded soname libraries from your Red Hat Enterprise Linux 5
packages are available in these files:
 [link:${KICKSTART_DIR#$VALUE_TMP_PREUPGRADE/}/${FILENAME_BASIS}-required]
 [link:${KICKSTART_DIR#$VALUE_TMP_PREUPGRADE/}/${FILENAME_BASIS}-optional]

For more info about these files, see $KICKSTART_README.
" >solution.txt


#Check for soname bumps and report them for RH packages installed on the system
line=
while IFS= read line || [ -n "$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" || {
      log_debug "$pkg was skipped"
      continue
    }
    pkgs_msg=""

    rq_msg=" (required by NonRH signed package(s):"
    for l in $(rpm -q --whatrequires $pkg | grep -v "no package requires" | \
     rev | cut -d'-' -f3- | rev | sort | uniq)
    do
      is_pkg_installed "$k" || continue
      is_dist_native "$l" || rq_msg="$rq_msg$l "
    done
    rq_msg="${rq_msg% })"

    [ -n "$npkgs" ] && [[ "$pkg" !=  "$npkgs" ]] \
      && pkgs_msg=" (on RHEL 7 available in: $npkgs)"
    [ "$rq_msg" == " (required by NonRH signed package(s):)" ] && rq_msg=""
    [ -n "$rq_msg" ] && log_medium_risk "The $pkg$rq_msg library changed its soname between RHEL 5 and RHEL 7${pkg_msg}"
    echo "$old_lib from $pkg$rq_msg changed to $new_lib$pkg_msg" >>"$sonamed_tmp"
    found=1
  done
done < "$BumpedLibs"

grep required "$sonamed_tmp" >>"$KICKSTART_DIR/${FILENAME_BASIS}-required"
grep -v required "$sonamed_tmp" | grep -v "^$" >> "$KICKSTART_DIR/${FILENAME_BASIS}-optional"

rm -f "$sonamed_tmp" "$BumpedLibs"

echo -n "
 * ${FILENAME_BASIS}-required - This file contains all Red Hat Enterprise Linux 5 libraries from your system where the soname version changed. As some of your packages depend on the listed libraries, you will need to rebuild each package against the new version of the relevant library.
 * ${FILENAME_BASIS}-optional - This file is similar to the ${FILENAME_BASIS}-required file, but in this case no package not signed by Red Hat requires any of the listed libraries. It is more of an informational thing for you, so that you can deal with a potential required rebuild.
 " >>"$KICKSTART_README"

echo \
"
We checked the requirements in packages not signed by Red Hat, but for the non
RPM-packaged binaries check the compatibility list yourself
by using, for example, the 'ldd <binary>' command.
If any of your applications use a library from the list above, you will
need to rebuild such a package or application against the new library.
Red Hat Enterprise Linux applications available in Red Hat Enterprise Linux 7 will handle
these bumps automatically during the migration to a new Red Hat Enterprise
Linux version because the applications were already built against these libraries.
" >>solution.txt

[ $found -eq 1 ] && log_medium_risk "\
 We detected some soname bumps in the libraries installed on the system. This might break the functionality of some of your third party applications. They might need a rebuild, so check their requirements." && \
 exit $RESULT_FAIL

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

exit $RESULT_PASS
