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

if [ -z "$myarch" ]; then
    log_error "The architecture cannot be detected."
    exit $RESULT_ERROR
fi

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

POSTUPGRADE_DIR="$VALUE_TMP_PREUPGRADE/postupgrade.d/yaboot"
PRE_UPGRADE_DIR="$VALUE_TMP_PREUPGRADE/preupgrade-scripts/yaboot"
SCRIPT_NAME="postupgrade-yaboot.sh"
POST_SCRIPT="postupgrade.d/$SCRIPT_NAME"

DIRTY_DIR="$VALUE_TMP_PREUPGRADE/dirtyconf"
DEFAULT_FILE="/etc/default/grub"
GENERATED_FILE="${DIRTY_DIR}${DEFAULT_FILE}"

mkdir -p "$POSTUPGRADE_DIR" "$PRE_UPGRADE_DIR"
cp -f "$POST_SCRIPT" "$POSTUPGRADE_DIR/$SCRIPT_NAME"
cp -f "yaboot_check" "$PRE_UPGRADE_DIR/"

is_pkg_installed "yaboot" && is_dist_native "yaboot" || {
    log_high_risk "The yaboot package signed by Red Hat is not installed on the system."
    msg="You are using a boot mechanism that is not supported by Red Hat for your system."
    msg+=" You cannot upgrade to the new system unless you have installed the"
    msg+=" yaboot RPM package signed by Red Hat, and unless your system is configured to"
    msg+=" use this package.\n\n"
    msg+="When you install the required yaboot RPM package for your system to use it to"
    msg+=" boot, run the preupg again to prepare"
    msg+=" your system for the upgrade."
    echo -e "\n$msg" >> solution.txt
    exit_fail
}

# create the directory structure
mkdir -p "$(dirname "$GENERATED_FILE")"

msg=""
log_msg=""
if [ -e "$DEFAULT_FILE" ]; then
    # ok, in this case $GENERATED_FILE is not generated but copied...
    cp -a "$DEFAULT_FILE" "$GENERATED_FILE"
    log_info "The $DEFAULT_FILE has been backed up into the $GENERATED_FILE."

    msg="The $DEFAULT_FILE file exists already. In this case we assume you already"
    msg+=" rewieved its content and it is prepared for in-place upgrade to the"
    msg+=" Red Hat Enterprise Linux 7 system. But because of the crucial"
    msg+=" importance of the validity of the file, make sure that the content"
    msg+=" of the file is as expected."

    log_msg="The $DEFAULT_FILE file has been found on the system already. Check"
    log_msg+=" content of the file for correct settings before upgrade."
else
    ./cmdline-to-default-grub > "$GENERATED_FILE"

    msg="Kernel command-line options were converted to a new Red Hat Enterprise"
    msg+=" Linux 7 format and moved into the $GENERATED_FILE file."
    msg+=" Review its content, especially the value of GRUB_CMDLINE_LINUX and"
    msg+=" modify it in case you find anything incorrect."
    msg+=" Improper values might result in an unbootable system."
    msg+=" See the manual page for the GRUB 2 configuration.\n\n"
    msg+="Then you have to move the file into $DEFAULT_FILE in case you"
    msg+=" want to do an inplace-upgrade to the Red Hat Enterprise Linux 7 system."

    log_msg="The $DEFAULT_FILE file has not been found. Continue with"
    log_msg+=" instructions in the report."
fi

echo -e "\n$msg" >> solution.txt
log_high_risk "$log_msg"
exit_fail

