##
##  elasticsearch.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_opkg  7.17.29
%define       V_dist  7.17.29

#   package information
Name:         elasticsearch
Summary:      Enterprise Indexing and Search Server
URL:          http://www.elasticsearch.org/
Vendor:       Shay Banon
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Database
License:      Apache
Version:      %{V_opkg}
Release:      20250624

#   list of sources
Source0:      https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-%{V_dist}-no-jdk-linux-x86_64.tar.gz
Source1:      rc.elasticsearch
Source2:      elasticsearch.yml
Patch0:       elasticsearch.patch

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

%description
    ElasticSearch is a powerful distributed RESTful Indexing and Search
    Server, built in top of the Apache Lucense search engine.

%track
    prog elasticsearch = {
        version   = %{V_dist}
        url       = https://www.elastic.co/downloads/elasticsearch-no-jdk
        regex     = elasticsearch-(\d+\.\d+\.\d+)-no-jdk-linux-x86_64\.tar\.gz
    }

%prep
    %setup -q -n elasticsearch-%{V_dist}
    %patch -p0

%build
    #   patch files
    %{l_shtool} subst \
        -e 's;/bin/bash;%{l_bash};g' \
        bin/elasticsearch \
        bin/elasticsearch-certgen \
        bin/elasticsearch-certutil \
        bin/elasticsearch-cli \
        bin/elasticsearch-croneval \
        bin/elasticsearch-env \
        bin/elasticsearch-keystore \
        bin/elasticsearch-migrate \
        bin/elasticsearch-plugin \
        bin/elasticsearch-saml-metadata \
        bin/elasticsearch-setup-passwords \
        bin/elasticsearch-sql-cli \
        bin/elasticsearch-syskeygen \
        bin/elasticsearch-translog \
        bin/elasticsearch-users \
        bin/x-pack-env \
        bin/x-pack-security-env \
        bin/x-pack-watcher-env \
        bin/x-pack/certgen \
        bin/x-pack/certutil \
        bin/x-pack/croneval \
        bin/x-pack/migrate \
        bin/x-pack/saml-metadata \
        bin/x-pack/setup-passwords \
        bin/x-pack/sql-cli \
        bin/x-pack/syskeygen \
        bin/x-pack/users

    #   strip down distribution
    rm -f [A-Z]*
    find . -name "*.orig" -print | xargs rm -f

%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/elasticsearch \
        $RPM_BUILD_ROOT%{l_prefix}/lib/elasticsearch \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/elasticsearch/plugins \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/elasticsearch/scripts \
        $RPM_BUILD_ROOT%{l_prefix}/var/elasticsearch/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/elasticsearch/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/elasticsearch/tmp \
        $RPM_BUILD_ROOT%{l_prefix}/var/elasticsearch/db

    #   install program components
    rm -f bin/*.bat bin/*.exe
    cp -r * \
        $RPM_BUILD_ROOT%{l_prefix}/lib/elasticsearch/

    #   replace JNA library
    rm -f $RPM_BUILD_ROOT%{l_prefix}/lib/elasticsearch/lib/jna-*.jar
    ln -s ../../jna.jar \
        $RPM_BUILD_ROOT%{l_prefix}/lib/elasticsearch/lib/jna.jar

    #   install link to configuration files
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/lib/elasticsearch/config >/dev/null 2>&1 || true
    ln -s ../../etc/elasticsearch \
        $RPM_BUILD_ROOT%{l_prefix}/lib/elasticsearch/config

    #   install link to plugins directory
    rmdir $RPM_BUILD_ROOT%{l_prefix}/lib/elasticsearch/plugins >/dev/null 2>&1 || true
    ln -s ../../libexec/elasticsearch/plugins \
        $RPM_BUILD_ROOT%{l_prefix}/lib/elasticsearch/plugins

    #   install link to scripts directory
    rmdir $RPM_BUILD_ROOT%{l_prefix}/lib/elasticsearch/scripts >/dev/null 2>&1 || true
    ln -s ../../libexec/elasticsearch/scripts \
        $RPM_BUILD_ROOT%{l_prefix}/lib/elasticsearch/scripts

    #   install link to log directory
    rmdir $RPM_BUILD_ROOT%{l_prefix}/lib/elasticsearch/logs >/dev/null 2>&1 || true
    ln -s ../../var/elasticsearch/log \
        $RPM_BUILD_ROOT%{l_prefix}/lib/elasticsearch/logs

    #   install wrapper commands
    for prog in elasticsearch elasticsearch-plugin elasticsearch-keystore elasticsearch-translog; do
        (   echo "#!/bin/sh"
            echo "eval \`JAVA_PLATFORM=\"sun-jdk\" %{l_prefix}/bin/java-toolkit -e\`"
            echo "export ES_HOME=\"%{l_prefix}/lib/elasticsearch\""
            echo "export ES_PATH_CONF=\"%{l_prefix}/etc/elasticsearch\""
            echo "export ES_TMPDIR=\"%{l_prefix}/var/elasticsearch/tmp\""
            echo "exec %{l_prefix}/lib/elasticsearch/bin/$prog \${1+\"\$@\"}"
        ) >$prog
        %{l_shtool} install -c -m 755 \
            $prog $RPM_BUILD_ROOT%{l_prefix}/bin/$prog
    done

    #   install default configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE elasticsearch.yml} $RPM_BUILD_ROOT%{l_prefix}/etc/elasticsearch/
    %{l_shtool} install -c -m 644 \
        config/jvm.options config/log4j2.properties \
        $RPM_BUILD_ROOT%{l_prefix}/etc/elasticsearch/

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.elasticsearch} $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/elasticsearch/*' \
        '%dir %attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/elasticsearch' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/elasticsearch' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/elasticsearch/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   Create initial ElasticSearch keystore
        echo "Create initial ElasticSearch keystore" | \
            %{l_rpmtool} msg -b -t notice
        su - %{l_rusr} -c \
            "LC_CTYPE=C; export LC_CTYPE; umask 077; \
            $RPM_INSTALL_PREFIX/bin/elasticsearch-keystore create"

        #   display final hints on initial installation
        (   echo "By default, ElasticSearch runs its server process on IPv4 address"
            echo "127.0.0.1, TCP port 9200. Start the ElasticSearch server by running:"
            echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc elasticsearch start"
            echo "Connect to ElasticSearch via REST through the URL:"
            echo "    http://localhost:9200/"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} elasticsearch status 2>/dev/null`
        [ ".$elasticsearch_active" = .yes ] && %{l_rc} elasticsearch restart
    fi
    exit 0

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

