##
##  zope.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  2.9.9
%define       V_minor  %nil

#   package information
Name:         zope
Summary:      Content Management System and Application Server
URL:          http://zope.org/
Vendor:       Zope Corporation
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        CMS
License:      Zope Public License
Version:      %{V_major}
Release:      20080510

#   list of sources
Source0:      http://www.zope.org/Products/Zope/%{V_major}/Zope-%{V_major}-final.tgz
Source1:      rc.zope
Source2:      zoperun
Source3:      zopectl
Source4:      zope.conf

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  python >= 2.3.3
PreReq:       python >= 2.3.3
BuildPreReq:  make

%description
    Zope is the leading Open Source web application server. Zope enables
    teams to collaborate in the creation and management of dynamic
    web-based business applications such as intranets and portals.
    Zope makes it easy to build features such as site search, news,
    personalization, and e-commerce into your web applications.

%track
    prog zope = {
        version   = %{V_major}
        url       = http://zope.org/Products/
        regex     = Download\s+Zope\s+(2\.9\.\d+(-\d+)?)<
    }

%prep
    %setup -q -n Zope-%{V_major}-final

%build
    #   configure package
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    ./configure \
        --prefix=%{l_prefix}/lib/zope \
        --with-python=%{l_prefix}/bin/python

    #   build package
    %{l_make} %{l_mflags}

%install

    #   install package
    %{l_make} %{l_mflags} install \
        LN=true INSTALL_FLAGS="--root=$RPM_BUILD_ROOT"
    ln -s %{l_prefix}/bin/python \
        $RPM_BUILD_ROOT%{l_prefix}/lib/zope/bin/python

    #   install addon utilities
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE zopectl} $RPM_BUILD_ROOT%{l_prefix}/bin/
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE zoperun} $RPM_BUILD_ROOT%{l_prefix}/bin/

    #   install configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/zope
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE zope.conf} $RPM_BUILD_ROOT%{l_prefix}/etc/zope/

    #   create Zope instance home
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/zope \
        $RPM_BUILD_ROOT%{l_prefix}/var/zope/import \
        $RPM_BUILD_ROOT%{l_prefix}/var/zope/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/zope/var \
        $RPM_BUILD_ROOT%{l_prefix}/var/zope/Products \
        $RPM_BUILD_ROOT%{l_prefix}/var/zope/Extensions

    #   strip down and adjust installation
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/bin/README.txt
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/zopeskel
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/share/zope

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

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/zope/zope.conf' \
        '%attr(700,%{l_rusr},%{l_rgrp}) %dir %{l_prefix}/var/zope/log' \
        '%attr(700,%{l_rusr},%{l_rgrp}) %dir %{l_prefix}/var/zope/var' \
        '%doc %{l_prefix}/share/zope/doc/*.txt'

%files -f files

%clean

%post
    #   compile python modules in place after installation
    echo "Compiling Python modules. Just be patient." |\
        %{l_rpmtool} msg -b -t info
    find $RPM_INSTALL_PREFIX/lib/zope -name "*.pyc" | xargs rm -f || true
    $RPM_INSTALL_PREFIX/bin/python \
        $RPM_INSTALL_PREFIX/libexec/zope/compilezpy.py \
        >/dev/null 2>&1 || true

    #   give information about next steps
    if [ $1 -eq 1 ]; then
        ( echo "You have successfully installed Zope. In order to use it, you first"
          echo "have to create a management user by running the following command"
          echo "(before starting up Zope):"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/zopectl adduser <user> <password>"
          echo "After this you can startup Zope with"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc zope start"
          echo "and connect to it through the URL:"
          echo "    http://localhost:8080/"
          echo "Use the created <user> and <password> to login to its management"
          echo "interface in order to configure Zope to your particular needs."
        ) | %{l_rpmtool} msg -b -t notice
    fi

%preun
    [ $1 -eq 0 ] || exit 0

    #   stop service
    %{l_rc} zope stop 2>/dev/null

    #   remove compiled python modules
    find $RPM_INSTALL_PREFIX/lib/zope -name "*.pyc" | xargs rm -f || true

    #   remove log files
    rm -rf $RPM_INSTALL_PREFIX/var/zope/var/* >/dev/null 2>&1 || true
    rm -rf $RPM_INSTALL_PREFIX/var/zope/log/* >/dev/null 2>&1 || true
    exit 0

