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

if [ ! -f "$VALUE_RPM_RHSIGNED" ]; then
    log_error "The $VALUE_RPM_RHSIGNED file is required."
    exit_error
fi

PREUPG_PREDIR="$VALUE_TMP_PREUPGRADE/preupgrade-scripts/"

PACKAGE_LIST="modcluster cluster clustermon corosync luci pacemaker pcs rgmanager ricci openais foghorn ccs cluster-glue"

CLUSTER_CONFIG="/etc/cluster/cluster.conf"
COROSYNC_CONFIG="/etc/corosync/corosync.conf"

found=0
packages=""
for pkg in $PACKAGE_LIST;
do
    is_pkg_installed $pkg && is_dist_native $pkg
    if [ $? -eq 0 ]; then
        log_info "The $pkg package is installed."
        packages="$packages $pkg"
        found=1
    fi
done
if [ $found -eq 1 ]; then
    log_high_risk "High Availability Add-On packages are installed. The upgrade is not possible."
    echo "If you do not use the following cluster&HA related packages:$packages , uninstall them and run the Preupgrade Assistant again." >> "$SOLUTION_FILE"
    # create pre-ugprade script to inhibit inplace upgrade till user keeps
    # installed any package from $PACKAGE_LIST
    cp "pre-hacluster.sh" "$PREUPG_PREDIR/pre-hacluster.sh"
    sed -i "s/<PLACE_HOLDER>/$packages/" "$PREUPG_PREDIR/pre-hacluster.sh"
    chmod +x "$PREUPG_PREDIR/pre-hacluster.sh"
    exit_fail
fi

if [ -f "$CLUSTER_CONFIG" ] || [ -f "$COROSYNC_CONFIG" ]; then
    log_high_risk "High Availability Add-On config files $CLUSTER_CONFIG or $COROSYNC_CONFIG exist. The upgrade is not possible."
    exit_fail
fi

exit_pass

