#!/bin/bash

. /usr/share/preupgrade/common.sh
check_applies_to "mysql-server"
check_rpm_to "bash" ""

#END GENERATED SECTION




PLUGINDIR="/usr/lib64/mysql/plugin"

# 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 6 contains MySQL 5.1 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 MariaDB project, see MariaDB Upstream
Web at [link: http://mariadb.org/en/about/].

MariaDB upstream uses the same file names as original MySQL.
That means that the 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 it is not necessary to follow
upstream. It means that the following layout is used in Red Hat Enterprise Linux 7:
 - The MariaDB packages names 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 it 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 the 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, which is not 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,embedded,embedded-devel,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'. Packages requiring other packages
(mysql-server, mysql-embedded, mysql-embedded-devel, mysql-test, or mysql-bench)
need to be rebuilt, so that they start requiring mariadb-* packages
instead. The following dependency issues have been found within the installed
packages:
$(cat $nonrh_mysql_deps_file | uniq)
If you rebuild those packages, or update to a later version, it might
solve this problem.
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 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 problems have been spotted in the services:
$(cat $nonrh_service_deps_file | uniq)
Change all services that require mysqld to require mariadb instead.
EOF
else
    cat >>$SOLUTION_FILE <<EOF

No problems in the services have been found, but check your services anyway.
In case a service requires mysqld in Red Hat Enterprise Linux 6, or it needs to start before
mysqld, set Require=mariadb.service and After=mariadb.service in Red Hat Enterprise Linux 7 for
those services.
EOF
fi


# Get all plugins that are not provided by RH packages
#
# How to test:
# 1) Create a file in $PLUGINDIR/somename.so
# 2) See if the somename.so is reported as an issue

nonrh_plugins=0
nonrh_plugins_file="./nonrh_plugins"
rm -f "$nonrh_plugins_file"
touch "$nonrh_plugins_file"

for plugin in `cd $PLUGINDIR ; ls *.so`; do
    package=`rpm -qf --qf '%{NAME}\n' "$PLUGINDIR/$plugin"`
    if [ $? -ne 0 ] || ! is_dist_native $package; then
        echo " - The $plugin plug-in needs to be rebuilt." >>"$nonrh_plugins_file"
        ((nonrh_plugins++))
    fi
done

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

All plug-ins not delivered by Red Hat Enterprise Linux 6 and compiled for MySQL 5.1 need
to be rebuilt for MariaDB 5.5 after the migration to Red Hat Enterprise Linux 7. The following
potential problems have been spotted in the plug-in directory:
$(cat $nonrh_plugins_file | uniq)
Rebuild those plug-ins or update them to the latest release compatible with MariaDB.
EOF
fi


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

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

# overal test result evaluation
if [ $nonrh_mysql_deps_issues -gt 0 ] || [ $nonrh_service_deps -gt 0 ] || [ $nonrh_plugins -gt 0 ] ; then
    [ $nonrh_mysql_deps_issues -gt 0 ] && log_high_risk "MariaDB RPM packages do not provide mysql names."
    [ $nonrh_service_deps -gt 0 ] && log_high_risk "The mariadb-server package provides a systemd unit file called 'mariadb'."
    [ $nonrh_plugins -gt 0 ] && log_high_risk "All plug-ins not delivered by Red Hat Enterprise Linux 6 and compiled for MySQL 5.1 need to be rebuilt for MariaDB 5.5 after the migration to Red Hat Enterprise Linux 7."
    result=$RESULT_FAIL
else
    result=$RESULT_INFORMATIONAL
fi

exit $result
