##
##  matomo.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:         matomo
Summary:      Open Analytics Platform
URL:          http://matomo.org/
Vendor:       Matthieu Aubry et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Web
License:      GPL
Version:      5.3.0
Release:      20250313

#   list of sources
Source0:      http://builds.matomo.org/piwik-%{version}.zip
Source1:      matomo-apache.conf
Source2:      matomo-setup.sh
Source3:      matomo-cron.sh
Source4:      matomo.sh
Source5:      rc.matomo
Patch0:       matomo.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, infozip
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       apache, wget
PreReq:       apache-php
PreReq:       apache-php::with_session = yes
PreReq:       apache-php::with_mysql = yes
PreReq:       apache-php::with_sendmail = yes
PreReq:       apache-php::with_json = yes
PreReq:       apache-php::with_zlib = yes
PreReq:       apache-php::with_iconv = yes
PreReq:       apache-php::with_dom = yes
PreReq:       apache-php::with_xml = yes
PreReq:       apache-php::with_simplexml = yes
PreReq:       apache-php::with_freetype = yes
PreReq:       apache-php::with_gd = yes
PreReq:       apache-extfwd
PreReq:       mysql

%description
    Matomo (formerly Piwik) is the leading open-source analytics
    platform that gives you more than just powerful analytics: free
    open-source software, 100% data ownership, user privacy protection,
    user-centric insights, and customisable and extensible.

%track
    prog matomo = {
        version   = %{version}
        url       = http://builds.matomo.org/
        regex     = piwik-(\d+\.\d+(\.\d+)?)\.zip
    }

%prep
    %setup -q -T -c
    unzip -q -x %{SOURCE0}
    %patch -p0

%build
    #   rename problematic files
    cd piwik
    mv  "plugins/Morpheus/icons/dist/brand/China Mobile.png" \
        "plugins/Morpheus/icons/dist/brand/China-Mobile.png"
    mv  "misc/How to install Matomo.html" \
        "misc/How-to-install-Matomo.html"

%install
    #   create installation hierarchy
    cd piwik
    %{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/matomo \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/matomo \
        $RPM_BUILD_ROOT%{l_prefix}/var/matomo/run/matomo-apache.session \
        $RPM_BUILD_ROOT%{l_prefix}/var/matomo/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/matomo/tmp

    #   install program files
    cp -rp * $RPM_BUILD_ROOT%{l_prefix}/libexec/matomo/
    find $RPM_BUILD_ROOT%{l_prefix}/libexec/matomo -name "*.orig" -print | xargs rm -f

    #   redirect some run-time writeable directories
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/libexec/matomo/tmp
    ln -s ../../var/matomo/tmp $RPM_BUILD_ROOT%{l_prefix}/libexec/matomo/tmp
    mv  $RPM_BUILD_ROOT%{l_prefix}/libexec/matomo/config/* \
        $RPM_BUILD_ROOT%{l_prefix}/etc/matomo/
    rmdir $RPM_BUILD_ROOT%{l_prefix}/libexec/matomo/config
    ln -s ../../etc/matomo $RPM_BUILD_ROOT%{l_prefix}/libexec/matomo/config

    #   install console script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE matomo.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/matomo

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

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

    #   install cron-job script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE matomo-cron.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/matomo-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 matomo-apache.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/matomo/

    #   determine installation files
    %{l_rpmtool} files -v -o../files -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/matomo' \
        '%config %attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/matomo/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/libexec/matomo' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/libexec/matomo/tmp' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/libexec/matomo/piwik.js' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/matomo' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/matomo/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/matomo/*/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "1. To complete this installation of Matomo please start MySQL and"
          echo "   initialize the Matomo database like this:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mysql start"
          echo "     \$ $RPM_INSTALL_PREFIX/sbin/matomo-setup install"
          echo "   But remember to configure your MySQL installation first."
          echo "   At least set a MySQL administrator password in:"
          echo "     \$ $RPM_INSTALL_PREFIX/etc/mysql/my.pwd"
          echo ""
          echo "2. By default, Matomo runs its own Apache server on IPv4 address"
          echo "   127.0.0.1, TCP port 8081. Please change this by editing the"
          echo "   \"Listen 127.0.0.1:8081\" directives in the configuration file"
          echo "   $RPM_INSTALL_PREFIX/etc/matomo/matomo-apache.conf"
          echo ""
          echo "3. After this postinstallation, start Matomo by running"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc matomo start"
          echo "   and initialize the Matomo database by connecting to:"
          echo "     http://127.0.0.1:8081/"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} matomo status 2>/dev/null`
        [ ".$matomo_active" = .yes ] && %{l_rc} matomo restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   before erase, stop service and remove log files
        %{l_rc} matomo stop 2>/dev/null

        #   remove database
        $RPM_INSTALL_PREFIX/sbin/matomo-setup uninstall >/dev/null 2>&1 || true

        #   remove run-time files
        rm -f $RPM_INSTALL_PREFIX/var/matomo/log/* >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/matomo/run/* >/dev/null 2>&1 || true
    fi
    exit 0

