##
##  minio.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
%define       V_minio_opkg  2024.02.11
%define       V_minio_snap  20240211

#   package information
Name:         minio
Summary:      Amazon S3 Compatible Object Store
URL:          https://minio.io/
Vendor:       Minio, Inc.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      Apache
Version:      %{V_minio_opkg}
Release:      20240211

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/minio/minio-%{V_minio_snap}.tar.xz
Source1:      http://download.openpkg.org/components/versioned/minio/mc-%{V_minio_snap}.tar.xz
Source2:      minio.cf
Source3:      rc.minio

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, go
PreReq:       OpenPKG, openpkg >= 20160101

%description
    Minio is an object storage server compatible with Amazon S3 cloud
    storage service. Minio follows a minimalist design philosophy. Minio
    is light enough to be bundled with the application stack. Minio
    currently implements two backends: Filesystem (FS) and ErasureCoded
    (XL).

%track
    prog minio = {
        version   = %{V_minio_snap}
        url       = http://download.openpkg.org/components/versioned/minio/
        regex     = minio-(\d+)\.tar\.xz
    }

%prep
    %setup -q -n minio
    %setup -q -n minio -T -D -a 1

%build
    #   build program
    (   export GOPATH=`pwd`
        cd $GOPATH/src/github.com/minio/minio
        go build -v
    ) || exit $?
    (   cd mc
        export GOPATH=`pwd`
        cd $GOPATH/src/github.com/minio/mc
        go build -v
    ) || exit $?

%install
    #   create directory hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/minio \
        $RPM_BUILD_ROOT%{l_prefix}/var/minio/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/minio/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/minio/db/cfg \
        $RPM_BUILD_ROOT%{l_prefix}/var/minio/db/dat

    #   install program
    %{l_shtool} install -c -s -m 755 \
        src/github.com/minio/minio/minio \
        mc/src/github.com/minio/mc/mc \
        $RPM_BUILD_ROOT%{l_prefix}/bin/

    #   install default configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE minio.cf} $RPM_BUILD_ROOT%{l_prefix}/etc/minio/

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

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display information about next steps
        ( echo "For finishing the Minio installation, please configure:"
          echo "    \$ vi $RPM_INSTALL_PREFIX/etc/minio/minio.cf"
          echo "    | MINIO_ACCESS_KEY=\"<access-key>\""
          echo "    | MINIO_SECRET_KEY=\"<secret-key>\""
          echo "Then start Monio with..."
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc minio start"
          echo "...and access the Minio UI service via:"
          echo "    \$ open http://127.0.0.1:9000"
          echo "Additionally, you can use the Minio client:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/mc mc config host add <name> \\%{l_nil}"
          echo "      http://127.0.0.1:9000 <access-key> <secret-key>"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/mc ..."
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} minio status 2>/dev/null`
        [ ".$minio_active" = .yes ] && %{l_rc} minio restart
    fi
    exit 0

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

