##
##  jira.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_jira          8.13.0
%define       V_jdbc_mysql    5.1.49
%define       V_jdbc_pgsql    42.2.18

#   package information
Name:         jira
Summary:      Issue Tracking System
URL:          https://www.atlassian.com/software/jira
Vendor:       Atlassian
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Ticketing
License:      Commercial
Version:      %{V_jira}
Release:      20201015

#   package options
%option       with_mysql  no
%option       with_pgsql  no

#   package source
Source0:      http://product-downloads.atlassian.com/software/jira/downloads/atlassian-jira-software-%{V_jira}.tar.gz
Source1:      http://ftp.gwdg.de/pub/misc/mysql/Downloads/Connector-J/mysql-connector-java-%{V_jdbc_mysql}.tar.gz
Source2:      http://jdbc.postgresql.org/download/postgresql-%{V_jdbc_pgsql}.jar
Source3:      rc.jira
Source4:      jira-setup.sh

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       java, JAVA-JDK
%if "%{with_mysql}" == "yes"
PreReq:       mysql
%endif
%if "%{with_pgsql}" == "yes"
PreReq:       postgresql
%endif

%description
    JIRA is the tracker for teams planning and building great products.
    Thousands of teams choose JIRA to capture and organize issues,
    assign work, and follow team activity. At your desk or on the go
    with the new mobile interface, JIRA helps your team get the job
    done.

%track
    prog jira:jira = {
        version   = %{V_jira}
        url       = https://www.atlassian.com/software/jira/download
        regex     = atlassian-jira-software-(__VER__)\.tar\.gz
    }
    prog jira:jdbc-mysql = {
        version   = %{V_jdbc_mysql}
        url       = http://dev.mysql.com/downloads/connector/j/5.1.html
        regex     = mysql-connector-java-(__VER__)\.tar\.gz
    }
    prog jira:jdbc-pgsql = {
        version   = %{V_jdbc_pgsql}
        url       = https://jdbc.postgresql.org/download.html
        regex     = postgresql-(\d+\.\d+\.\d+)\.jar
    }

%prep
    %setup -q -n atlassian-jira-software-%{V_jira}-standalone
    %setup -q -n atlassian-jira-software-%{V_jira}-standalone -T -D -a 1

%build
    #   remove useless empty files
    find . -name ".gitkeep"  -print | xargs rm -f
    find . -name ".emptydir" -print | xargs rm -f

%install
    #   create installation directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/jira/data \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/jira \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/jira \
        $RPM_BUILD_ROOT%{l_prefix}/var/jira/licenses \
        $RPM_BUILD_ROOT%{l_prefix}/var/jira/tmp \
        $RPM_BUILD_ROOT%{l_prefix}/var/jira/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/jira/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/jira/webapps \
        $RPM_BUILD_ROOT%{l_prefix}/var/jira/external-source \
        $RPM_BUILD_ROOT%{l_prefix}/var/jira/work \
        $RPM_BUILD_ROOT%{l_prefix}/var/jira/db

    #   install product parts
    cp -r lib bin atlassian-jira tomcat-docs \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/jira/
    cp -r external-source webapps \
        $RPM_BUILD_ROOT%{l_prefix}/var/jira/

    #   install configuration files
    cp -r conf/* \
        $RPM_BUILD_ROOT%{l_prefix}/etc/jira/
    %{l_shtool} subst \
        -e 's;\(<Context path="\)\("\);\1/jira\2;g' \
        $RPM_BUILD_ROOT%{l_prefix}/etc/jira/server.xml
    mv  $RPM_BUILD_ROOT%{l_prefix}/libexec/jira/atlassian-jira/WEB-INF/classes/jira-application.properties \
        $RPM_BUILD_ROOT%{l_prefix}/etc/jira/
    ln -s ../../../../../etc/jira/jira-application.properties \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/jira/atlassian-jira/WEB-INF/classes/jira-application.properties
    %{l_shtool} subst \
        -e 's;^\(jira.home =\).*;\1%{l_prefix}/var/jira/data;g' \
        $RPM_BUILD_ROOT%{l_prefix}/etc/jira/jira-application.properties

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

    #   install setup script
%if "%{with_mysql}" == "yes"
    l_dbtype="mysql"
%endif
%if "%{with_pgsql}" == "yes"
    l_dbtype="pgsql"
%endif
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e "s;@l_dbtype@;$l_dbtype;g" \
        %{SOURCE jira-setup.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/jira-setup

    #   install JDBC driver
%if "%{with_mysql}" == "yes"
    %{l_shtool} install -c -m 644 \
        mysql-connector-java-%{V_jdbc_mysql}/mysql-connector-java-%{V_jdbc_mysql}-bin.jar \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/jira/lib/
%endif
%if "%{with_pgsql}" == "yes"
    %{l_shtool} install -c -m 644 \
        %{SOURCE postgresql-%{V_jdbc_pgsql}.jar} \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/jira/lib/
%endif

    #   create symbolic links
    ln -s ../../etc/jira                 $RPM_BUILD_ROOT%{l_prefix}/libexec/jira/conf
    ln -s ../../var/jira/tmp             $RPM_BUILD_ROOT%{l_prefix}/libexec/jira/temp
    ln -s ../../var/jira/log             $RPM_BUILD_ROOT%{l_prefix}/libexec/jira/logs
    ln -s ../../var/jira/external-source $RPM_BUILD_ROOT%{l_prefix}/libexec/jira/external-source
    ln -s ../../var/jira/webapps         $RPM_BUILD_ROOT%{l_prefix}/libexec/jira/webapps
    ln -s ../../var/jira/licenses        $RPM_BUILD_ROOT%{l_prefix}/libexec/jira/licenses
    ln -s ../../var/jira/work            $RPM_BUILD_ROOT%{l_prefix}/libexec/jira/work

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "1. To complete this installation of Jira please start the RDBMS and"
          echo "   create the Jira database (only required for MySQL and PostgreSQL):"
%if "%{with_mysql}" == "yes"
                echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mysql start"
                echo "    \$ $RPM_INSTALL_PREFIX/sbin/jira-setup install"
%endif
%if "%{with_pgsql}" == "yes"
                echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc postgresql start"
                echo "    \$ $RPM_INSTALL_PREFIX/sbin/jira-setup install"
%endif
          echo ""
          echo "2. Now start Jira by running the command"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc jira start"
          echo "   and connect with a browser to the URL:"
          echo "     http://127.0.0.1:8080/jira/"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} jira status 2>/dev/null`
        [ ".$jira_active" = .yes ] && %{l_rc} jira restart
    fi
    exit 0

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

