##
##  joomla.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_dist   3.4.7
%define       V_opkg   3.4.7
%define       V_subdir 20184/162909

#   package information
Name:         joomla
Summary:      Content Management System (CMS)
URL:          http://www.joomla.org/
Vendor:       Open Source Matters
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        CMS
License:      GPL/LGPL
Version:      %{V_opkg}
Release:      20160227

#   list of sources
Source0:      http://joomlacode.org/gf/download/frsrelease/%{V_subdir}/Joomla_%{V_dist}-Stable-Full_Package.zip
Source1:      joomla-setup.sh
Source2:      joomla-apache.conf
Source3:      rc.joomla

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, infozip
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       apache
PreReq:       apache-php
PreReq:       apache-php::with_mysql = yes
PreReq:       apache-php::with_zlib = yes
PreReq:       apache-php::with_zip = yes
PreReq:       apache-php::with_xml = yes
PreReq:       apache-php::with_simplexml = yes
PreReq:       apache-php::with_gd = yes
PreReq:       apache-php::with_session = yes
PreReq:       apache-php::with_mm = yes
PreReq:       apache-php::with_sendmail = yes
PreReq:       apache-php::with_json = yes
PreReq:       MTA

%description
    Joomla! is a popular Web-based Content Management System (CMS).

%track
    prog joomla = {
        version   = %{V_dist}
        url       = http://joomlacode.org/gf/project/joomla/frs/
        regex     = frsrelease/\d+/\d+/Joomla_(__VER__)-Stable-Full_Package\.zip
    }
    prog joomla:subdir = {
        version   = %{V_subdir}
        url       = http://joomlacode.org/gf/project/joomla/frs/
        regex     = frsrelease/(\d+/\d+)/Joomla-__VER__-Stable-Full_Package\.zip
    }

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

%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/joomla \
        $RPM_BUILD_ROOT%{l_prefix}/lib/joomla/runtime \
        $RPM_BUILD_ROOT%{l_prefix}/var/joomla/db \
        $RPM_BUILD_ROOT%{l_prefix}/var/joomla/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/joomla/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/joomla/run/joomla-apache.php-session

    #   adjust default configuration
    %{l_shtool} subst \
        -e 's;/usr/sbin/sendmail;%{l_prefix}/sbin/sendmail;' \
        installation/template/tmpl/configuration.html

    #   install main program files
    find . -name ".#*" -print | xargs rm -f
    find . -name "*.orig" -print | xargs rm -f
    cp -rp * $RPM_BUILD_ROOT%{l_prefix}/lib/joomla/runtime/
    rm -f $RPM_BUILD_ROOT%{l_prefix}/lib/joomla/runtime/configuration.php-dist
    rm -f $RPM_BUILD_ROOT%{l_prefix}/lib/joomla/runtime/htaccess.txt

    #   post-adjustment: move risky installation area out of runtime area
    #   (will be dynmically linked in again later)
    mv  $RPM_BUILD_ROOT%{l_prefix}/lib/joomla/runtime/installation \
        $RPM_BUILD_ROOT%{l_prefix}/lib/joomla/installation

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e 's;@l_path@;%{l_build_path};' \
        %{SOURCE rc.joomla} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

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

    #   install 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 joomla-apache.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/joomla/

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "To complete the Joomla! installation:"
          echo "1. start the MySQL RDBMS:"
          echo "   \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mysql start"
          echo "2. setup the Joomla! database in the MySQL RDBMS:"
          echo "   \$ $RPM_INSTALL_PREFIX/sbin/joomla-setup install"
          echo "3. start the Joomla! Apache webserver:"
          echo "   \$ $RPM_INSTALL_PREFIX/bin/openpkg rc joomla start"
          echo "4. setup Joomla! through the Web installer..."
          echo "   http://localhost:8080/joomla/"
          echo "   ..and on page \"2: Database\" use:"
          echo "   - Host Name:     \"localhost\""
          echo "   - User Name:     \"joomla\""
          echo "   - Password:      \"joomla\""
          echo "   - Database Name: \"joomla\""
          echo "5. fixup installation afterwards:"
          echo "   \$ $RPM_INSTALL_PREFIX/sbin/joomla-setup cleanup"
          echo "6. access Joomla! via:"
          echo "   http://localhost:8080/joomla/              (website view)"
          echo "   http://localhost:8080/joomla/administrator (website admin)"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        rm -f $RPM_INSTALL_PREFIX/lib/joomla/runtime/installation
        [ $1 -eq 2 ] || exit 0
        eval `%{l_rc} joomla status 2>/dev/null`
        [ ".$joomla_active" = .yes ] && %{l_rc} joomla restart
    fi
    exit 0

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

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

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

