##
##  statsd.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_statsd_base  0.9.0
%define       V_statsd_snap  20200828

#   package information
Name:         statsd
Summary:      Statistic Gathering Daemon
URL:          https://github.com/etsy/statsd/
Vendor:       Etsy
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      MIT
Version:      %{V_statsd_base}
Release:      20200828

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/statsd/statsd-%{V_statsd_base}-%{V_statsd_snap}.tar.gz
Source1:      statsd.conf
Source2:      statsd.sh
Source3:      stats.sh
Source4:      rc.statsd

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, node, node-openpkg, npm
PreReq:       OpenPKG, openpkg >= 20160101, node, socat

%description
    StatsD is a network daemon that runs on the Node.js platform and
    listens for statistics, like counters and timers, sent over UDP or
    TCP and sends aggregates to one or more pluggable backend services.

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

%prep
    %setup -q -c

%build
    #   strip down program
    %{l_prefix}/bin/node-openpkg squeeze
    rm -rf node_modules/statsd/node_modules/winser
    rm -f  node_modules/statsd/node_modules/.bin/winser

%install
    #   install program
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/lib/statsd
    %{l_prefix}/bin/node-openpkg install \
        %{l_prefix}/lib/statsd/

    #   install configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/statsd
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE statsd.conf} $RPM_BUILD_ROOT%{l_prefix}/etc/statsd/

    #   install server wrapper
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE statsd.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/statsd

    #   install minimum client
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE stats.sh} $RPM_BUILD_ROOT%{l_prefix}/bin/stats

    #   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.statsd} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   create run-time directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/statsd/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/statsd/run

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

%files -f files

%clean

%post
    #   after upgrade, restart service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} statsd status 2>/dev/null`
    [ ".$statsd_active" = .yes ] && %{l_rc} statsd restart
    exit 0

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

