##
##  ethereum.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 information
Name:         ethereum
Summary:      Ethereum Blockchain Service
URL:          https://geth.ethereum.org/
Vendor:       Ethereum Community
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        P2P
License:      LGPL
Version:      1.8.27
Release:      20190418

#   package options
%option       with_private   yes
%option       with_bootnode  yes

#   list of sources
Source0:      https://github.com/ethereum/go-ethereum/archive/v%{version}.tar.gz
Source1:      ethereum-genesis.json
Source2:      ethereum-geth.conf
Source3:      rc.ethereum
Patch0:       ethereum.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, go
PreReq:       OpenPKG, openpkg >= 20160101, apg
%if "%{with_private}" == "yes" && "%{with_bootnode}" == "yes"
PreReq:       ethereum-bootnode
%endif

%description
    Ethereum is a decentralized platform that runs smart contracts,
    applications that run exactly as programmed without possibility of
    downtime, censorship, fraud or third party interference. Go Ethereum
    (aka Geth) is one of the three original implementations of the
    Ethereum protocol.

%track
    prog ethereum = {
        version   = %{version}
        url       = https://github.com/ethereum/go-ethereum/releases
        regex     = v(__VER__)\.tar\.gz
    }

%prep
    %setup -q -n go-ethereum-%{version}
    %patch -p0

%build
    #   build program
    %{l_make} %{l_mflags} all

%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/ethereum \
        $RPM_BUILD_ROOT%{l_prefix}/var/ethereum/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/ethereum/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/ethereum/data/ethash

    #   install program
    for prog in build/bin/*; do
        name=`echo $prog | sed -e 's;build/bin/;ethereum-;'`
        %{l_shtool} install -c -s -m 775 \
            $prog $RPM_BUILD_ROOT%{l_prefix}/bin/$name
    done
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/ethereum-bootnode

    #   install default configuration files
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE ethereum-geth.conf} $RPM_BUILD_ROOT%{l_prefix}/etc/ethereum/
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE ethereum-genesis.json} $RPM_BUILD_ROOT%{l_prefix}/etc/ethereum/

    #   install run-command script
    flags=""
%if "%{with_private}" == "yes"
        flags="--mine --etherbase=0x0000000000000000000000000000000000000000"
%endif
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e "s;@flags@;$flags;" \
        %{SOURCE rc.ethereum} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   determine bootnodes
%if "%{with_private}" == "yes"
%if "%{with_bootnode}" != "yes"
        bootnodes=""
%else
        bootnodes=`$RPM_INSTALL_PREFIX/bin/ethereum-bootnode \
            -nodekey $RPM_INSTALL_PREFIX/etc/ethereum-bootnode/ethereum-bootnode.key -writeaddress`
        bootnodes="\"enode://$bootnodes@127.0.0.1:30301\""
%endif
%else
        bootnodes=`$RPM_INSTALL_PREFIX/bin/ethereum-geth dumpconfig | \
            grep '^BootstrapNodes' | sed -e 's;^.*\\[;;' -e 's;\\].*;;'`
%endif
        %{l_shtool} subst -e "s;@bootnodes@;$bootnodes;" \
            $RPM_INSTALL_PREFIX/etc/ethereum/ethereum-geth.conf

%if "%{with_private}" == "yes"
        #   generate local account
        echo "Generating Private Ethereum Blockchain Local Account" | \
            %{l_rpmtool} msg -b -t notice
        su - %{l_rusr} -c \
            "umask 0077; $RPM_INSTALL_PREFIX/bin/apg -n1 -a1 -m32 -x32 -M NCL \
                >$RPM_INSTALL_PREFIX/etc/ethereum/ethereum-account.passwd"
        account=`su - %{l_rusr} -c \
            "$RPM_INSTALL_PREFIX/bin/ethereum-geth \
                --config $RPM_INSTALL_PREFIX/etc/ethereum/ethereum-geth.conf \
                account new \
                --password $RPM_INSTALL_PREFIX/etc/ethereum/ethereum-account.passwd" 2>&1 | \
            egrep "^Address:" | sed -e 's;^Address: {\(.*\)}.*;\1;'`

        #   generate genesis block
        echo "Generating Private Ethereum Blockchain Genesis Block" | \
            %{l_rpmtool} msg -b -t notice
        %{l_shtool} subst -e "s;@account@;$account;" \
            $RPM_INSTALL_PREFIX/etc/ethereum/ethereum-genesis.json
        su - %{l_rusr} -c \
            "$RPM_INSTALL_PREFIX/bin/ethereum-geth \
                --config $RPM_INSTALL_PREFIX/etc/ethereum/ethereum-geth.conf \
                init $RPM_INSTALL_PREFIX/etc/ethereum/ethereum-genesis.json \
                >>$RPM_INSTALL_PREFIX/var/ethereum/log/ethereum-geth.log 2>&1"
%endif
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} ethereum status 2>/dev/null`
        [ ".$ethereum_active" = .yes ] && %{l_rc} ethereum restart
    fi
    exit 0

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

