##
##  wordpress.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:         wordpress
Summary:      Weblog Publishing System
URL:          http://wordpress.org/
Vendor:       R. Boren & M. Mullenweg et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        CMS
License:      GPL
Version:      6.8.3
Release:      20251005

#   list of sources
Source0:      http://wordpress.org/wordpress-%{version}.tar.gz
Source1:      wordpress-apache.conf
Source2:      wordpress-setup.sh
Source3:      wordpress-cron.sh
Source4:      rc.wordpress
Patch0:       wordpress.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_zlib = yes
PreReq:       apache-extfwd
PreReq:       mysql

%description
    WordPress is a state-of-the-art semantic personal publishing
    platform (aka Weblog or BLOG) with a focus on aesthetics, web
    standards, and usability.

%track
    prog wordpress = {
        version   = %{version}
        url       = http://wordpress.org/download/release-archive/
        regex     = wordpress-(\d+\.\d+(\.\d+)?)\.tar\.gz
    }

%prep
    %setup -q -n wordpress
    %patch -p0

%build
    #   create configuration file
    %{l_shtool} install -m 644 \
        -e 's;database_name_here;wordpress;g' \
        -e 's;username_here;wordpress;g' \
        -e 's;password_here;wordpress;g' \
        wp-config-sample.php wp-config.php

%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/wordpress \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/wordpress \
        $RPM_BUILD_ROOT%{l_prefix}/var/wordpress/run/wordpress-apache.session \
        $RPM_BUILD_ROOT%{l_prefix}/var/wordpress/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/wordpress/tmp

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

    #   create additional directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/wordpress/wp-content/backup-db \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/wordpress/wp-content/uploads

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

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

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

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/wordpress/*' \
        '%config %{l_prefix}/libexec/wordpress/wp-config.php' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/wordpress' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/wordpress/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/wordpress/*/*' \
        '%attr(-,%{l_rusr},%{l_ngrp}) %{l_prefix}/libexec/wordpress/wp-content/uploads' \
        '%attr(-,%{l_rusr},%{l_ngrp}) %{l_prefix}/libexec/wordpress/wp-content/backup-db'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "1. To complete this installation of Wordpress please start MySQL and"
          echo "   initialize the Wordpress database like this:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mysql start"
          echo "     \$ $RPM_INSTALL_PREFIX/sbin/wordpress-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, Wordpress 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/wordpress/wordpress-apache.conf"
          echo ""
          echo "3. After this postinstallation, start Wordpress by running"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc wordpress start"
          echo "   and initialize the Wordpress database by connecting to:"
          echo "     http://127.0.0.1:8081/wp-admin/install.php"
          echo ""
          echo "4. If Wordpress operates behind a HTTPS reverse proxy, enable"
          echo "   the \"SetEnv\" directive in wordpress-apache.conf and force"
          echo "   the Wordpress \"siteurl\" configuration variable via:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/mysql --user=wordpress \\%{l_nil}"
          echo "       --password=wordpress wordpress"
          echo "     | UPDATE wp_options SET option_value = \"<url>\" \\%{l_nil}"
          echo "     | WHERE option_name IN (\"siteurl\", \"home\");"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   display final hints on update installation
        ( echo "Please perform a Wordpress database upgrade by visiting the"
          echo "     http://127.0.0.1:8081/wp-admin/upgrade.php"
          echo "page on your Wordpress website."
        ) | %{l_rpmtool} msg -b -t notice

        #   after upgrade, restart service
        eval `%{l_rc} wordpress status 2>/dev/null`
        [ ".$wordpress_active" = .yes ] && %{l_rc} wordpress restart
    fi
    exit 0

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

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

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

