##
##  mattermost.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:         mattermost
Summary:      Web Collaboration Platform
URL:          http://mattermost.org/
Vendor:       Mattermost, Inc.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        CMS
License:      Apache
Version:      7.0.0
Release:      20220616

#   list of sources
Source0:      https://github.com/mattermost/mattermost-server/archive/v%{version}.tar.gz
Source1:      https://releases.mattermost.com/%{version}/mattermost-%{version}-linux-amd64.tar.gz
Source2:      rc.mattermost
Source3:      mattermost.sh
Source4:      mattermost-setup.sh
Source5:      mattermost-post.sh
Patch0:       mattermost.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20140101, go
PreReq:       OpenPKG, openpkg >= 20140101, postgresql, curl

%description
    Mattermost is a flexible instant messaging platform that meets even
    the most demanding privacy and security standards.

%track
    prog mattermost = {
        version   = %{version}
        url       = https://mattermost.com/download/
        regex     = mattermost-(\d+\.\d+(\.\d+)?)
    }

%prep
    %setup -q -c
    %setup -q -T -D -a 1
    %patch -p0

%build
    #   build server
    export GOPATH=`pwd`
    mkdir -p src/github.com/mattermost/
    ln -s `pwd`/mattermost-server-%{version} src/github.com/mattermost/mattermost-server
    (   cd src/github.com/mattermost/mattermost-server
        %{l_make} %{l_mflags} build BUILD_NUMBER=%{version}
    ) || exit $?

    #   strip down regular distribution
    rm -f mattermost/client/*.js.map
    rm -f mattermost/client/*.css.map

%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/mattermost/plugins \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/mattermost \
        $RPM_BUILD_ROOT%{l_prefix}/lib/mattermost \
        $RPM_BUILD_ROOT%{l_prefix}/var/mattermost/data-db \
        $RPM_BUILD_ROOT%{l_prefix}/var/mattermost/data-fs \
        $RPM_BUILD_ROOT%{l_prefix}/var/mattermost/data-plugins \
        $RPM_BUILD_ROOT%{l_prefix}/var/mattermost/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/mattermost/log

    #   install programs
    %{l_shtool} install -c -m 755 \
        mattermost-server-%{version}/mattermost \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/mattermost/mattermost

    #   install wrapper scripts
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE mattermost.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/mattermost
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE mattermost-post.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/mattermost-post

    #   install configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        mattermost/config/config.json \
        $RPM_BUILD_ROOT%{l_prefix}/etc/mattermost/mattermost.json

    #   install regular distribution parts
    cp -rp \
        mattermost/client \
        mattermost/fonts \
        mattermost/i18n \
        mattermost/prepackaged_plugins \
        mattermost/templates \
        $RPM_BUILD_ROOT%{l_prefix}/lib/mattermost/

    #   provide directory symlinks
    ln -s ../../etc/mattermost \
        $RPM_BUILD_ROOT%{l_prefix}/lib/mattermost/config
    ln -s ../../var/mattermost/log \
        $RPM_BUILD_ROOT%{l_prefix}/lib/mattermost/logs
    ln -s ../../var/mattermost/data-plugins \
        $RPM_BUILD_ROOT%{l_prefix}/lib/mattermost/plugins

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

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

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "1. To complete this installation of Mattermost please start PostgreSQL"
          echo "   and initialize the Mattermost database like this:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc postgresql start"
          echo "     \$ $RPM_INSTALL_PREFIX/sbin/mattermost-setup install"
          echo ""
          echo "2. After this post-installation, start Mattermost by running"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mattermost start"
          echo "   and initialize the Mattermost database by connecting to:"
          echo "     http://127.0.0.1:8080/"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} mattermost status 2>/dev/null`
        [ ".$mattermost_active" = .yes ] && %{l_rc} mattermost restart
    fi
    exit 0

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

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

        #   remove run-time files
        rm -rf $RPM_INSTALL_PREFIX/var/mattermost/data-db/*      >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/mattermost/data-fs/*      >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/mattermost/data-plugins/* >/dev/null 2>&1 || true
        rm -f  $RPM_INSTALL_PREFIX/var/mattermost/log/*          >/dev/null 2>&1 || true
        rm -f  $RPM_INSTALL_PREFIX/var/mattermost/run/*          >/dev/null 2>&1 || true
    fi
    exit 0

