##
##  bs.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:         bs
Summary:      Big Sister
URL:          http://bigsister.graeff.com/
Vendor:       Thomas Aeby
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Monitoring
License:      GNU
Version:      1.12
Release:      20170717

#   list of sources
Source0:      http://download.sourceforge.net/bigsister/big-sister-%{version}.tar.gz
Source1:      rc.bs
Source2:      bsapache.conf
Source3:      bsapachectl

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

%description
    Big Sister is a clone of Sean MacGuire's Big Brother. Its primary
    functions are: monitor networked systems; provide a simple view
    of the current network status; generate alarms on status changes;
    generate a history of status changes; interoperate with other Big
    Sister instances or foreign network monitors.

%track
    prog bs = {
        version   = %{version}
        url       = http://sourceforge.net/projects/bigsister/files/
        regex     = big-sister-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -n big-sister-%{version}

%build

%install

    #   patch sources
    %{l_shtool} subst \
        -e 's;chmod;true;' \
        -e 's;chown;true;' \
        install.sh
    %{l_shtool} subst \
        -e 's;common::root/adm;common::root/var;' \
        common.pm

    #   create installation hierarchie
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/share/bs/cgi \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/var/bs \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/bs/www/skins

    #   install skins manually
    (cd skins && %{l_tar} cf - *) |\
        (cd $RPM_BUILD_ROOT%{l_prefix}/libexec/bs/www/skins && %{l_tar} xf -) || exit $?
    find $RPM_BUILD_ROOT%{l_prefix}/libexec/bs/www/skins -type d -exec chmod 755 {} \;
    find $RPM_BUILD_ROOT%{l_prefix}/libexec/bs/www/skins -type f -exec chmod 644 {} \;

    #   perform standard installation procedure
    ./configure \
        --prefix=%{l_prefix}/libexec/bs
    %{l_make} %{l_mflags} install \
        DESTDIR=$RPM_BUILD_ROOT \
        PERL=%{l_prefix}/bin/perl \
        CGIPATH=/bs-cgi \
        WEBROOT=%{l_prefix}/share/bs \
        EXEC=%{l_prefix}/libexec/bs \
        USER=%{l_rusr}

    #   move var to var
    rmdir $RPM_BUILD_ROOT%{l_prefix}/libexec/bs/var || true
    ln -s %{l_prefix}/var/bs $RPM_BUILD_ROOT%{l_prefix}/libexec/bs/var

    #   we are not on win32, so remove eventlog
    rm -f $RPM_BUILD_ROOT%{l_prefix}/libexec/bs/uxmon/Config/eventlog
    rm -f $RPM_BUILD_ROOT%{l_prefix}/libexec/bs/uxmon/Config/logfile

    #   install rc file
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e 's;@l_bsdir@;%{l_prefix}/libexec/bs;g' \
         %{SOURCE rc.bs} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   install apachectl wrapper
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/bs/tools
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE bsapachectl} \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/bs/tools/

    #   install apache config
    l_hostname=`%{l_shtool} echo -e %h`
    l_domainname=`%{l_shtool} echo -e %d | cut -c2-`
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e 's;@l_hostname@;${l_hostname};g' \
        -e 's;@l_domainame@;${l_domainname};g' \
        %{SOURCE bsapache.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/bs/etc/

    #   install apache data
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/bs/run
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/bs/log

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

%files -f files

%clean

%preun
    if [ $1 -eq 0 ]; then
        $RPM_INSTALL_PREFIX/bin/openpkg rc bs stop
        rm -f $RPM_INSTALL_PREFIX/var/bs/log/*
        rm -f $RPM_INSTALL_PREFIX/var/bs/run/*
    fi

