##
##  ndb.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.
##

#   FIXME: ndbd cannot be stopped with signal of parent process? Only the subprocess works?!

#   package version
%define       V_major   5.1
%define       V_minor   73
%define       V_mysql   %{V_major}.%{V_minor}

#   package information
Name:         ndb
Summary:      Network Database (NDB) of MySQL RDBMS
URL:          http://www.ndb.com/products/ndb/
Vendor:       MySQL AB
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      GPL
Version:      %{V_mysql}
Release:      20140528

#   list of sources
Source0:      http://ftp.gwdg.de/pub/misc/mysql/Downloads/MySQL-%{V_major}/mysql-%{V_mysql}.tar.gz
Source1:      rc.ndb
Source2:      ndbd.ini
Source3:      ndb_mgmd.ini
Patch0:       ndb.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make, gcc, gcc::with_cxx = yes
PreReq:       OpenPKG, openpkg >= 20160101

%description
    Network Database (NDB) is the cluster storage engine of MySQL RDBMS.

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

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

%build
    #   patch file search path
    %{l_shtool} subst %{l_value -s -a} \
        mysys/default.c

    #   configure source tree
    CC="%{l_cc}" \
    CXX="%{l_cxx}" \
    CFLAGS="%{l_cflags -O}" \
    CXXFLAGS="%{l_cxxflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags}" \
    LIBS="" \
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --infodir=%{l_prefix}/info \
        --sysconfdir=%{l_prefix}/etc/ndb \
        --localstatedir=%{l_prefix}/var/ndb \
        --libexecdir=%{l_prefix}/sbin \
        --with-server \
        --with-plugins=partition,ndbcluster \
        --with-charset=utf8 \
        --with-collation=utf8_unicode_ci \
        --without-readline \
        --without-libedit \
        --without-zlib \
        --disable-shared

    #   build minimum MySQL components necessary for NDB
    ( cd include
      %{l_make} %{l_mflags} my_config.h
    ) || exit $?
    ( for subdir in mysys strings dbug; do
          ( cd $subdir && %{l_make} %{l_mflags} ) || exit $?
      done
    ) || exit $?

    #   build NDB components
    ( cd storage/ndb
      %{l_make} %{l_mflags}
    ) || exit $?

%install
    #   install NDB components
    ( cd storage/ndb
      %{l_make} %{l_mflags} install \
          DESTDIR=$RPM_BUILD_ROOT
    ) || exit $?
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man8
    %{l_shtool} install -c -m 644 \
        man/ndb*.1 $RPM_BUILD_ROOT%{l_prefix}/man/man1/
    %{l_shtool} install -c -m 644 \
        man/ndb*.8 $RPM_BUILD_ROOT%{l_prefix}/man/man8/

    #   strip installation area
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/include
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/lib
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/mysql
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/bin/ndb_size.pl
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/bin/ndb_error_reporter

    #   install default configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/ndb
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE ndbd.ini} \
        %{SOURCE ndb_mgmd.ini} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/ndb/

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

    #   determine the package files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/ndb/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/ndb' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/ndb/*'

%files -f files

%clean

%pre
    #   before upgrade, save status and stop service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} ndb status 2>/dev/null | tee %{l_tmpfile}`
    %{l_rc} ndb stop 2>/dev/null
    exit 0

%post
    if [ $1 -eq 1 ]; then
        ( echo "To access NDB configure your MySQL servers with:"
          echo "    [mysqld]"
          echo "    ndbcluster"
          echo "    ndb-nodeid=31"
          echo "    ndb-connectstring=127.0.0.1:1186"
        ) | %{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
        [ ".$ndb_active" = .yes ] && %{l_rc} ndb start
    fi
    exit 0

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} ndb stop 2>/dev/null
    rm -rf $RPM_INSTALL_PREFIX/var/ndb/db/*    >/dev/null 2>&1 || true
    rm -rf $RPM_INSTALL_PREFIX/var/ndb/cache/* >/dev/null 2>&1 || true
    rm -f  $RPM_INSTALL_PREFIX/var/ndb/run/*   >/dev/null 2>&1 || true
    rm -f  $RPM_INSTALL_PREFIX/var/ndb/log/*   >/dev/null 2>&1 || true
    exit 0

