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


RemovedPkgs=$(mktemp /tmp/.removedpkgsXXX)
cat "$COMMON_DIR"/default*_removed* | grep -v "\.so" | cut -f1 -d' ' | sort | uniq >"$RemovedPkgs"

[ ! -f "$VALUE_RPM_RHSIGNED" ] && \
  log_error "Generic common files are missing." && \
  exit $RESULT_ERROR

[ ! -r "$RemovedPkgs" ] && \
  log_error "Generic part of the module is missing." && \
  exit $RESULT_ERROR

found=0
rm -f solution.txt
echo \
"Some of the packages were removed between Red Hat Enterprise Linux 5 and Red Hat Enterprise Linux 7. This might break
the upgrade for some of your packages. We are not aware of any compatible
replacement for these packages.

The following packages are no longer available:" >solution.txt

#Check for package removals in the comps packages
pkg=
while IFS= read pkg || [ -n "$pkg" ];
do
  #skip non-rh and unavailable packages
  grep -q "^$pkg[[:space:]]" $VALUE_RPM_QA && is_dist_native $pkg || continue
  j=" (required by NonRH signed package(s):"
  for k in $(rpm -q --whatrequires $pkg | grep -v "^no package requires" | \
   rev | cut -d'-' -f3- | rev)
  do
    grep -q "^$k[[:space:]]" $VALUE_RPM_QA || continue
    is_dist_native $k ||  j="$j$k "
  done
  j="${j% })"
  [ "$j" == " (required by NonRH signed package(s):)" ] && j=""
  [ -n "$j" ] && log_high_risk "The $pkg $j package was removed between Red Hat Enterprise Linux 5 and Red Hat Enterprise Linux 7"
  echo "$pkg$j" >>solution.txt
  found=1
done < "$RemovedPkgs"
rm -f "$RemovedPkgs"

grep required solution.txt >>"$VALUE_TMP_PREUPGRADE/kickstart/RemovedPkg-required"
grep -v required solution.txt | grep -v " " | grep -v "^$" >> "$VALUE_TMP_PREUPGRADE/kickstart/RemovedPkg-optional"
grep required "$VALUE_TMP_PREUPGRADE/kickstart/RemovedPkg-required" >/dev/null || rm "$VALUE_TMP_PREUPGRADE/kickstart/RemovedPkg-required"
grep [a-zA-Z] "$VALUE_TMP_PREUPGRADE/kickstart/RemovedPkg-optional" >/dev/null || rm "$VALUE_TMP_PREUPGRADE/kickstart/RemovedPkg-optional"
[ -f "$VALUE_TMP_PREUPGRADE/kickstart/RemovedPkg-required" ] && \
  echo " * RemovedPkg-required - This file contains all Red Hat Enterprise Linux 5 packages that were removed in Red Hat Enterprise Linux 7 and there is no known compatible alternative for them. As some of your packages depend on the removed packages, check the changes closely." >>"$KICKSTART_README"
[ -f "$VALUE_TMP_PREUPGRADE/kickstart/RemovedPkg-optional" ] && \
  echo " * RemovedPkg-optional - This file is similar to the RemovedPkg-required file, but in this case no package that was not signed by Red Hat requires the removed packages. It is more of an informational thing for you, so that you can deal with the unavailability of these packages." >>"$KICKSTART_README"

echo \
"
If some packages not signed by Red Hat require these packages, you may need to ask your
vendor to provide an alternative solution or you might get the missing package from
other sources than Red Hat Enterprise Linux.
" >>solution.txt

[ $found -eq 1 ] && log_medium_risk "\
We detected that some packages installed on the system were removed between Red Hat Enterprise Linux 5 and Red Hat Enterprise Linux 7. This might break the functionality of the packages that depend on them." && exit $RESULT_FAIL

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

exit $RESULT_PASS
