##
##  drupal.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:         drupal
Summary:      Content Management Platform
URL:          http://www.drupal.org/
Vendor:       Dries Buytaert et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        CMS
License:      GPL
Version:      6.22
Release:      20110526

#   package options
%option       with_mysql   yes
%option       with_pgsql   no
%option       with_dcron   no

#   checking for option conflicts
%if "%{with_mysql}" == "no" && "%{with_pgsql}" == "no"
    %{error:one of the build-time options 'with_mysql' or 'with_pgsql' have to be enabled}
%endif
%if "%{with_mysql}" == "yes" && "%{with_pgsql}" == "yes"
    %{error:only one of the build-time options 'with_mysql' or 'with_pgsql' can be enabled at a time}
%endif

#   list of sources
Source0:      http://ftp.drupal.org/files/projects/drupal-%{version}.tar.gz
Source1:      drupal-setup.sh
Source2:      drupal-cron.sh
Source3:      drupal-apache.conf
Source4:      rc.drupal
Patch0:       drupal.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, infozip
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       pcre, pcre::with_utf8
PreReq:       apache
PreReq:       apache::with_mod_deflate = yes
PreReq:       apache-php
PreReq:       apache-php::with_ctype = yes
PreReq:       apache-php::with_gd = yes
PreReq:       apache-php::with_mbstring = yes
PreReq:       apache-php::with_session = yes
PreReq:       apache-php::with_mm = yes
PreReq:       apache-php::with_xml = yes
PreReq:       apache-php::with_xslt = yes
PreReq:       apache-php::with_sendmail = yes
%if "%{with_mysql}" == "yes"
PreReq:       apache-php::with_mysql = yes
%endif
%if "%{with_pgsql}" == "yes"
PreReq:       apache-php::with_pgsql = yes
%endif
%if "%{with_dcron}" == "yes"
PreReq:       dcron
%endif
PreReq:       MTA

%description
    Drupal is a dynamic web site platform which allows an individual
    or community of users to publish, manage and organize a
    variety of content, Drupal integrates many popular features of
    content management systems, weblogs, collaborative tools and
    discussion-based community software into one easy-to-use package.

%track
    prog drupal = {
        version   = %{version}
        url       = http://ftp.drupal.org/files/projects/
        regex     = drupal-(6\.\d+)\.tar\.gz
    }

%prep
    #   unpack core distribution
    %setup -q -n drupal-%{version}

    #   apply local patches
    %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/drupal/sites \
        $RPM_BUILD_ROOT%{l_prefix}/share/drupal \
        $RPM_BUILD_ROOT%{l_prefix}/var/drupal/files \
        $RPM_BUILD_ROOT%{l_prefix}/var/drupal/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/drupal/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/drupal/dump \
        $RPM_BUILD_ROOT%{l_prefix}/var/drupal/db

    #   avoid copying unnecessary text files
    find . -name "*.orig" -print | xargs rm -f
    rm -f CHANGELOG.txt COPYRIGHT.txt INSTALL.*.txt INSTALL.txt
    rm -f LICENSE.txt MAINTAINERS.txt UPGRADE.txt

    #   install all files (and strip down installation afterwards)
    cp -rp * .htaccess \
        $RPM_BUILD_ROOT%{l_prefix}/share/drupal/

    #   pre-configure settings.php
    %{l_shtool} subst \
%if "%{with_mysql}" == "yes"
        -e 's|^\( *\$db_url\) *=.*$|\1 = "mysql://drupal:drupal@127.0.0.1/drupal";|' \
%endif
%if "%{with_pgsql}" == "yes"
        -e 's|^\( *\$db_url\) *=.*$|\1 = "pgsql://drupal:drupal@127.0.0.1/drupal";|' \
%endif
        $RPM_BUILD_ROOT%{l_prefix}/share/drupal/sites/default/default.settings.php

    #   set data location
    ln -s ../../../../var/drupal/files \
        $RPM_BUILD_ROOT%{l_prefix}/share/drupal/sites/default/files

    #   install run-command script
%if "%{with_dcron}" == "yes"
    l_drupal_cron="no"
%else
    l_drupal_cron="yes"
%endif
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e "s;@l_drupal_cron@;$l_drupal_cron;g" \
        %{SOURCE rc.drupal} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   install database setup script
%if "%{with_mysql}" == "yes"
    l_dbtype="mysql"
%endif
%if "%{with_pgsql}" == "yes"
    l_dbtype="pgsql"
%endif
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e "s;@l_dbtype@;$l_dbtype;g" \
        -e "s;@l_bash@;%{l_bash};g" \
        %{SOURCE drupal-setup.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/drupal-setup

    #   install crond(8) job script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE drupal-cron.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/drupal-cron
%if "%{with_dcron}" == "yes"
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/dcron/crontabs
    echo "*/5 * * * * su %{l_rusr} -c %{l_prefix}/sbin/drupal-cron" \
        >$RPM_BUILD_ROOT%{l_prefix}/etc/dcron/crontabs/drupal
%endif

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

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/drupal' \
%if "%{with_dcron}" == "yes"
        '%not %dir %{l_prefix}/etc/dcron' \
        '%not %dir %{l_prefix}/etc/dcron/crontabs' \
        '%attr(-,%{l_susr},%{l_mgrp}) %{l_prefix}/etc/dcron/crontabs/drupal' \
%endif
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/share/drupal' \
        '%attr(555,%{l_rusr},%{l_rgrp}) %{l_prefix}/share/drupal/sites/default' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/drupal'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "1. To complete this installation of Drupal please start the RDBMS"
          echo "   and initialize the Drupal database like this:"
%if "%{with_mysql}" == "yes"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mysql start"
%endif
%if "%{with_pgsql}" == "yes"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc postgresql start"
%endif
          echo "     \$ $RPM_INSTALL_PREFIX/sbin/drupal-setup install"
          echo ""
          echo "2. Now start Drupal by running the command"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc drupal start"
          echo "   and connect with a browser to the URL:"
          echo "     http://127.0.0.1:8080/drupal/install.php"
          echo "   Follow the instructions. On the page \"Database configuration\""
%if "%{with_mysql}" == "yes"
          echo "   use database type \"mysql\", database name \"drupal\", database"
%endif
%if "%{with_pgsql}" == "yes"
          echo "   use database type \"pgsql\", database name \"drupal\", database"
%endif
          echo "   username \"drupal\" and database password \"drupal\". Also create"
          echo "   an account and login. The first account will automatically become"
          echo "   the main administrator account with total control over Drupal."
          echo ""
          echo "3. By default, Drupal runs its own Apache server on IPv4 address"
          echo "   127.0.0.1, TCP port 8080. Please change this by editing the"
          echo "   \"Listen 127.0.0.1:8080\" directive in"
          echo "     $RPM_INSTALL_PREFIX/etc/drupal/drupal-apache.conf"
          echo "   Also do not forget to cp -p"
          echo " $RPM_INSTALL_PREFIX/share/drupal/sites/default/settings.php"
          echo "     $RPM_INSTALL_PREFIX/share/drupal/sites/settings.php"
          echo "   and adjust the \$base_url variable."
          echo ""
          echo "4. Access Drupal via:"
          echo "     http://127.0.0.1:8080/drupal/      (website view)"
          echo "     http://127.0.0.1:8080/drupal/admin (website admin)"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   display final hints on update installation
        ( echo "Please perform a Drupal database upgrade by visiting the"
          echo "     http://127.0.0.1:8080/drupal/update.php"
          echo "page on your Drupal website."
        ) | %{l_rpmtool} msg -b -t notice

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

%preun
    if [ $1 -eq 0 ]; then
        #   before erase, stop service and remove log files
        %{l_rc} drupal stop 2>/dev/null
        $RPM_INSTALL_PREFIX/sbin/drupal-setup uninstall >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/drupal/files/* >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/drupal/log/*   >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/drupal/run/*   >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/drupal/dump/*  >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/drupal/db/*    >/dev/null 2>&1 || true
    fi
    exit 0

