##
##  libreplan.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_libreplan    1.4.1
%define       V_jdbc_pgsql   9.4.1208

#   package information
Name:         libreplan
Summary:      Project Planning
URL:          http://www.libreplan.com/
Vendor:       Libreplan Community
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Web
License:      AGPL
Version:      %{V_libreplan}
Release:      20161213

#   list of sources
Source0:      http://download.sourceforge.net/libreplan/libreplan_%{V_libreplan}.war
Source1:      http://download.sourceforge.net/libreplan/%{V_libreplan}.sql
Source2:      http://jdbc.postgresql.org/download/postgresql-%{V_jdbc_pgsql}.jar
Source3:      libreplan.xml
Source4:      libreplan-setup.sh

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       java, JAVA-JDK
PreReq:       tomcat
PreReq:       postgresql

%description
    LibrePlan is a collaborative tool to plan, monitor and control
    projects and has a rich web interface which provides a desktop alike
    user experience. All the team members can take part in the planning
    and this makes possible to have a real-time planning .

%track
    prog libreplan = {
        version   = %{version}
        url       = https://sourceforge.net/projects/libreplan/files/LibrePlan/
        regex     = libreplan_(__VER__)\.tar\.gz
    }
    prog libreplan:jdbc-pgsql = {
        version   = %{V_jdbc_pgsql}
        url       = https://jdbc.postgresql.org/download.html
        regex     = postgresql-(\d+\.\d+-\d+)\.jar
    }

%prep
    %setup -q -T -c

%build

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/tomcat/Catalina/localhost \
        $RPM_BUILD_ROOT%{l_prefix}/var/tomcat/webapps \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/tomcat/lib \
        $RPM_BUILD_ROOT%{l_prefix}/share/libreplan \
        $RPM_BUILD_ROOT%{l_prefix}/var/libreplan/db

    #   install program
    %{l_shtool} install -c -m 644 \
        %{SOURCE0} $RPM_BUILD_ROOT%{l_prefix}/var/tomcat/webapps/libreplan.war

    #   install database setup script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e "s;@l_bash@;%{l_bash};g" \
        %{SOURCE libreplan-setup.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/libreplan-setup

    #   install database SQL commands
    %{l_shtool} install -c -m 644 \
        %{SOURCE1} $RPM_BUILD_ROOT%{l_prefix}/share/libreplan/libreplan.sql

    #   install database configuration
    %{l_shtool} install -c -m 644 \
        %{SOURCE libreplan.xml} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/tomcat/Catalina/localhost/libreplan.xml

    #   install database JDBC driver link
    %{l_shtool} install -c -m 644 \
        %{SOURCE2} \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/tomcat/lib/libreplan-jdbc-postgresql.jar

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/libreplan/db' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/tomcat/webapps/*' \
        '%not %dir %{l_prefix}/var/tomcat/webapps' \
        '%not %dir %{l_prefix}/var/tomcat'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "1. DATABASE SERVER SETUP"
          echo "   To complete this installation of LibrePlan please start"
          echo "   the RDBMS and initialize the LibrePlan database like this:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc postgresql start"
          echo "     \$ $RPM_INSTALL_PREFIX/sbin/libreplan-setup install"
          echo "   This will create a database named 'libreplan' in the RDBMS"
          echo "   with the owner 'libreplan' and password 'libreplan'."
          echo ""
          echo "2. APPLICATION SERVER SETUP"
          echo "   Now start LibrePlan by running Tomcat:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc tomcat start"
          echo "   and connect to its Manager UI:"
          echo "     http://127.0.0.1:8080/manager/"
          echo "   Click the 'start' button for the LibrePlan application."
          echo ""
          echo "3. APPLICATION USAGE"
          echo "   Finally you can connect to LibrePlan under the URL:"
          echo "     http://127.0.0.1:8080/libreplan/"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart application server
        eval `%{l_rc} tomcat status 2>/dev/null`
        [ ".$tomcat_active" = .yes ] && %{l_rc} tomcat restart
    fi
    exit 0

%preun
    #   before erase, stop service and remove log files
    if [ $1 -eq 0 ]; then
        rm -rf $RPM_INSTALL_PREFIX/var/libreplan/db/*           >/dev/null 2>&1 || true
        rm -f  $RPM_INSTALL_PREFIX/var/tomcat/log/libreplan*log >/dev/null 2>&1 || true
    fi
    exit 0

