#!/bin/bash


#Preupgrade Assistant performs system upgradability assessment
#and gathers information required for successful operating system upgrade.
#Copyright (C) 2013 Red Hat Inc.
#Petr Hracek <phracek@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`
yaboot="/usr/bin/yaboot"

[ -z $myarch ] && exit $RESULT_ERROR

if [ x"$myarch" != x"ppc64" ];
then
    exit $RESULT_NOT_APPLICABLE
fi

is_pkg_installed "yaboot"

if [ $? -ne 0 ];
then
    log_error "Yaboot RPM not found. You probably do not use yaboot. This is unsupported. The Preupgrade Assistant cannot know for sure which boot mechanism is in use."
    exit_error
fi

is_dist_native yaboot || {
  log_extreme_risk "Yaboot RPM is not signed by Red Hat. This is unsupported."
  exit_fail
}

POSTUPGRADE_DIR="$VALUE_TMP_PREUPGRADE/postupgrade.d/yaboot"
if [[ ! -d "$POSTUPGRADE_DIR" ]]; then
    mkdir -p "$POSTUPGRADE_DIR"
fi
SCRIPT_NAME="postupgrade-yaboot.sh"
POST_SCRIPT="postupgrade.d/$SCRIPT_NAME"
cp -f $POST_SCRIPT $POSTUPGRADE_DIR/$SCRIPT_NAME

DEFAULT_FILE=/etc/default/grub
./cmdline-to-default-grub > default-grub
if [ -e "$DEFAULT_FILE" ] && ! cmp "$DEFAULT_FILE" default-grub; then
    log_error "Wanted to create the $DEFAULT_FILE file but it exists already. Remove it before the execution of preupg. Custom modifications can be made subsequently."
    exit_error
fi

mkdir -p /etc/default/
/bin/cp default-grub $DEFAULT_FILE

log_medium_risk "The /etc/default/grub file generated for this system. Review its content, especially the value of GRUB_CMDLINE_LINUX. Improper values might result in an unbootable system. See the manual page for the GRUB 2 configuration."
exit_fail
