##
##  owncloud.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:         owncloud
Summary:      Cloud Storage Service
URL:          http://owncloud.org/
Vendor:       ownCloud Inc.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Web
License:      AGPL
Version:      10.9.0
Release:      20211222

#   package options
%option       with_ldap  no

#   list of sources
Source0:      http://download.owncloud.org/community/owncloud-%{version}.tar.bz2
Source1:      https://apps.owncloud.com/CONTENT/content-files/168708-contacts.zip
Source2:      https://apps.owncloud.com/CONTENT/content-files/168707-calendar.zip
Source3:      owncloud-apache.conf
Source4:      rc.owncloud
Patch0:       owncloud.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       apache
PreReq:       apache-php
PreReq:       apache-php::with_json = yes
PreReq:       apache-php::with_gd = yes
PreReq:       apache-php::with_fileinfo = yes
PreReq:       apache-php::with_sqlite = yes
PreReq:       apache-php::with_curl = yes
PreReq:       apache-php::with_json = yes
PreReq:       apache-php::with_xml = yes
PreReq:       apache-php::with_simplexml = yes
PreReq:       apache-php::with_zip = yes
PreReq:       apache-php::with_mbstring = yes
PreReq:       apache-php::with_session = yes
PreReq:       apache-php::with_ctype = yes
PreReq:       apache-php::with_dom = yes
PreReq:       apache-php::with_mhash = yes
PreReq:       apache-php::with_iconv = yes
%if "%{with_ldap}" == "yes"
PreReq:       apache-php::with_openldap = yes
%endif

%description
    ownCloud enables universal access to files through the widely
    implemented WebDAV standard, providing a platform to easily view
    and sync contacts, calendars and bookmarks across devices while
    supporting sharing, viewing and editing via the web interface.
    Installation has minimal server requirements, doesn't need special
    permissions and is quick. ownCloud is extendable via a simple,
    powerful API for applications.

%track
    prog owncloud = {
        version   = %{version}
        url       = http://owncloud.org/install/
        regex     = owncloud-(\d+\.\d+\.\d+)\.tar\.bz2
    }

%prep
    %setup -q -c
    %setup -q -T -D -a 1
    %setup -q -T -D -a 2
    %patch -p0
    mv calendar owncloud/apps/
    mv contacts owncloud/apps/

%build

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/owncloud \
        $RPM_BUILD_ROOT%{l_prefix}/lib/owncloud \
        $RPM_BUILD_ROOT%{l_prefix}/var/owncloud/data \
        $RPM_BUILD_ROOT%{l_prefix}/var/owncloud/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/owncloud/run/session \
        $RPM_BUILD_ROOT%{l_prefix}/var/owncloud/log

    #   install default ownCloud configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        owncloud/config/config.sample.php \
        $RPM_BUILD_ROOT%{l_prefix}/etc/owncloud/config.php

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

    #   remove nasty files (containing whitespaces)
    rm -f "owncloud/apps/calendar/img/Icon License"
    rm -f "owncloud/apps/files_texteditor/css/DroidSansMono/Google Android License.txt"
    rm -rf "owncloud/apps/gallery/build/documentation/reports/code coverage"
    rm -f "owncloud/settings/img/log Icon License"
    rm -f "owncloud/core/skeleton/photos/san francisco.jpg"
    rm -f "owncloud/core/skeleton/Photos/San Francisco.jpg"
    rm -f "owncloud/core/skeleton/ownCloud Manual.pdf"
    rm -f "owncloud/lib/composer/rackspace/php-opencloud/docs/changelog/Upgrading to 1.7.0.md"

    #   remove empty directories
    rm -rf owncloud/config owncloud/data

    #   move ownCloud program files into place
    cp -rp owncloud/.[a-z]* owncloud/* \
        $RPM_BUILD_ROOT%{l_prefix}/lib/owncloud/

    #   link data and config directories to our location
    ( cd $RPM_BUILD_ROOT%{l_prefix}/lib/owncloud
      ln -s ../../var/owncloud/data data
      ln -s ../../etc/owncloud config
    ) || exit $?

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.owncloud} $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/owncloud' \
        '%config %attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/owncloud/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/lib/owncloud/apps' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/owncloud/.[a-z]*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/owncloud/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/owncloud/*/*'

%files -f files

%clean

%post
    #   after upgrade, restart service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} owncloud status 2>/dev/null`
    [ ".$owncloud_active" = .yes ] && %{l_rc} owncloud restart
    exit 0

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} owncloud stop 2>/dev/null
    rm -rf $RPM_INSTALL_PREFIX/var/owncloud/data/*  >/dev/null 2>&1 || true
    rm -f  $RPM_INSTALL_PREFIX/var/owncloud/log/*   >/dev/null 2>&1 || true
    rm -f  $RPM_INSTALL_PREFIX/var/owncloud/run/*/* >/dev/null 2>&1 || true
    rm -f  $RPM_INSTALL_PREFIX/var/owncloud/run/*   >/dev/null 2>&1 || true
    exit 0

