##
##  roundcube.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:         roundcube
Summary:      Mail Frontend
URL:          http://roundcube.net/
Vendor:       Thomas Bruederli et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Mail
License:      GPL
Version:      1.6.11
Release:      20250602

#   list of sources
Source0:      https://github.com/roundcube/roundcubemail/releases/download/%{version}/roundcubemail-%{version}.tar.gz
Source1:      roundcube-apache.conf
Source2:      roundcube-setup.sh
Source3:      rc.roundcube
Patch0:       roundcube.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, rsync
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       apache
PreReq:       apache-php
PreReq:       apache-php::with_json = yes
PreReq:       apache-php::with_session = yes
PreReq:       apache-php::with_mbstring = yes
PreReq:       apache-php::with_ctype = yes
PreReq:       apache-php::with_iconv = yes
PreReq:       apache-php::with_mhash = yes
PreReq:       apache-php::with_mcrypt = yes
PreReq:       apache-php::with_sockets = yes
PreReq:       apache-php::with_fileinfo = yes
PreReq:       apache-php::with_ssl = yes
PreReq:       apache-php::with_zip = yes
PreReq:       apache-php::with_dom = yes
PreReq:       apache-php::with_xml = yes
PreReq:       apache-php::with_exif = yes
PreReq:       apache-php::with_pgsql = yes

%description
    Roundcube is a browser-based multilingual IMAP/SMTP client with an
    application-like user interface. It provides full functionality you
    expect from an email client, including MIME support, address book,
    folder manipulation, message searching and spell checking.

%track
    prog roundcube = {
        version   = %{version}
        url       = http://roundcube.net/download/
        regex     = roundcubemail-(\d+\.\d+(?:\.\d+)*)\.tar\.gz
    }

%prep
    %setup -q -n roundcubemail-%{version}
    %patch -p0

%build
    #   patch sources
    %{l_shtool} subst %{l_value -s -a} \
        config/defaults.inc.php

    #   remove some useless files
    rm -f CHANGELOG README.md LICENSE INSTALL UPGRADING
    rm -f composer.json-dist
    rm -rf temp logs

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

    #   install program files
    rsync -ra * \
        $RPM_BUILD_ROOT%{l_prefix}/lib/roundcube/

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

    #   install default Apache webserver 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 roundcube-apache.conf} $RPM_BUILD_ROOT%{l_prefix}/etc/roundcube/

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

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   on initial installation, give hints about next steps
        ( echo "1. To complete this installation of Roundcube please start PostgreSQL and"
          echo "   initialize the Roundcube database like this:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc postgresql start"
          echo "     \$ $RPM_INSTALL_PREFIX/sbin/roundcube-setup install"
          echo "   This will create a database named 'roundcube' in the RDBMS"
          echo "   with the owner 'roundcube' and password 'roundcube'."
          echo ""
          echo "2. By default, Roundcube runs its own Apache server on IPv4 address"
          echo "   127.0.0.1, TCP port 8081. Please change this by editing the"
          echo "   \"Listen 127.0.0.1:8081\" directives in the configuration file"
          echo "   $RPM_INSTALL_PREFIX/etc/roundcube/roundcube-apache.conf"
          echo ""
          echo "3. After this postinstallation, start Roundcube by running"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc roundcube start"
          echo "   and initialize the Roundcube database by connecting to:"
          echo "     http://127.0.0.1:8081/"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} roundcube status 2>/dev/null`
        [ ".$roundcube_active" = .yes ] && %{l_rc} roundcube restart
    fi
    exit 0

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

