##
##  sftpgo.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  2.6.6
%define       V_dist  20250227

#   package information
Name:         sftpgo
Summary:      SSH/SFTP and HTTPS/WebDAV Server
URL:          https://github.com/drakkan/sftpgo
Vendor:       Nicola Murino
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      GPL
Version:      %{V_opkg}.%{V_dist}
Release:      20250227

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/sftpgo/sftpgo-%{V_dist}.tar.xz
Source1:      rc.sftpgo
Source2:      sftpgo.yaml
Source3:      sftpgo.passwd
Source4:      sftpgo-cred.sh

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

%description
    SFTPgo is a combined SSH/SFTP and HTTPS/WebDAV server.

%track
    prog sftpgo:release = {
        version   = %{V_opkg}
        url       = https://github.com/drakkan/sftpgo/releases
        regex     = v(__VER__)\.tar\.gz
    }
    prog sftpgo:snapshot = {
        version   = %{V_dist}
        url       = http://download.openpkg.org/components/versioned/sftpgo/
        regex     = sftpgo-(__VER__)\.tar\.xz
    }

%prep
    %setup -q -n sftpgo

%build
    #   build program
    export GOPATH=`pwd`
    cd src/github.com/drakkan/sftpgo/
    go build -v \
        -ldflags "-X github.com/drakkan/sftpgo/version.commit=000000 \
            -X github.com/drakkan/sftpgo/version.date=`date -u '+%FT%TZ'`" \
        -o sftpgo

%install
    #   install program
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/share/sftpgo
    %{l_shtool} install -c -s -m 755 \
        src/github.com/drakkan/sftpgo/sftpgo \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/
    cp -rp src/github.com/drakkan/sftpgo/templates \
        $RPM_BUILD_ROOT%{l_prefix}/share/sftpgo/
    cp -rp src/github.com/drakkan/sftpgo/static \
        $RPM_BUILD_ROOT%{l_prefix}/share/sftpgo/

    #   install default configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/sftpgo \
        $RPM_BUILD_ROOT%{l_prefix}/etc/sftpgo/credentials
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE sftpgo.yaml} $RPM_BUILD_ROOT%{l_prefix}/etc/sftpgo/
    %{l_shtool} install -c -m 644 \
        %{SOURCE sftpgo.passwd} $RPM_BUILD_ROOT%{l_prefix}/etc/sftpgo/

    #   install utility program
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e 's;@l_bash@;%{l_bash};g' \
        %{SOURCE sftpgo-cred.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/sftpgo-cred

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

    #   create run-time directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/sftpgo/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/sftpgo/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/sftpgo/data \
        $RPM_BUILD_ROOT%{l_prefix}/var/sftpgo/backup

    #   provide sample data
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/sftpgo/data/example
    echo "Example" \
        >$RPM_BUILD_ROOT%{l_prefix}/var/sftpgo/data/example/example.txt

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   initialize database
        echo "Initializing SFTPgo database" | %{l_rpmtool} msg -b -t notice
        su - %{l_rusr} -c "$RPM_INSTALL_PREFIX/sbin/sftpgo initprovider \
            --config-dir $RPM_INSTALL_PREFIX/etc/sftpgo --config-file sftpgo.yaml >/dev/null 2>&1"

        #   initialize keys
        su - %{l_rusr} -c "$RPM_INSTALL_PREFIX/sbin/sftpgo-cred localhost 127.0.0.1"
        (   echo "SFTPgo was configured with standard SSH/WebDAV credentials"
            echo "for \"localhost\" and \"127.0.0.1\". For production use, you"
            echo "usually let SFTPgo listen on an external IP address. For this the"
            echo "credentials have to regenerated with:"
            echo "    \$ $RPM_INSTALL_PREFIX/sbin/sftpgo-cred www.example.com 192.168.0.1"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} sftpgo status 2>/dev/null`
        [ ".$sftpgo_active" = .yes ] && %{l_rc} sftpgo restart
    fi
    exit 0

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

