##
##  hazelcast.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_hazelcast  4.0
%define       V_management 4.0

#   package information
Name:         hazelcast
Summary:      Data Distribution Platform
URL:          http://hazelcast.org/
Vendor:       Hazelcast
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      Apache
Version:      %{V_hazelcast}
Release:      20200205

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/hazelcast/hazelcast-%{V_hazelcast}.tar.gz
Source1:      http://download.hazelcast.com/management-center/hazelcast-management-center-%{V_management}.tar.gz
Source2:      hazelcast.xml
Source3:      rc.hazelcast

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101, java, JAVA-JDK

%description
    Hazelcast is a clustering and highly scalable data distribution
    platform. With its various distributed data structures, distributed
    caching capabilities, elastic nature, memcache support, integration
    with Spring and Hibernate and more importantly with so many
    happy users, Hazelcast is feature-rich, enterprise-ready and
    developer-friendly in-memory data grid solution.

%track
    prog hazelcast = {
        version   = %{V_hazelcast}
        url       = http://hazelcast.org/download/
        regex     = hazelcast-(__VER__)\.jar
    }

%prep
    %setup -q
    %setup -q -T -D -a 1

%build

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/hazelcast \
        $RPM_BUILD_ROOT%{l_prefix}/lib/hazelcast \
        $RPM_BUILD_ROOT%{l_prefix}/var/hazelcast/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/hazelcast/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/hazelcast/db

    #   install default configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE hazelcast.xml} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/hazelcast/

    #   install mancenter startup script
    (    echo "#!/bin/sh"
         echo "eval \`JAVA_PLATFORM=\"sun-jdk\" %{l_prefix}/bin/java-toolkit -e\`"
         echo "%{l_prefix}/bin/java \\%{l_nil}"
         echo "    -jar %{l_prefix}/lib/hazelcast/hazelcast-mancenter.war \\%{l_nil}"
         echo "    \${1+\"\$@\"}"
    ) >hazelcast-mancenter.sh
    %{l_shtool} install -c -m 755 \
        hazelcast-mancenter.sh \
        $RPM_BUILD_ROOT%{l_prefix}/bin/hazelcast-mancenter

    #   install program components
    %{l_shtool} install -c -m 644 \
        lib/hazelcast-all-*.jar \
        $RPM_BUILD_ROOT%{l_prefix}/lib/hazelcast/hazelcast.jar
    %{l_shtool} install -c -m 644 \
        hazelcast-management-center-*/*.war \
        $RPM_BUILD_ROOT%{l_prefix}/lib/hazelcast/hazelcast-mancenter.war

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

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

%files -f files

%clean

%post
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} hazelcast status 2>/dev/null`
        [ ".$hazelcast_active" = .yes ] && %{l_rc} hazelcast restart
    fi
    exit 0

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

