##
##  youtrack.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_opkg 2018.2.44329
%define       V_dist 2018.2.44329

#   package information
Name:         youtrack
Summary:      Agile Issue Tracking
URL:          http://www.jetbrains.com/youtrack/
Vendor:       JetBrains
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Ticketing
License:      Commercial
Version:      %{V_opkg}
Release:      20180804

#   list of sources
Source0:      http://download.jetbrains.com/charisma/youtrack-%{V_dist}.zip
Source1:      rc.youtrack

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

%description
    YouTrack is a Web-based bug tracker and issue tracking system
    developed by JetBrains. It focuses on query-based issue search with
    auto-completion, manipulating issues in batches, customizing the set
    of issue attributes and creating custom workflows, and implements a
    keyboard-centric approach.

%track
    prog youtrack = {
        version   = %{V_dist}
        url       = http://www.jetbrains.com/youtrack/download/get_youtrack.html
        regex     = youtrack-(__VER__)\.war
    }

%prep
    %setup -q

%build
    #   patch application
    %{l_shtool} subst \
        -e 's;${user.home}/teamsysdata-backup;%{l_prefix}/var/youtrack/db.bak;g' \
        -e 's;${user.home}/teamsysdata;%{l_prefix}/var/youtrack/db;g' \
        apps/youtrack/web/WEB-INF/web.xml

    #   remove useless content
    rm -f bin/youtrack.bat
    rm -rf internal/java

%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/youtrack \
        $RPM_BUILD_ROOT%{l_prefix}/lib/youtrack \
        $RPM_BUILD_ROOT%{l_prefix}/var/youtrack/db.bak \
        $RPM_BUILD_ROOT%{l_prefix}/var/youtrack/db \
        $RPM_BUILD_ROOT%{l_prefix}/var/youtrack/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/youtrack/log

    #   install application into application server area
    rm -rf logs temp data backups
    cp -rp * $RPM_BUILD_ROOT%{l_prefix}/lib/youtrack/

    #   relocate configuration data locations
    mv  $RPM_BUILD_ROOT%{l_prefix}/lib/youtrack/conf/* \
        $RPM_BUILD_ROOT%{l_prefix}/etc/youtrack/
    rmdir $RPM_BUILD_ROOT%{l_prefix}/lib/youtrack/conf
    ln -s ../../etc/youtrack $RPM_BUILD_ROOT%{l_prefix}/lib/youtrack/conf

    #   relocate variable data locations
    ln -s ../../var/youtrack/db     $RPM_BUILD_ROOT%{l_prefix}/lib/youtrack/data
    ln -s ../../var/youtrack/db.bak $RPM_BUILD_ROOT%{l_prefix}/lib/youtrack/backups
    ln -s ../../var/youtrack/run    $RPM_BUILD_ROOT%{l_prefix}/lib/youtrack/temp
    ln -s ../../var/youtrack/log    $RPM_BUILD_ROOT%{l_prefix}/lib/youtrack/logs

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

%files -f files

%clean

%post
    #   after upgrade, restart application server
    if [ $1 -eq 2 ]; then
        eval `%{l_rc} youtrack status 2>/dev/null`
        [ ".$youtrack_active" = .yes ] && %{l_rc} youtrack restart
    fi
    exit 0

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

