#!/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
#END GENERATED SECTION

myarch=$(arch)

dst_arch="x86_64"
_symlink_dir=$(basename $(dirname $( readlink "$COMMON_DIR/default_kept-pkgs")))

case $myarch in
  i386|i686)
    dst_arch="x86_64"
    is_32b=0
    ;;
  ppc)
    dst_arch="ppc64"
    is_32b=0
    ;;
  s390)
    dst_arch="s390x"
esac

info_msg="Cross architecture migration is a new feature that we are still testing. Any feedback is gratefully welcomed. It helps us with future development."
rem_instr="
For the correct migration you have to run the Preupgrade Assistant again with the '--dst-arch' option, for example:
  # preupg --dst-arch $dst_arch
"

#strange output of myarch? Bad luck...
[ -z "$myarch" ] && exit $RESULT_ERROR
#Do we have 32 bit architecture?
if [ $myarch == "i686" ] || [ $myarch == "ppc" ] || [ $myarch == "s390" ] || [ $myarch == "i386" ];
then
  [ "$myarch" == "$_symlink_dir" ] && {
    # option --dst-arch wasn't used
    log_high_risk "Migration to 32-bit systems is not supported. If you want to migrate, specify a destination to a 64-bit architecture relevant for your current architecture to 'preupg' by the '--dst-arch' option."
    echo "$rem_instr" >> solution.txt
  }
  # try find lm lahf_lm flags -> just "lm\b" is enough
  cat /proc/cpuinfo | grep "flags" | grep -q "lm\b" >/dev/null && {
    log_medium_risk "Your system has 64-bit capabilities, but the installation is 32-bit."
    log_info "$info_msg"
    exit_fail
  }
  log_high_risk "Your system has only 32-bit capabilities. Red Hat Enterprise Linux 7 32-bit installations are not supported."
  log_high_risk "Migration to Red Hat Enterprise Linux 7 could be done only on a machine with new hardware."
  log_info "$info_msg"
  exit_fail
fi
if [ $myarch == "x86_64" ] || [ $myarch == "s390x" ] || [ $myarch == "ppc64" ];
then
  exit $RESULT_PASS
fi
#Should not happen, but for safety.
log_extreme_risk "Invalid architecture $myarch - this is not supported for Red Hat Enterprise Linux 5."
exit $RESULT_ERROR
