##
##  phpbb.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:         phpbb
Summary:      Web Bulletin Board System (BBS)
URL:          http://www.phpbb.com/
Vendor:       phpBB Group
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        CMS
License:      GPL
Version:      3.0.12
Release:      20150117

#   list of sources
Source0:      http://download.sourceforge.net/phpbb/phpBB-%{version}.tar.bz2
Source2:      phpbb-apache.conf
Source3:      rc.phpbb
Patch0:       phpbb.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       apache
PreReq:       apache-php::with_gd = yes
PreReq:       apache-php::with_zlib = yes
PreReq:       apache-php::with_ftp = yes
PreReq:       apache-php::with_xml = yes
PreReq:       apache-php::with_sqlite = yes
PreReq:       pcre, pcre::with_utf8 = yes
PreReq:       sqlite
PreReq:       imagemagick

%description
    phpBB is a high powered, fully scalable, and highly customizable
    Open Source Bulletin Board System (BBS). phpBB has a user-friendly
    interface, simple and straightforward administration panel, and
    helpful FAQ. Based on the powerful PHP server language and a backend
    RDBMS, phpBB is the ideal free community solution for all web sites.

%track
    prog phpbb = {
        version   = %{version}
        url       = http://sourceforge.net/projects/phpbb/files/
        regex     = phpBB-(__VER__)\.tar\.(gz|bz2)
    }

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

%build

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/phpbb \
        $RPM_BUILD_ROOT%{l_prefix}/lib/phpbb/runtime \
        $RPM_BUILD_ROOT%{l_prefix}/var/phpbb/db \
        $RPM_BUILD_ROOT%{l_prefix}/var/phpbb/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/phpbb/run

    #   install main program files
    %{l_shtool} subst %{l_value -s -a} \
        install/install_install.php
    find . -name ".#*" -print | xargs rm -f
    cp -rp * $RPM_BUILD_ROOT%{l_prefix}/lib/phpbb/runtime/

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

    #   post-adjustment: move writable areas out of runtime area
    mv  $RPM_BUILD_ROOT%{l_prefix}/lib/phpbb/runtime/cache \
        $RPM_BUILD_ROOT%{l_prefix}/var/phpbb/db/cache
    ln -s ../../../var/phpbb/db/cache \
        $RPM_BUILD_ROOT%{l_prefix}/lib/phpbb/runtime/cache
    mv  $RPM_BUILD_ROOT%{l_prefix}/lib/phpbb/runtime/files \
        $RPM_BUILD_ROOT%{l_prefix}/var/phpbb/db/files
    ln -s ../../../var/phpbb/db/files \
        $RPM_BUILD_ROOT%{l_prefix}/lib/phpbb/runtime/files
    mv  $RPM_BUILD_ROOT%{l_prefix}/lib/phpbb/runtime/store \
        $RPM_BUILD_ROOT%{l_prefix}/var/phpbb/db/store
    ln -s ../../../var/phpbb/db/store \
        $RPM_BUILD_ROOT%{l_prefix}/lib/phpbb/runtime/store
    mv  $RPM_BUILD_ROOT%{l_prefix}/lib/phpbb/runtime/images/avatars/upload \
        $RPM_BUILD_ROOT%{l_prefix}/var/phpbb/db/upload
    ln -s ../../../../../var/phpbb/db/store \
        $RPM_BUILD_ROOT%{l_prefix}/lib/phpbb/runtime/images/avatars/upload
    mv  $RPM_BUILD_ROOT%{l_prefix}/lib/phpbb/runtime/config.php \
        $RPM_BUILD_ROOT%{l_prefix}/etc/phpbb/config.php
    ln -s ../../../etc/phpbb/config.php \
        $RPM_BUILD_ROOT%{l_prefix}/lib/phpbb/runtime/config.php

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e 's;@l_path@;%{l_build_path};' \
        %{SOURCE rc.phpbb} $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 phpbb-apache.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/phpbb/

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ln -s ../install $RPM_INSTALL_PREFIX/lib/phpbb/runtime/install
        ( echo "To complete the phpBB installation:"
          echo "1. start the phpBB Apache:"
          echo "   \$ $RPM_INSTALL_PREFIX/bin/openpkg rc phpbb start"
          echo "2. install the phpBB database by accessing the"
          echo "   following URL and proceeding through its menus:"
          echo "   http://localhost:8080/phpbb/install/"
          echo "3. cleanup after installation to enable run-time:"
          echo "   \$ rm -f $RPM_INSTALL_PREFIX/lib/phpbb/runtime/install"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        [ $1 -eq 2 ] || exit 0
        eval `%{l_rc} phpbb status 2>/dev/null`
        [ ".$phpbb_active" = .yes ] && %{l_rc} phpbb restart
    fi
    exit 0

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

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

