#!/bin/bash

#Preupgrade Assistant performs system upgradability assessment
#and gathers information required for successful operating system upgrade.
#Copyright (C) 2013 Red Hat Inc.
#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
#END GENERATED SECTION

rm -f solution.txt
touch solution.txt

#[ -f "/etc/redhat-release" ] || {
#  log_error "Could not determine version. Is this really Red Hat Enterprise Linux 5?"
#  exit_error
#}

[ -f "versions" ] || {
  log_error "The 'versions' file was not found."
  exit $RESULT_ERROR
}

[ -f "rpmdev-sort" ] && [ -f "rpmdev-vercmp" ] || {
  log_error "A crucial part of the module is missing."
  exit $RESULT_ERROR
}

# first line in versions contains last RHEL 5 release
rhel_latest=$(cat versions | head -n 1 | grep "#\s*release:" | grep -oE "5\.[0-9][0-9]?")

[ -n "$rhel_latest" ] || {
  log_error "wrong file format: versions"
  exit $RESULT_ERROR
}

QUERY_FORMAT="%{NAME}-%{VERSION}-%{RELEASE}\n"
MISSING_VARIANT_MSG="The variant and release of your system were not detected.
Migration without required packages is not supported. Install
the redhat-release package and then run 'preupg' again. You can use the command:

# yum install redhat-release && preupg
"

local_log_risk="log_high_risk"
ERROR_MSG="For the best result of migration to a new system it is recommended
(but not required) to update your system to the Red Hat Enterprise Linux $rhel_latest release first.
Then run 'preupg' again.

Migration from older versions might cause some unexpected issues, which are not
(and will not be) handled by our scripts."

check_variant_release() {
  # check if the system is the last release version of RHEL 5 (5.11)
  rhel_version=$(cat /etc/redhat-release | sed -r "s/[a-zA-Z ]+([0-9.]+).*/\1/")
  [ "$rhel_version" != "$rhel_latest" ] && {
    $local_log_risk "This is not the latest Red Hat Enterprise Linux $rhel_latest release."
    echo "$ERROR_MSG" >> solution.txt
    exit $RESULT_FAIL
  }
}

if [ -f "/etc/redhat-release" ]; then
  check_variant_release
else
  # set just high risk - we don't support this when issue will be discovered, but user can do that
  log_high_risk "The system variant and release were not detected. The redhat-release package is missing."
  echo "$MISSING_VARIANT_MSG" >> solution.txt
  exit $RESULT_FAIL
fi

# check NVR of some core packages
line=
while IFS= read line || [ -n "$line" ]; do
  echo $line | grep -q "^\s*#"
  [ $? -eq 0 ] && continue # comment lines
  [ -n "$line" ] || continue # empty line

  pkgname=$(echo $line | cut -d "=" -f 1 | sed -e "s/^\s*//" -e "s/\s*$//")
  pkg_nvr=$(echo $line | cut -d "=" -f 2 | sed -e "s/^\s*//" -e "s/\s*$//")

  # head because of multilib pkgs..you know.. x86_64 and i686 versions installed at once...
  pkg_nvr_installed=$(rpm -q --qf "${QUERY_FORMAT}" $pkgname | ./rpmdev-sort | tail -n1)
  [ $? -ne 0 ] && {
    log_error "The $pkgname package is not installed or there might be another problem with the rpm utility."
    exit $RESULT_ERROR
  }

  ./rpmdev-vercmp "$pkg_nvr" "$pkg_nvr_installed" > /dev/null
  status=$?
  [ $status -ne 0 ] && [ $status -ne 11 ] && [ $status -ne 12 ] && {
    log_error "rpm versions cannot be compared"
    exit $RESULT_ERROR
  }

  [ $status -eq 11 ] && {
    $local_log_risk "This is not the latest Red Hat Enterprise Linux 5 release."
    echo "$ERROR_MSG" >> solution.txt
    exit $RESULT_FAIL
  }
done < versions

PREUPGRADE_DIR="$VALUE_TMP_PREUPGRADE/preupgrade-scripts"
RELEASE_FILE="release_version"
if [[ -f "$COMMON_DIR/$RELEASE_FILE" ]]; then
    cp $COMMON_DIR/$RELEASE_FILE $PREUPGRADE_DIR/$RELEASE_FILE
fi

exit $RESULT_PASS

