##
##  mediawiki.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_major 1.15
%define       V_minor 4

#   package information
Name:         mediawiki
Summary:      Wikipedia Wiki Engine
URL:          http://www.mediawiki.org/
Vendor:       Wikipedia
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        CMS
License:      GPL
Version:      %{V_major}.%{V_minor}
Release:      20100529

#   list of sources
Source0:      http://download.wikimedia.org/mediawiki/%{V_major}/mediawiki-%{version}.tar.gz
Source1:      mediawiki-setup.sh
Source2:      mediawiki-apache.conf
Source3:      rc.mediawiki
Patch0:       mediawiki.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       mysql
PreReq:       apache
PreReq:       apache-php
PreReq:       apache-php::with_mm = yes
PreReq:       apache-php::with_xml = yes
PreReq:       apache-php::with_mysql = yes
PreReq:       apache-php::with_gd = yes
PreReq:       pcre, pcre::with_utf8 = yes
PreReq:       diffutils
PreReq:       imagemagick
PreReq:       memcached
PreReq:       w3m

%description
    MediaWiki is the Wiki engine behind Wikipedia. It is a very powerful
    and full-featured RDBMS based Wiki engine written in PHP.

%track
    prog mediawiki = {
        version   = %{version}
        url       = http://www.mediawiki.org/wiki/Download
        regex     = mediawiki-(\d+\.\d+\.\d+)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0

%build
    mv config/index.php config_index.php.in
    rm -rf config

%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/mediawiki \
        $RPM_BUILD_ROOT%{l_prefix}/lib/mediawiki \
        $RPM_BUILD_ROOT%{l_prefix}/var/mediawiki/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/mediawiki/log

    #   install main program files
    cp -rp * $RPM_BUILD_ROOT%{l_prefix}/lib/mediawiki/
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/lib/mediawiki/docs
    rm -f $RPM_BUILD_ROOT%{l_prefix}/lib/mediawiki/[A-Z][A-Z]*

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

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

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

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "To complete the WikiMedia installation:"
          echo "1. start the MySQL RDBMS and the MediaWiki Apache:"
          echo "   \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mysql start"
          echo "   \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mediawiki start"
          echo "2. create the MediaWiki database in the MySQL RDBMS:"
          echo "   \$ $RPM_INSTALL_PREFIX/sbin/mediawiki-setup install"
          echo "3. run the MediaWiki setup:"
          echo "   http://localhost:8080/mediawiki/config/"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        [ $1 -eq 2 ] || exit 0
        eval `%{l_rc} mediawiki status 2>/dev/null`
        [ ".$mediawiki_active" = .yes ] && %{l_rc} mediawiki restart
    fi
    exit 0

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

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

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

