##
##  davical.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_opkg    1.1.3.1
%define       V_davical 1.1.3.1
%define       V_awl     0.55

#   package information
Name:         davical
Summary:      CalDAV Server
URL:          http://www.davical.org/
Vendor:       Andrew McMillan
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      GPL
Version:      %{V_opkg}
Release:      20161213

#   package options
%define       with_ssl  no

#   list of sources
Source0:      https://gitlab.com/davical-project/davical/repository/davical-%{V_davical}.tar.gz
Source1:      https://gitlab.com/davical-project/awl/repository/awl-%{V_awl}.tar.gz
Source2:      davical-config.php
Source3:      davical-apache.conf
Source4:      davical-setup.sh
Source5:      rc.davical
Patch0:       davical.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       pcre, pcre::with_utf8
PreReq:       apache
%if "%{with_ssl}" == "yes"
PreReq:       apache::with_mod_ssl = yes, x509
%endif
PreReq:       apache-php
PreReq:       apache-php::with_pgsql = yes
PreReq:       apache-php::with_gettext = yes
PreReq:       postgresql

%description
    The DAViCal CalDAV Server is a repository for calendar, schedule,
    todo and journal entries which may be accessed with CalDAV capable
    client software.

%track
    prog davical:davical = {
        version   = %{V_davical}
        url       = https://gitlab.com/davical-project/davical/tags
        regex     = davical-(__VER__)\.tar\.gz
    }
    prog davical:awl = {
        version   = %{V_awl}
        url       = https://gitlab.com/davical-project/awl/tags
        regex     = awl-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -c
    %setup -q -T -D -a 1
    %patch -p0

%build
    ln -s awl.git awl
    ( cd davical.git
      %{l_shtool} subst %{l_value -s -a} inc/always.php.in
      %{l_make} %{l_mflags} htdocs/always.php
    ) || exit $?

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

    #   install program components
    find . -name "*.orig" -print | xargs rm -f
    cp -r \
        awl.git/dba \
        awl.git/inc \
        $RPM_BUILD_ROOT%{l_prefix}/lib/davical/awl/
    cp -r \
        davical.git/dba \
        davical.git/inc \
        davical.git/htdocs \
        $RPM_BUILD_ROOT%{l_prefix}/lib/davical/davical/

    #   install DAViCal application configuration file
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE davical-config.php} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/davical/

    #   install DAViCal Apache configuration file
    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 davical-apache.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/davical/

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

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

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

%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 PostgreSQL"
          echo "   and initialize the DAViCal database like this:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc postgresql start"
          echo "     \$ $RPM_INSTALL_PREFIX/sbin/davical-setup install"
          echo ""
%if "%{with_ssl}" == "yes"
          echo "2. By default, DAViCal runs its own SSL/TLS based Apache server"
          echo "   under 127.0.0.1, TCP port 8443. Please change this by editing"
          echo "   the \"Listen 127.0.0.1:8443\" directive in:"
%else
          echo "2. By default, DAViCal runs its own Apache server"
          echo "   under 127.0.0.1, TCP port 8080. Please change this by editing"
          echo "   the \"Listen 127.0.0.1:8080\" directive in:"
%endif
          echo "     $RPM_INSTALL_PREFIX/etc/davical/davical-apache.conf"
          echo ""
          echo "3. Now start DAViCal by running the command"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc davical start"
          echo "   and connect with a browser to the URL"
%if "%{with_ssl}" == "yes"
          echo "     https://127.0.0.1:8443/"
%else
          echo "     https://127.0.0.1:8080/"
%endif
          echo "   and create user accounts."
          echo ""
          echo "4. Now you are able to subscribe to user \"example\"'s \"home\""
          echo "   calendar with a CalDAV client through the URL"
%if "%{with_ssl}" == "yes"
          echo "     https://127.0.0.1:8443/caldav/example/home"
%else
          echo "     http://127.0.0.1:8080/caldav/example/home"
%endif
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} davical status 2>/dev/null`
        [ ".$davical_active" = .yes ] && %{l_rc} davical restart
    fi
    exit 0

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

