##
##  smokeping.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:         smokeping
Summary:      Latency Logging and Graphing System
URL:          http://oss.oetiker.ch/smokeping/
Vendor:       Tobias Oetiker
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Mapping
License:      GPL
Version:      2.4.2
Release:      20080725

#   list of sources
Source0:      http://oss.oetiker.ch/smokeping/pub/smokeping-%{version}.tar.gz
Source1:      smokeping.conf
Source2:      smokeping.html
Source3:      rc.smokeping

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       perl, perl-www, rrdtool, rrdtool::with_perl = yes, fping, MTA

%description
    SmokePing is a latency logging and graphing system. It consists of
    a daemon process which organizes the latency measurements and a CGI
    which presents the graphs.

%track
    prog smokeping = {
        version   = %{version}
        url       = http://oss.oetiker.ch/smokeping/pub/
        regex     = smokeping-(__VER__)\.tar\.gz
    }

%prep
    %setup -q

    #   clean-up source tree
    find . -type f -name '.#*' -exec rm -f {} \;

    #   fix paths
    %{l_shtool} subst \
        -e 's;^#!/.*$;#!%{l_prefix}/bin/perl;' \
        -e 's;^\(use lib qw(\)[^)]*rrdtool[^)]*\()\;.*\)$;\1%{l_prefix}/lib/smokeping\2;' \
        -e 's;^\(Smokeping::main("\).*\(")\;\)$;\1%{l_prefix}/etc/smokeping/smokeping.conf\2;' \
        -e 's;^\(Smokeping::cgi("\).*\(")\;\)$;\1%{l_prefix}/etc/smokeping/smokeping.conf\2;' \
        bin/smokeping.dist \
        htdocs/smokeping.cgi.dist

%build
    :

%install

    #   create directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/cgi \
        $RPM_BUILD_ROOT%{l_prefix}/etc/smokeping \
        $RPM_BUILD_ROOT%{l_prefix}/lib/smokeping \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/share/smokeping \
        $RPM_BUILD_ROOT%{l_prefix}/var/smokeping/dat \
        $RPM_BUILD_ROOT%{l_prefix}/var/smokeping/img

    #   install SmokePing
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        bin/smokeping.dist \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/smokeping
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        htdocs/smokeping.cgi.dist \
        $RPM_BUILD_ROOT%{l_prefix}/cgi/smokeping
    ( cd lib
      umask 022
      %{l_tar} cf - . | %{l_tar} xf - -C $RPM_BUILD_ROOT%{l_prefix}/lib/smokeping/
    ) || exit $?

    #   install default configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE smokeping.conf} \
        %{SOURCE smokeping.html} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/smokeping/
    %{l_shtool} mkln -s \
        ../../etc/smokeping/smokeping.html \
        $RPM_BUILD_ROOT%{l_prefix}/share/smokeping/
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        etc/smokemail.dist \
        $RPM_BUILD_ROOT%{l_prefix}/etc/smokeping/smokemail

    #   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.smokeping} \
        $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/smokeping/*' \
        '%dir %attr(1777,%{l_mgrp},%{l_mgrp}) %{l_prefix}/var/smokeping/img'

%files -f files

%clean

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

%preun
    #   before erase, stop service and remove archvies and image caches
    [ $1 -eq 0 ] || exit 0
    %{l_rc} smokeping stop 2>/dev/null
    rm -rf $RPM_INSTALL_PREFIX/var/smokeping/dat/* >/dev/null 2>&1 || true
    rm -rf $RPM_INSTALL_PREFIX/var/smokeping/img/* >/dev/null 2>&1 || true
    exit 0

