##
##  mysql.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package version
%define       V_major   5.7
%define       V_minor   32
%define       V_mysql   %{V_major}.%{V_minor}
%define       V_opkg    %{V_major}.%{V_minor}

#   package information
Name:         mysql
Summary:      Fast Relational Database Management System
URL:          http://www.mysql.com/products/mysql/
Vendor:       ORACLE
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Database
License:      GPL
Version:      %{V_opkg}
Release:      20221222

#   package options
%option       with_server      yes
%option       with_innobase    yes
%option       with_archive     no
%option       with_blackhole   no
%option       with_federated   no
%option       with_heap        no
%option       with_ndbcluster  no
%option       with_partition   no
%option       with_charset     utf8
%option       with_collation   utf8_unicode_ci

#   list of sources
Source0:      https://dev.mysql.com/get/Downloads/MySQL-%{V_major}/mysql-%{V_mysql}.tar.gz
Source1:      my.cnf
Source2:      my.pwd
Source3:      rc.mysql
Patch0:       mysql.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, perl, make, cmake, gcc, gcc::with_cxx = yes
PreReq:       OpenPKG, openpkg >= 20160101, perl, x509
BuildPreReq:  zlib, readline, ncurses, boost >= 1.68.0, libevent, openssl11
PreReq:       zlib, readline, ncurses, boost >= 1.68.0, libevent, openssl11

%description
    MySQL is a multi-user Relational Database Management System (RDBMS),
    which is controlled through Structured Query Language (SQL)
    operating in full multi-threading mode. The main goals of MySQL are
    speed, robustness and ease of use. MySQL was originally developed
    because of the need for a SQL server that could handle very big
    databases with magnitude higher speed than what any database vendor
    could offer.

%track
    prog mysql = {
        version   = %{V_mysql}
        url       = http://dev.mysql.com/downloads/mysql/%{V_major}.html
        regex     = mysql-(\d+\.\d+\.\d+)
    }

%prep
    %setup -q -n mysql-%{V_mysql}
    %patch -p0

%build
    #   fix shebang on supplemental Perl scripts
    rm -rf scripts/systemd
    find . -name "*.orig" -print | xargs rm -f
    %{l_shtool} subst \
        -e 's;^#! */usr/bin/perl;#!%{l_prefix}/bin/perl;' \
        scripts/*

    #   configure source tree
    %{l_shtool} subst \
        -e 's;-O3;-O1;g' \
        cmake/build_configurations/compiler_options.cmake
    mkdir build
    cd build
    cmake \
        -Wno-dev \
        -DCMAKE_BUILD_TYPE="Release" \
        -DCMAKE_C_COMPILER="%{l_cc}" \
        -DCMAKE_C_FLAGS="%{l_cflags -O} -I`pwd`/../include -I`pwd`/include %{l_cppflags openssl11 ncurses .} -Wno-deprecated-declarations -Wno-implicit-fallthrough" \
        -DCMAKE_CXX_COMPILER="%{l_cxx}" \
        -DCMAKE_CXX_FLAGS="%{l_cxxflags -O} -I`pwd`/../include -I`pwd`/include %{l_cppflags openssl11 ncurses .} -Wno-deprecated-declarations -Wno-implicit-fallthrough" \
        -DCMAKE_EXE_LINKER_FLAGS="%{l_ldflags openssl11 .}" \
        -DCMAKE_INSTALL_PREFIX="%{l_prefix}" \
        -DFORCE_UNSUPPORTED_COMPILER=ON \
        -DSYSCONFDIR="%{l_prefix}/etc/mysql" \
        -DINSTALL_MYSQLDATADIR="var/mysql/db" \
        -DINSTALL_MYSQLSHAREDIR="share/mysql" \
        -DINSTALL_MYSQLKEYRINGDIR="etc/mysql" \
        -DINSTALL_PLUGINDIR="libexec/mysql/plugin" \
        -DINSTALL_SHAREDIR="share/mysql" \
        -DMYSQL_UNIX_ADDR="%{l_prefix}/var/mysql/run/mysqld.sock" \
        -DDISABLE_SHARED=ON \
        -DWITH_RAPID=OFF \
        -DWITH_UNIT_TESTS=OFF \
        -DWITH_SSL="%{l_prefix}" \
        -DWITH_ZLIB="system" \
        -DWITH_LIBEVENT="system" \
        -DOPENSSL_INCLUDE_DIR="%{l_prefix}/include/openssl11" \
        -DOPENSSL_LIBRARY="%{l_prefix}/lib/openssl11/libssl.a" \
        -DCRYPTO_LIBRARY="%{l_prefix}/lib/openssl11/libcrypto.a" \
        -DLIBEVENT_INCLUDE_PATH="%{l_prefix}/include" \
        -DLIBEVENT_LIB_PATHS="%{l_prefix}/lib" \
%if "%{with_innobase}" == "yes"
        -DWITH_INNOBASE_STORAGE_ENGINE=ON \
%else
        -DWITH_INNOBASE_STORAGE_ENGINE=OFF \
%endif
%if "%{with_archive}" == "yes"
        -DWITH_ARCHIVE_STORAGE_ENGINE=ON \
%else
        -DWITH_ARCHIVE_STORAGE_ENGINE=OFF \
%endif
%if "%{with_blackhole}" == "yes"
        -DWITH_BLACKHOLE_STORAGE_ENGINE=ON \
%else
        -DWITH_BLACKHOLE_STORAGE_ENGINE=OFF \
%endif
%if "%{with_federated}" == "yes"
        -DWITH_FEDERATED_STORAGE_ENGINE=ON \
%else
        -DWITH_FEDERATED_STORAGE_ENGINE=OFF \
%endif
%if "%{with_heap}" == "yes"
        -DWITH_HEAP_STORAGE_ENGINE=ON \
%else
        -DWITH_HEAP_STORAGE_ENGINE=OFF \
%endif
%if "%{with_ndbcluster}" == "yes"
        -DWITH_NDBCLUSTER_STORAGE_ENGINE=ON \
%else
        -DWITH_NDBCLUSTER_STORAGE_ENGINE=OFF \
%endif
%if "%{with_partition}" == "yes"
        -DWITH_PARTITION_STORAGE_ENGINE=ON \
%else
        -DWITH_PARTITION_STORAGE_ENGINE=OFF \
%endif
        -DDEFAULT_CHARSET="%{with_charset}" \
        -DDEFAULT_COLLATION="%{with_collation}" \
%if "%{with_server}" != "yes"
        -DWITHOUT_SERVER=ON \
%endif
        ..

    #   build source tree
    %{l_make} %{l_mflags -O}

%install
    #   perform standard installation procedure
    (   cd build
        %{l_make} %{l_mflags} install \
            DESTDIR=$RPM_BUILD_ROOT
    ) || exit $?

    #   cleanup mysql_config script
    %{l_shtool} subst \
        -e 's;^\(ldflags=.\).*\(.\)$;\1%{l_ldflags}\2;' \
        $RPM_BUILD_ROOT%{l_prefix}/bin/mysql_config

    #   rename some utilities with too generic names
    for prog in lz4_decompress zlib_decompress innochecksum replace resolveip resolve_stack_dump perror; do
        mv  $RPM_BUILD_ROOT%{l_prefix}/bin/$prog \
            $RPM_BUILD_ROOT%{l_prefix}/bin/mysql_$prog
        mv  $RPM_BUILD_ROOT%{l_prefix}/man/man1/$prog.1 \
            $RPM_BUILD_ROOT%{l_prefix}/man/man1/mysql_$prog.1
    done

    #   strip installation area
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* 2>/dev/null || true
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/comp_err
    rm -f $RPM_BUILD_ROOT%{l_prefix}/man/man1/comp_err.1

    #   install global configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/mysql
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE my.cnf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/mysql/
    %{l_shtool} install -c -m 600 \
        %{SOURCE my.pwd} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/mysql/

    #   install run-command script
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.mysql} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   make sure the database directory exists
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/mysql/db \
        $RPM_BUILD_ROOT%{l_prefix}/var/mysql/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/mysql/run

    #   directory for temporary tables
    %{l_shtool} mkdir -f -p -m 700 \
        $RPM_BUILD_ROOT%{l_prefix}/var/mysql/tmp

    #   optional client-only installation
%if "%{with_server}" != "yes"
    ( cd $RPM_BUILD_ROOT%{l_prefix}/bin
      for bin in *; do
          case "$bin" in
              mysql | mysql_config ) ;;
              * ) rm -f $bin ;;
          esac
      done
    ) || exit $?
    ( cd $RPM_BUILD_ROOT%{l_prefix}/man/man1
      for man in *; do
          case "$man" in
              mysql.1 | mysql_config.1 ) ;;
              * ) rm -f $man ;;
          esac
      done
    ) || exit $?
%endif

    #   determine the package files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
%if "%{with_server}" == "yes"
        %{l_files_std} \
        '%config %attr(644,%{l_musr},%{l_mgrp}) %{l_prefix}/etc/mysql/my.cnf' \
        '%config %attr(600,%{l_susr},%{l_mgrp}) %{l_prefix}/etc/mysql/my.pwd' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/mysql' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/mysql/*'
%else
        %{l_files_std}
%endif

%files -f files

%clean

%pre
%if "%{with_server}" == "yes"
    if [ $1 -eq 2 ]; then
        #   sorry, on major uprade from MySQL 5.1, the user has to react manually!
        if [ ! -d $RPM_INSTALL_PREFIX/var/mysql/db ]; then
            ( echo "ATTENTION! You are attempting to perform a major upgrade"
              echo "from MySQL 5.1 to MySQL %{V_major}. Sorry, this cannot be done"
              echo "without your manual intervention as the administrator!"
              echo ""
              echo "1. Use old MySQL to perform a full dump of all your databases:"
              echo "  \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mysql start"
              echo "  \$ $RPM_INSTALL_PREFIX/bin/mysqldump \\%{l_nil}"
              echo "    --user=root --password --all-databases >/path/to/dump"
              echo ""
              echo "2. Remove the old MySQL (required, as it stores databases differently):"
              echo "  \$ $RPM_INSTALL_PREFIX/bin/openpkg rpm -e mysql --nodeps"
              echo "  \$ rm -rf $RPM_INSTALL_PREFIX/var/mysql"
              echo ""
              echo "3. Install the new MySQL from scratch:"
              echo "  \$ $RPM_INSTALL_PREFIX/bin/openpkg build mysql | sh"
              echo ""
              echo "4. Start new MySQL and restore all databases:"
              echo "  \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mysql start"
              echo "  \$ $RPM_INSTALL_PREFIX/bin/mysql \\%{l_nil}"
              echo "    --user=root --password </path/to/dump"
              echo "  \$ $RPM_INSTALL_PREFIX/bin/mysql_upgrade \\%{l_nil}"
              echo "    --user=root --password"
            ) | %{l_rpmtool} msg -b -t error
            exit 1
        fi

        #   before upgrade, save status and stop service
        eval `%{l_rc} mysql status 2>/dev/null | tee %{l_tmpfile}`
        %{l_rc} mysql stop 2>/dev/null
    fi
%endif
    exit 0

%post
%if "%{with_server}" == "yes"
    if [ $1 -eq 1 ]; then
        #   after install, create initial database
        $RPM_INSTALL_PREFIX/bin/mysqld \
            --defaults-file=$RPM_INSTALL_PREFIX/etc/mysql/my.cnf \
            --datadir=$RPM_INSTALL_PREFIX/var/mysql/db \
            --initialize-insecure \
            --user=%{l_rusr} \
            >/dev/null 2>&1
        chown -R %{l_rusr}:%{l_rgrp} $RPM_INSTALL_PREFIX/var/mysql/db
        ( echo "An initial MySQL DB was created. The owner of the database"
          echo "is the DB user 'root'. Its initial password is empty."
          echo "After starting MySQL with..."
          echo "  \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mysql start"
          echo ""
          echo "...you should change the password as soon as possible with:"
          echo "  \$ $RPM_INSTALL_PREFIX/bin/mysqladmin \\%{l_nil}"
          echo "    --user=root password <root-password>"
          echo ""
          echo "Additionally, because the MySQL package includes automated"
          echo "maintenance procedures that require administrator access to"
          echo "the database, you must maintain a (plain text) copy of the"
          echo "administrator account name and password:"
          echo "  \$ vi $RPM_INSTALL_PREFIX/etc/mysql/my.pwd"
          echo ""
          echo "Now you can connect to MySQL as the administrator with:"
          echo "  \$ $RPM_INSTALL_PREFIX/bin/mysql \\%{l_nil}"
          echo "    --user=root --password=<root-password>"
          echo ""
          echo "Then you usually create a database <database> for a user <user>"
          echo "and with password <password>:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/mysqladmin \\%{l_nil}"
          echo "      --user=root --password=<root-password> create <database>"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/mysql \\%{l_nil}"
          echo "      --user=root --password=<root-password> mysql"
          echo "    mysql> GRANT ALL PRIVILEGES ON <database>.*"
          echo "           TO <username>@localhost IDENTIFIED BY '<password>';"
          echo "    mysql> FLUSH PRIVILEGES;"
          echo "    mysql> \\q"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restore status
        { eval `cat %{l_tmpfile}`; rm -f %{l_tmpfile}; true; } >/dev/null 2>&1
        [ ".$mysql_active" = .yes ] && %{l_rc} mysql start
    fi
%endif
    exit 0

%preun
%if "%{with_server}" == "yes"
    if [ $1 -eq 0 ]; then
        #   before erase, stop service and remove log files
        %{l_rc} mysql stop 2>/dev/null
        rm -f $RPM_INSTALL_PREFIX/var/mysql/log/* >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/mysql/run/* >/dev/null 2>&1 || true
    fi
%endif
    exit 0

