##
##  tendermint.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  0.37.0
%define       V_dist  20230618

#   package information
Name:         tendermint
Summary:      Tendermint Blockchain Platform
URL:          https://tendermint.com/
Vendor:       Tendermint Project
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        P2P
License:      Apache
Version:      %{V_opkg}.%{V_dist}
Release:      20230618

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/tendermint/tendermint-%{V_dist}.tar.xz
Source1:      rc.tendermint
Source2:      tendermint.toml
Source3:      tendermint-init.sh

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, go
PreReq:       OpenPKG, openpkg >= 20160101

%description
    Tendermint Core is a Byzantine Fault Tolerant (BFT) and
    Proof-of-Stake (PoS) based middleware that takes a state transition
    machine (the application) and securely replicates it on many
    machines (the blockchain P2P network). It provides just the lower
    networking protocol and the middle consensus protocol and supports
    arbitrary application protocols on top of it. The application
    interacts with Tendermint Core through the Application Block-Chain
    Interface (ABCI).

%track
    prog tendermint:release = {
        version   = %{V_opkg}
        url       = https://github.com/tendermint/tendermint/releases
        regex     = tree/v(\d+\.\d+\.\d+)
    }
    prog tendermint:snapshot = {
        version   = %{V_dist}
        url       = http://download.openpkg.org/components/versioned/tendermint/
        regex     = tendermint-(__VER__)\.tar\.xz
    }

%prep
    %setup -q -n tendermint

%build
    #   build programs
    export GOPATH=`pwd`
    cd src/github.com/tendermint/tendermint
    go build -v -o tendermint \
        cmd/tendermint/*.go
    go build -v -o abci-cli \
        abci/cmd/abci-cli/*.go

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

    #   install programs
    %{l_shtool} install -c -s -m 755 \
        src/github.com/tendermint/tendermint/tendermint \
        src/github.com/tendermint/tendermint/abci-cli \
        $RPM_BUILD_ROOT%{l_prefix}/bin/

    #   install helper script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE tendermint-init.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/bin/tendermint-init

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

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

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   initialize node
        echo "Initializing Tendermint node" | \
            %{l_rpmtool} msg -b -t notice
        su - %{l_rusr} -c \
            "$RPM_INSTALL_PREFIX/bin/tendermint \
                init --home $RPM_INSTALL_PREFIX/var/tendermint/db \
                >>$RPM_INSTALL_PREFIX/var/tendermint/log/tendermint.log 2>&1; \
            mv  $RPM_INSTALL_PREFIX/var/tendermint/db/config/*.json \
                $RPM_INSTALL_PREFIX/etc/tendermint/; \
            rm -rf $RPM_INSTALL_PREFIX/var/tendermint/db/config; \
            ln -s ../../../etc/tendermint \
                $RPM_INSTALL_PREFIX/var/tendermint/db/config; \
            $RPM_INSTALL_PREFIX/bin/tendermint-init example example-node1"

        #   display some initial hints, too
        ( echo "An initial single-node Tendermint configuration was setup for you."
          echo "You can re-generate the validator credentials and the genesis block with:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/tendermint-init \\%{l_nil}"
          echo "      <chain-id> <local-validator-id> [<remote-validator-id> ...]"
          echo "The <chain-id> is the identifier of the blockchain, while the"
          echo "<local-validator-id> (and <remote-validator-id>) are identifiers"
          echo "of the involved networking nodes, acting as validators."
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} tendermint status 2>/dev/null`
        [ ".$tendermint_active" = .yes ] && %{l_rc} tendermint restart
    fi
    exit 0

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

