##
##  orangescrum.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:         orangescrum
Summary:      Scrum Project Management Tool
URL:          http://www.orangescrum.org/
Vendor:       OrangeScrum
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Web
License:      GPL
Version:      3.0.201810
Release:      20190106

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/orangescrum/orangescrum-%{version}.zip
Source1:      orangescrum-apache.conf
Source2:      orangescrum-setup.sh
Source3:      orangescrum-cron.sh
Source4:      rc.orangescrum
Patch0:       orangescrum.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       apache
PreReq:       apache-php
PreReq:       apache-php::with_session = yes
PreReq:       apache-php::with_mysql = yes
PreReq:       apache-php::with_curl = yes
PreReq:       php
PreReq:       php::with_session = yes
PreReq:       php::with_mysql = yes
PreReq:       php::with_curl = yes
PreReq:       mysql

%description
    OrangeScrum is a Web-based Project Management tool for Scrum.

%track
    prog orangescrum = {
        version   = %{version}
        url       = https://www.orangescrum.org/free-download
        regex     = OrangeScrum\s+\((__VER__)\)
    }

%prep
    %setup -q -n orangescrum-master
    %patch -p0

%build
    #   avoid problematic filenames
    mv  lib/Cake/Test/test_app/View/Themed/TestTheme/webroot/space\ image.text \
        lib/Cake/Test/test_app/View/Themed/TestTheme/webroot/space-image.text
    mv  app/webroot/csv/new\ file \
        app/webroot/csv/new-file

    #   remove useless files
    rm -f *.pdf

%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/orangescrum \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/orangescrum \
        $RPM_BUILD_ROOT%{l_prefix}/var/orangescrum/run/orangescrum-apache.session \
        $RPM_BUILD_ROOT%{l_prefix}/var/orangescrum/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/orangescrum/tmp

    #   install program files
    cp -rp .[a-z]* * $RPM_BUILD_ROOT%{l_prefix}/libexec/orangescrum/
    find $RPM_BUILD_ROOT%{l_prefix}/libexec/orangescrum -name "*.orig" -print | xargs rm -f

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

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

    #   install cron-job script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE orangescrum-cron.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/orangescrum-cron

    #   install custom Apache 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 orangescrum-apache.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/orangescrum/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/orangescrum/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/libexec/orangescrum/app' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/libexec/orangescrum/app/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/libexec/orangescrum/app/*/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/libexec/orangescrum/app/*/*/*' \
        '%config %attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/libexec/orangescrum/app/Config/*.php' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/orangescrum' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/orangescrum/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/orangescrum/*/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "1. To complete this installation of OrangeScrum please start MySQL and"
          echo "   initialize the OrangeScrum database like this:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mysql start"
          echo "     \$ $RPM_INSTALL_PREFIX/sbin/orangescrum-setup install"
          echo "   But remember to configure your MySQL installation first."
          echo "   At least set a MySQL administrator password in:"
          echo "     \$ $RPM_INSTALL_PREFIX/etc/mysql/my.pwd"
          echo ""
          echo "2. By default, OrangeScrum 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/orangescrum/orangescrum-apache.conf"
          echo ""
          echo "3. After this postinstallation, start OrangeScrum by running"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc orangescrum start"
          echo "   and initialize the OrangeScrum database by connecting to:"
          echo "     http://127.0.0.1:8081/"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} orangescrum status 2>/dev/null`
        [ ".$orangescrum_active" = .yes ] && %{l_rc} orangescrum restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   before erase, stop service and remove log files
        %{l_rc} orangescrum stop 2>/dev/null

        #   remove database
        $RPM_INSTALL_PREFIX/sbin/orangescrum-setup uninstall >/dev/null 2>&1 || true

        #   remove run-time files
        rm -f $RPM_INSTALL_PREFIX/var/orangescrum/log/* >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/orangescrum/run/* >/dev/null 2>&1 || true
    fi
    exit 0

