##
##  weaveserver.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:         weaveserver
Summary:      Mozilla Weave Sync Server
URL:          http://mozillalabs.com/weave/
Vendor:       Toby Elliot
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Web
License:      MPL
Version:      20091127
Release:      20100131

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/weaveserver/weave_minimal-%{version}.tgz
Source1:      weaveserver.conf
Patch0:       weaveserver.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_sqlite = yes
PreReq:       php
PreReq:       php::with_json = yes
PreReq:       php::with_sqlite = yes

%description
    This is a minimum server for the Mozilla Firefox Weave Sync addon.

%track
    prog weaveserver = {
        version   = %{version}
        url       = http://download.openpkg.org/components/versioned/weaveserver/
        regex     = weave_minimal-(__VER__)\.tgz
    }

%prep
    %setup -q -n weave_minimal
    %patch -p0

%build
    %{l_shtool} subst \
        -e 's;weave_storage\.php;%{l_prefix}/share/weaveserver/weave_storage.php;' \
        -e 's;weave_basic_object\.php;%{l_prefix}/share/weaveserver/weave_basic_object.php;' \
        create_user index.php weave_storage.php weave_basic_object.php
    %{l_shtool} subst \
        -e 's;weave_db;%{l_prefix}/var/weaveserver/weave.db;g' \
        weave_storage.php
    ( echo "#!%{l_prefix}/bin/php"
      cat create_user
    ) >create_user.sh

%install
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/apache/apache.d \
        $RPM_BUILD_ROOT%{l_prefix}/share/weaveserver \
        $RPM_BUILD_ROOT%{l_prefix}/var/weaveserver
    %{l_shtool} install -c -m 755 \
        create_user.sh $RPM_BUILD_ROOT%{l_prefix}/bin/weaveserver-createuser
    %{l_shtool} install -c -m 644 \
        index.php weave_basic_object.php weave_storage.php \
        $RPM_BUILD_ROOT%{l_prefix}/share/weaveserver/
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE weaveserver.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/apache/apache.d/
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/share/weaveserver/*' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/weaveserver'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        (   umask 022
            (echo "c"; echo "guest"; echo "guest") |\
                $RPM_INSTALL_PREFIX/bin/weaveserver-createuser
            chown %{l_nusr}:%{l_ngrp} $RPM_INSTALL_PREFIX/var/weaveserver/weave.db
            chmod 644 $RPM_INSTALL_PREFIX/var/weaveserver/weave.db
        ) >/dev/null 2>&1
    elif [ $1 -eq 2 ]; then
        eval `%{l_rc} apache status 2>/dev/null`
        [ ".$apache_active" = .yes ] && %{l_rc} apache restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        rm -f $RPM_INSTALL_PREFIX/var/weaveserver/weave.db >/dev/null 2>&1 || true
        eval `%{l_rc} apache status 2>/dev/null`
        [ ".$apache_active" = .yes ] && %{l_rc} apache restart
    fi

