##
##  punbb.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:         punbb
Summary:      Web Discussion Board
URL:          http://www.punbb.org/
Vendor:       Rickard Andersson et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        CMS
License:      GPL
Version:      1.4.4
Release:      20151015

#   list of sources
Source0:      http://punbb.informer.com/download/punbb-%{version}.tar.gz
Source1:      punbb-apache.conf
Source2:      punbb-setup.sh
Source3:      rc.punbb
Patch0:       punbb.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       apache
PreReq:       apache-php
PreReq:       apache-php::with_pgsql = yes
PreReq:       apache-php::with_zlib = yes
PreReq:       apache-php::with_xml = yes
PreReq:       apache-php::with_tokenizer = yes
PreReq:       apache-php::with_ctype = yes
PreReq:       apache-php::with_curl = yes
PreReq:       pcre
PreReq:       pcre::with_utf8 = yes
PreReq:       postgresql

%description
    PunBB is a fast and lightweight PHP-powered discussion board.
    Its primary goals are to be faster, smaller and less graphically
    intensive as compared to other discussion boards. PunBB has fewer
    features than many other discussion boards, but is generally faster
    and outputs smaller, semantically correct XHTML-compliant pages.

%track
    prog punbb = {
        version   = %{version}
        url       = http://punbb.informer.com/downloads.php
        regex     = punbb-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0

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

    #   install main program files
    find . -name ".svn" -type d -print | xargs rm -rf
    find . -name "*.orig" -type f -print | xargs rm -f
    cp -rp * $RPM_BUILD_ROOT%{l_prefix}/lib/punbb/

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

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

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "To complete the PunBB installation:"
          echo "1. start the PostgreSQL RDBMS:"
          echo "   \$ $RPM_INSTALL_PREFIX/bin/openpkg rc postgresql start"
          echo "2. create the PunBB database:"
          echo "   \$ $RPM_INSTALL_PREFIX/sbin/punbb-setup install"
          echo "3. start the PunBB Apache:"
          echo "   \$ $RPM_INSTALL_PREFIX/bin/openpkg rc punbb start"
          echo "4. install the PunBB database by accessing the"
          echo "   following URL and proceeding through its menus:"
          echo "   http://localhost:8080/punbb/admin/install.php"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        [ $1 -eq 2 ] || exit 0
        eval `%{l_rc} punbb status 2>/dev/null`
        [ ".$punbb_active" = .yes ] && %{l_rc} punbb restart
    fi
    exit 0

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

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

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

        #   remove generated files
        rm -f $RPM_INSTALL_PREFIX/lib/punbb/config.php        >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/lib/punbb/cache/cache_*.php >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/lib/punbb/img/avatars/*     >/dev/null 2>&1 || true
    fi
    exit 0

