##
##  mariadb.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   12.0
%define       V_minor   2
%define       V_mariadb %{V_major}.%{V_minor}
%define       V_opkg    %{V_major}.%{V_minor}

#   package information
Name:         mariadb
Summary:      Fast Relational Database Management System
URL:          http://mariadb.org/
Vendor:       MariaDB Foundation
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      GPL
Version:      %{V_opkg}
Release:      20250824

#   package options
%option       with_server      yes
%option       with_archive     no
%option       with_blackhole   no
%option       with_connect     no
%option       with_csv         no
%option       with_federated   no
%option       with_federatedx  no
%option       with_heap        yes
%option       with_innobase    yes
%option       with_maria       yes
%option       with_mroonga     no
%option       with_myisam      yes
%option       with_oqgraph     no
%option       with_partition   no
%option       with_perfschema  yes
%option       with_rocksdb     no
%option       with_sequence    no
%option       with_sphinx      no
%option       with_spider      no
%option       with_tokudb      no
%option       with_xtradb      yes
%option       with_galera      no

#   list of sources
Source0:      https://downloads.mariadb.org/interstitial/mariadb-%{V_mariadb}/source/mariadb-%{V_mariadb}.tar.gz
Source1:      my.cnf
Source2:      my.pwd
Source3:      rc.mariadb
Patch0:       mariadb.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20140101, perl, make, gcc, gcc::with_cxx = yes, cmake, pkgconfig
PreReq:       OpenPKG, openpkg >= 20140101, perl
BuildPreReq:  zlib, readline, ncurses, libevent, openssl, pcre, libexecinfo, thrift, judy
PreReq:       zlib, readline, ncurses, libevent, openssl, pcre, libexecinfo, thrift, judy
%if "%{with_galera}" == "yes"
PreReq:       galera
%endif
Provides:     mysql = %{version}

%description
    MariaDB 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 MariaDB
    are speed, robustness and ease of use. MariaDB was derived from
    MySQL 5.1, which 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. MariaDB
    contains backported fixed from MySQL 5.5/5.6/5.7 plus many
    enhancements of its own.

%track
    prog mariadb = {
        version   = %{V_mariadb}
        url       = https://downloads.mariadb.org/mariadb/+releases/
        regex     = mariadb/(%{V_major}\.\d+)/
    }

%prep
    %setup -q
    %patch -p0

%build
    #   fix shebang on supplemental Perl scripts
    rm -f scripts/*.orig
    %{l_shtool} subst \
        -e 's;^#!/usr/bin/perl;#!%{l_prefix}/bin/perl;' \
        scripts/*

    #   configure source tree
    mkdir build
    cd build
    cmake \
        -Wno-dev \
        -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
        -DCMAKE_BUILD_TYPE="Release" \
        -DCMAKE_INSTALL_PREFIX="%{l_prefix}" \
        -DMYSQL_DATADIR="%{l_prefix}/var/mariadb/db" \
        -DINSTALL_SYSCONFDIR="%{l_prefix}/etc/mariadb" \
        -DINSTALL_MYSQLDATADIR="%{l_prefix}/var/mariadb/db" \
        -DINSTALL_MYSQLSHAREDIR="%{l_prefix}/share/mariadb" \
        -DINSTALL_SHAREDIR="%{l_prefix}/share/mariadb" \
        -DINSTALL_MYSQLTESTDIR="%{l_prefix}/share/mariadb/mysql-test" \
        -DINSTALL_SUPPORTFILESDIR="%{l_prefix}/share/mariadb/support-files" \
        -DINSTALL_SCRIPTDIR="%{l_prefix}/bin" \
        -DINSTALL_PLUGINDIR="%{l_prefix}/libexec/mariadb/plugin" \
        -DINSTALL_UNIX_ADDRDIR="%{l_prefix}/var/mariadb/run/mysqld.sock" \
        -DCMAKE_C_COMPILER="%{l_cc}" \
        -DCMAKE_C_FLAGS="%{l_cflags} -I`pwd`/include -I`pwd`/../include %{l_cppflags ncurses .}" \
        -DCMAKE_CXX_COMPILER="%{l_cxx}" \
        -DCMAKE_CXX_FLAGS="%{l_cxxflags} -I`pwd`/include -I`pwd`/../include %{l_cppflags ncurses .}" \
        -DCMAKE_EXE_LINKER_FLAGS="%{l_ldflags}" \
        -DDISABLE_SHARED=ON \
        -DWITH_UNIT_TESTS=OFF \
        -DENABLED_PROFILING=OFF \
        -DSECURITY_HARDENED=OFF \
        -DCRYPTO_LIBRARY="%{l_prefix}/lib/libcrypto.a" \
        -DOPENSSL_INCLUDE_DIR="%{l_prefix}/include" \
        -DOPENSSL_LIBRARIES="%{l_prefix}/lib/libssl.a" \
        -DOPENSSL_ROOT_DIR="%{l_prefix}" \
        -DEVENT_LIBRARY="%{l_prefix}/lib/libevent.a" \
        -DEXECINFO="%{l_prefix}/lib/libexecinfo.a" \
        -DWITH_PCRE="system" \
        -DWITH_ZLIB="system" \
%if "%{with_archive}" == "yes"
        -DWITH_ARCHIVE_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_ARCHIVE_STORAGE_ENGINE=ON \
%endif
%if "%{with_blackhole}" == "yes"
        -DWITH_BLACKHOLE_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_BLACKHOLE_STORAGE_ENGINE=ON \
%endif
%if "%{with_connect}" == "yes"
        -DWITH_CONNECT_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_CONNECT_STORAGE_ENGINE=ON \
%endif
%if "%{with_csv}" == "yes"
        -DWITH_CSV_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_CSV_STORAGE_ENGINE=ON \
%endif
%if "%{with_federated}" == "yes"
        -DWITH_FEDERATED_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_FEDERATED_STORAGE_ENGINE=ON \
%endif
%if "%{with_federatedx}" == "yes"
        -DWITH_FEDERATEDX_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_FEDERATEDX_STORAGE_ENGINE=ON \
%endif
%if "%{with_heap}" == "yes"
        -DWITH_HEAP_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_HEAP_STORAGE_ENGINE=ON \
%endif
%if "%{with_innobase}" == "yes"
        -DWITH_INNOBASE_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_INNOBASE_STORAGE_ENGINE=ON \
%endif
%if "%{with_maria}" == "yes"
        -DWITH_MARIA_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_MARIA_STORAGE_ENGINE=ON \
%endif
%if "%{with_myisam}" == "yes"
        -DWITH_MYISAM_STORAGE_ENGINE=ON \
        -DWITH_MYISAMMRG_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_MYISAM_STORAGE_ENGINE=ON \
        -DWITHOUT_MYISAMMRG_STORAGE_ENGINE=ON \
%endif
%if "%{with_mroonga}" == "yes"
        -DWITH_MROONGA_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_MROONGA_STORAGE_ENGINE=ON \
%endif
%if "%{with_oqgraph}" == "yes"
        -DWITH_OQGRAPH_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_OQGRAPH_STORAGE_ENGINE=ON \
%endif
%if "%{with_partition}" == "yes"
        -DWITH_PARTITION_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_PARTITION_STORAGE_ENGINE=ON \
%endif
%if "%{with_perfschema}" == "yes"
        -DWITH_PERFSCHEMA_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_PERFSCHEMA_STORAGE_ENGINE=ON \
%endif
%if "%{with_rocksdb}" == "yes"
        -DWITH_ROCKSDB_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_ROCKSDB_STORAGE_ENGINE=ON \
%endif
%if "%{with_sequence}" == "yes"
        -DWITH_SEQUENCE_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_SEQUENCE_STORAGE_ENGINE=ON \
%endif
%if "%{with_sphinx}" == "yes"
        -DWITH_SPHINX_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_SPHINX_STORAGE_ENGINE=ON \
%endif
%if "%{with_spider}" == "yes"
        -DWITH_SPIDER_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_SPIDER_STORAGE_ENGINE=ON \
%endif
%if "%{with_tokudb}" == "yes"
        -DWITH_TOKUDB_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_TOKUDB_STORAGE_ENGINE=ON \
%endif
%if "%{with_xtradb}" == "yes"
        -DWITH_XTRADB_STORAGE_ENGINE=ON \
%else
        -DWITHOUT_XTRADB_STORAGE_ENGINE=ON \
%endif
%if "%{with_galera}" == "yes"
        -DWITH_WSREP=ON \
        -DWITH_INNODB_DISALLOW_WRITES=ON \
%else
        -DWITH_WSREP=OFF \
%endif
%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 $?

    #   post-adjust installation
%if "%{with_galera}" == "yes"
    chmod 755 $RPM_BUILD_ROOT%{l_prefix}/bin/wsrep_sst_common
%else
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/bin/wsrep_sst_rsync_wan
    rm -f $RPM_BUILD_ROOT%{l_prefix}/man/man1/wsrep_sst_*
%endif

    #   rename some utilities with too generic names
    for prog in mbstream innochecksum replace resolveip resolve_stack_dump perror; do
        mv  $RPM_BUILD_ROOT%{l_prefix}/bin/$prog \
            $RPM_BUILD_ROOT%{l_prefix}/bin/mysql_$prog
        if [ -f $RPM_BUILD_ROOT%{l_prefix}/man/man1/$prog.1 ]; then
            mv  $RPM_BUILD_ROOT%{l_prefix}/man/man1/$prog.1 \
                $RPM_BUILD_ROOT%{l_prefix}/man/man1/mysql_$prog.1
        fi
    done

    #   strip installation area
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* 2>/dev/null || true
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/rcmysql
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/etc/mariadb/init.d
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/etc/mariadb/logrotate.d
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/mariadb/mysql-test
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/mariadb/support-files

    #   install global configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/mariadb
%if "%{with_galera}" == "yes"
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        -e 's/^@with_galera@://' \
        %{SOURCE my.cnf} $RPM_BUILD_ROOT%{l_prefix}/etc/mariadb/
%else
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        -e '/@with_galera@:/d' \
        %{SOURCE my.cnf} $RPM_BUILD_ROOT%{l_prefix}/etc/mariadb/
%endif
    %{l_shtool} install -c -m 600 \
        %{SOURCE my.pwd} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/mariadb/

    #   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.mariadb} \
        $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/mariadb/db \
        $RPM_BUILD_ROOT%{l_prefix}/var/mariadb/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/mariadb/run

    #   directory for temporary tables
    %{l_shtool} mkdir -f -p -m 700 \
        $RPM_BUILD_ROOT%{l_prefix}/var/mariadb/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/mariadb/my.cnf' \
        '%config %attr(600,%{l_susr},%{l_mgrp}) %{l_prefix}/etc/mariadb/my.pwd' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/mariadb' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/mariadb/*'
%else
        %{l_files_std}
%endif

%files -f files

%clean

%pre
%if "%{with_server}" == "yes"
    if [ $1 -eq 2 ]; then
        #   before upgrade, save status and stop service
        eval `%{l_rc} mariadb status 2>/dev/null | tee %{l_tmpfile}`
        %{l_rc} mariadb 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/mysql_install_db \
            --basedir=$RPM_INSTALL_PREFIX \
            --defaults-file=$RPM_INSTALL_PREFIX/etc/mariadb/my.cnf \
            --datadir=$RPM_INSTALL_PREFIX/var/mariadb/db \
            --user=%{l_rusr} \
            >/dev/null 2>&1
        chown -R %{l_rusr}:%{l_rgrp} $RPM_INSTALL_PREFIX/var/mariadb/db
        ( echo "An initial MariaDB DB was created. The owner of the database"
          echo "is the DB user 'root'. Its initial password is empty."
          echo "After starting MariaDB with..."
          echo "  \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mariadb start"
          echo ""
          echo "...you should change the password as soon as possible with:"
          echo "  \$ $RPM_INSTALL_PREFIX/bin/mysqladmin \\ "
          echo "    --user=root password '<root-password>'"
          echo ""
          echo "Additionally, because the MariaDB 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/mariadb/my.pwd"
          echo ""
          echo "Now you can connect to MariaDB as the administrator with:"
          echo "  \$ $RPM_INSTALL_PREFIX/bin/mysql \\ "
          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
        [ ".$mariadb_active" = .yes ] && %{l_rc} mariadb 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} mariadb stop 2>/dev/null
        rm -f $RPM_INSTALL_PREFIX/var/mariadb/log/* >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/mariadb/run/* >/dev/null 2>&1 || true
    fi
%endif
    exit 0

