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

KeptLibs=$(mktemp .KeptLibsXXX --tmpdir=/tmp)
MovedReplacedLibs=$(mktemp .MovedReplacedLibsXXX --tmpdir=/tmp)
cat "$COMMON_DIR"/default*_soversioned-kept "$COMMON_DIR"/default*_so-kept >$KeptLibs
cat "$COMMON_DIR"/default*_so*-moved_* "$COMMON_DIR"/default*_so*obsoleted >$MovedReplacedLibs

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


found=0
results="$KICKSTART_DIR/NoSonameBumpLibs"
rm -f "$results" >/dev/null
echo -n \
"#This is an autogenerated file by the Preupgrade Assistant. DO NOT CHANGE!
#
#This file contains a list of libraries installed on the system where
#the soname is available even on the Red Hat Enterprise Linux 7 system. Therefore Red Hat Enterprise Linux 6 applications
#depending only on these libraries might be used in Red Hat Enterprise Linux 7 without being rebuilt.
#
#But some of these libraries are available inside different packages on the new
#Red Hat Enterprise Linux 7 system or packages are available inside different repositories
#(the information about moving the packages between repositories is a part
#of other modules for RPMs). Affected libraries are delimited from the first
#group by another comment-line.
" >"$results"

#Check for kept sonames and report them for RH packages installed on the system
while read line; do

  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
    echo "$(echo "$line" | cut -d':' -f1) from $pkg kept." >>"$results"
    found=1
  done

done < "$KeptLibs"

# and kept packages which are inside different package on new system
# or package moved to another repository between OSs
echo "#libs below have changed a package or repository" >> "$results"
while read line; do
  pkgs_msg=""
  npkgs="$(echo "$line" | cut -d ":" -f2 | cut -sd "|" -f2)"
  for pkg in $(echo "$line" | cut -d ":" -f2 | cut -d "|" -f1 | sed "s/,/ /g")
  do
    is_pkg_installed "$pkg" && is_dist_native "$pkg" || continue
    [ -n "$npkgs" ] && [[ "$pkg" !=  "$npkgs" ]] \
      && pkgs_msg=" (in Red Hat Enterprise Linux 7 available in: $npkgs)"
    echo "$(echo "$line" | cut -d':' -f1) from $pkg kept$pkgs_msg " >>"$results"
  done
done < "$MovedReplacedLibs"

rm -rf "$KeptLibs" "$MovedReplacedLibs"

echo -n "
 * NoSonameBumpLibs: This informational file displays all sonames of libraries, where the soname is the same in Red Hat Enterprise Linux 6 and Red Hat Enterprise Linux 7. If your binaries not signed by Red Hat depend on these libraries only, they might work without being rebuilt after the upgrade.
" >>"$KICKSTART_README" && exit $RESULT_INFORMATIONAL

exit $RESULT_PASS
