##
##  cacti.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:         cacti
Summary:      Network Monitoring and Graphing Frontend
URL:          http://www.cacti.net/
Vendor:       raXnet
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Monitoring
License:      GPL
Version:      1.2.30
Release:      20250324

#   list of sources
Source0:      http://www.cacti.net/downloads/cacti-%{version}.tar.gz
Source1:      cacti-setup.sh
Source2:      cacti-cron.sh
Source3:      cacti-apache.conf
Source4:      rc.cacti

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       rrdtool, mysql, snmp, perl, php, dcron
PreReq:       apache
PreReq:       apache-php
PreReq:       apache-php::with_mysql = yes
PreReq:       apache-php::with_mm = yes
PreReq:       apache-php::with_xml = yes
PreReq:       php
PreReq:       php::with_mysql = yes
PreReq:       php::with_mm = yes
PreReq:       php::with_xml = yes

%description
    Cacti is a complete PHP-driven frontend to RRDTool. It stores all
    of the necessary information to create graphs and populate them
    with data in a MySQL database. Along with being able to maintain
    Graphs, Data Sources, and Round Robin Archives in a database,
    Cacti handles the data gathering as well. There is also SNMP
    support for those used to creating traffic graphs with MRTG.

%track
    prog cacti = {
        version   = %{version}
        url       = http://www.cacti.net/downloads/
        regex     = cacti-(__VER__)\.tar\.gz
    }

%prep
    %setup -q

%build

%install

    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/dcron/crontabs \
        $RPM_BUILD_ROOT%{l_prefix}/etc/cacti \
        $RPM_BUILD_ROOT%{l_prefix}/share/cacti \
        $RPM_BUILD_ROOT%{l_prefix}/var/cacti/run

    #   adjust configuration
    %{l_shtool} subst \
        -e 's;"cactiuser";"cacti";g' \
        include/config.php
    %{l_shtool} subst \
        -e 's;perl <path_cacti>;%{l_prefix}/bin/perl %{l_prefix}/share/cacti;' \
        -e 's;\(path_php_binary.*\)<DEFAULT>;\1%{l_prefix}/bin/php;' \
        -e 's;\(path_snmpwalk.*\)<DEFAULT>;\1%{l_prefix}/bin/snmpwalk;' \
        -e 's;\(path_snmpget.*\)<DEFAULT>;\1%{l_prefix}/bin/snmpget;' \
        -e 's;\(path_rrdtool.*\)<DEFAULT>;\1%{l_prefix}/bin/rrdtool;' \
        -e 's;\(path_webroot.*\)<DEFAULT>;\1%{l_prefix}/share/cacti;' \
        -e 's;\(path_webcacti.*\)<DEFAULT>;\1/cacti/;' \
        -e 's;\(smnp_version.*\)ucd-snmp;\1net-snmp;' \
        cacti.sql
    %{l_shtool} subst \
        -e 's;docs/;../docs/;g' \
        -e 's;"/bin";"%{l_prefix}/bin", "%{l_prefix}/sbin", "/bin";g' \
        install/index.php
    %{l_shtool} subst \
        -e 's;#!/usr/bin/php;#!%{l_prefix}/bin/php;' \
        cmd.php

    #   install all files and strip down installation afterwards
    cp -rp * $RPM_BUILD_ROOT%{l_prefix}/share/cacti/
    ( cd $RPM_BUILD_ROOT%{l_prefix}/share/cacti
      rm -f LICENSE README
      rm -rf cactid
      rm -f rra/.placeholder
      rm -f log/rrd.log
    ) || exit $?

    #   move variable data into separate locations
    mv $RPM_BUILD_ROOT%{l_prefix}/share/cacti/log \
       $RPM_BUILD_ROOT%{l_prefix}/var/cacti/log
    ln -s \
       %{l_prefix}/var/cacti/log \
       $RPM_BUILD_ROOT%{l_prefix}/share/cacti/log
    mv $RPM_BUILD_ROOT%{l_prefix}/share/cacti/rra \
       $RPM_BUILD_ROOT%{l_prefix}/var/cacti/rra
    ln -s \
       %{l_prefix}/var/cacti/rra \
       $RPM_BUILD_ROOT%{l_prefix}/share/cacti/rra

    #   internally Cacti appends the webroot to its directory
    #   but we don't want to do this, so use a self-ref link.
    ( cd $RPM_BUILD_ROOT%{l_prefix}/share/cacti
      ln -s . cacti
    ) || exit $?

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

    #   install MySQL database setup script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE cacti-setup.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/cacti-setup

    #   install Cron job script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE cacti-cron.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/cacti-cron

    #   install custom Apache configuration
    l_hostname=`%{l_shtool} echo -e %h`
    l_domainname=`%{l_shtool} echo -e %d | cut -c2-`
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        -e "s;@l_hostname@;$l_hostname;g" \
        -e "s;@l_domainname@;$l_domainname;g" \
        %{SOURCE cacti-apache.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/cacti/

    #   install custom crontab
    ( echo "##"
      echo "##  Cacti crontab"
      echo "##"
      echo ""
      echo "*/5 * * * * su %{l_rusr} -c %{l_prefix}/sbin/cacti-cron"
      echo ""
    ) >$RPM_BUILD_ROOT%{l_prefix}/etc/dcron/crontabs/cacti

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "1. To complete this installation of Cacti please start MySQL and"
          echo "   initialize the Cacti database like this:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mysql start"
          echo "     \$ $RPM_INSTALL_PREFIX/sbin/cacti-setup"
          echo ""
          echo "2. By default, Cacti is run by the OpenPKG cron jobs only every"
          echo "   5 minutes. To run Cacti every 1 minute for even more granular"
          echo "   data gathering, edit the following crontab(5) file:"
          echo "     \$ vi $RPM_INSTALL_PREFIX/etc/dcron/crontabs/cacti"
          echo ""
          echo "3. By default, Cacti runs its own Apache server on IPv4 address"
          echo "   127.0.0.1, TCP port 8080. Please change this by editing the"
          echo "   \"Listen 127.0.0.1:8080\" directive in the configuration file"
          echo "   $RPM_INSTALL_PREFIX/etc/cacti/cacti-apache.conf"
          echo ""
          echo "4. After this postinstallation, start Cacti by running"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc cacti start"
          echo "   and connect with a browser to:"
          echo "     http://127.0.0.1:8080/"
          echo "   Follow Cacti's initial configuration dialogs, and then login"
          echo "   as 'admin' (with initial password 'admin') to change the"
          echo "   password to a custom one."
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} cacti status 2>/dev/null`
        [ ".$cacti_active" = .yes ] && %{l_rc} cacti restart
    fi
    exit 0

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

