#!/bin/bash


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

# This check can be used if you need root privilegues
check_root

# this function will be provided by API in future
is_pkg_installed()
{
  grep -q "^$1[[:space:]]" $VALUE_RPM_QA || return 1
  return 0
}

# if any relevant package is installed and it's provided by Red Hat
# process the script - otherwise ends as not-applicable
not_applicable=1
for pkg in mysql-server mysql55-mysql-server;
do
  is_pkg_installed "$pkg" && is_dist_native "$pkg" || continue
  not_applicable=0
  break
done

[ $not_applicable -ne 0 ] && exit_not_applicable


# provide general information about changes between MySQL and MariaDB
# How to test:
# 1) see the text if it is formatted well
cat >>$SOLUTION_FILE <<EOF
Red Hat Enterprise Linux 5 contains MySQL 5.0 as a default MySQL implementation.
Red Hat Enterprise Linux 7 contains MariaDB 5.5 as a default MySQL implementation.
MariaDB is a community-developed drop-in replacement for MySQL.
For more information about the MariaDB project, see MariaDB Upstream
Web at [link: http://mariadb.org/en/about/].

MariaDB upstream uses the same file names as the original MySQL.
That means that MariaDB shell is called mysql, MariaDB daemon is called
mysqld_safe and the client library is called libmysqlclient.so.

To keep MariaDB packages properly distinguished from the original
MySQL packages, Red Hat Enterprise Linux 7 uses MariaDB names where not necessary to follow
upstream. It means that the following layout is used in Red Hat Enterprise Linux 7:
 - The MariaDB packages are called mariadb, mariadb-libs, mariadb-server
   and so on.
 - Only the mariadb and mariadb-libs packages provide also RPM symbols 'mysql'
   and 'mysql-libs'; the rest of the packages do not provide alternative mysql names.
 - The systemd unit file is called 'mariadb.service'.
 - The log file is called 'mariadb.log' and is located in the /var/log/mariadb/
   directory by default. You can change the name and the location in the
   /etc/my.cnf file after installation, but do not forget to adjust SELinux
   accordingly.
 - The logrotate script is called 'mariadb'.
EOF


# check if there are some packages that require mysql-server and other
# missing symbols
#
# How to test:
# 1) install a package that requires mysql-server and is not signed by RH
# 2) observe if such a package is reported by Preupgrade Assistant
nonrh_mysql_deps_issues=0
nonrh_mysql_deps_file="./deps"
rm -f "$nonrh_mysql_deps_file"
touch "$nonrh_mysql_deps_file"

# checks if given packages, that require some of the mysql-xxx package,
# are not signed with RH key. If not, they are added to the list.
# Usa: check_packages_signed MYSQL_PACKAGE DEP_PACKAGE [ DEP_PACKAGE , ... ]
check_packages_signed(){
    ret=0
    required="$1" ; shift
    while [ -n "$1" ] ; do
        if ! is_dist_native $1; then
            ret=1
            echo " - the $1 package requires $required that is not available in Red Hat Enterprise Linux 7" >>"$nonrh_mysql_deps_file"
        fi
        shift
    done
    return $ret
}

# loop through all packages that do not provide mariadb-xxx alternative
for package in mysql-{server,test,bench} mysql55-mysql-{server,test,bench} ; do
    dependencies="`rpm -q --qf '%{NAME}\n' --whatrequires $package`"
    if [ $? -eq 0 ] ; then
        check_packages_signed "$package" $dependencies
        nonrh_mysql_deps_issues=$(($? + nonrh_mysql_deps_issues))
    fi
done

if [ $nonrh_mysql_deps_issues -gt 0 ] ; then
    cat >>$SOLUTION_FILE <<EOF

MariaDB RPM packages do not provide mysql names except 'mariadb',
'mariadb-libs' and 'mariadb-devel'. The packages that require the other packages
(mysql-server, mysql-test, or mysql-bench)
will need to be rebuilt, so they will start requiring mariadb-* packages
instead. The following dependency issues have been found within the installed
packages:
$(cat $nonrh_mysql_deps_file | uniq)
Rebuild those packages or update to a newer version, which could
fix this issue.
EOF
fi

# Get all services that require mysqld service started
#
# How to test:
# 1) Create service that requires mysqld to be started before, using LSB
#    header; this service is not part of the RH package
# 2) See if the service is reported as an issue
cat >>$SOLUTION_FILE <<EOF

The mysql-server package provides a SysV init script called 'mysqld'.
The mariadb-server package provides a systemd unit file called 'mariadb'.
All the packages that need to start after the mariadb daemon need to use
the correct name, which is 'mariadb.service'.
EOF

nonrh_service_deps=0
nonrh_service_deps_file="./service_deps"
rm -f "$nonrh_service_deps_file"
touch "$nonrh_service_deps_file"

for servicename in `grep -lie 'Required-Start:.*mysqld' /etc/rc.d/init.d/*`; do
    package=`rpm -qf --qf '%{NAME}\n' "$servicename"`
    if [ $? -ne 0 ] || ! is_dist_native $package ; then
        echo " - SysV init script $servicename requires mysqld service" >>"$nonrh_service_deps_file"
        ((nonrh_service_deps++))
    fi
done

if [ $nonrh_service_deps -gt 0 ] ; then
    cat >>$SOLUTION_FILE <<EOF

The following potential issues have been spotted in services:
$(cat $nonrh_service_deps_file | uniq)
Change all services that require 'mysqld' so that they require 'mariadb' instead.
EOF
else
    cat >>$SOLUTION_FILE <<EOF

No issues in services have been found, but check your services anyway.
In case some services require mysqld in Red Hat Enterprise Linux 5, or they need to start before
mysqld, set 'Require=mariadb.service' and 'After=mariadb.service' in Red Hat Enterprise Linux 7 for
those services.
EOF
fi


# give some general advice
cat >>$SOLUTION_FILE <<EOF

For more information about migration to MariaDB, see
[link:https://access.redhat.com/site/articles/2123151].
EOF

# overal test result evaluation
if [ $nonrh_mysql_deps_issues -gt 0 ] || [ $nonrh_service_deps -gt 0 ] ; then
    [ $nonrh_mysql_deps_issues -gt 0 ] && log_medium_risk "MariaDB RPM packages do not provide mysql names."
    [ $nonrh_service_deps -gt 0 ] && log_medium_risk "The mariadb-server package provides a systemd unit file called 'mariadb'."
    result=$RESULT_FAIL
else
    result=$RESULT_INFORMATIONAL
fi

exit $result
