#   ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
#   File: shadowsocks.spec
#   Copyright 🄯 2024, 2025 Van de Bugger.
#   SPDX-License-Identifier: FSFAP
#   ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――

Source1000:         vdb.lua
%include            %{S:1000}

%global crate       shadowsocks-rust

Name:               shadowsocks
Version:            1.22.0
Release:            0.vdb.2%{dist}
Summary:            A fast tunnel proxy that helps you bypass firewalls
License:            MIT
URL:                https://crates.io/crates/shadowsocks-rust
Source0:            %{crates_source}
Source1:            %{name}-local@.service
Source2:            %{name}-sysusers.conf

BuildRequires:      cargo-rpm-macros >= 24
BuildRequires:      systemd-rpm-macros
BuildRequires:      cargo
BuildRequires:      rust >= 1.81
BuildRequires:      %{__perl}

%global common_descr %{text -- \
    Shadowsocks is a secure *split* proxy loosely based on SOCKS5. It consists of two components,
    local and remote:

    client <---> local <--[encrypted]--> remote <---> target

    The Shadowsocks local component acts like a traditional SOCKS5 server and provides proxy
    service to clients. It encrypts and forwards data streams and packets from the client to the
    Shadowsocks remote component, which decrypts and forwards to the target. Replies from target
    are similarly encrypted and relayed by remote back to local, which decrypts and eventually
    returns to the original client.
}

%description
%{text -- \
    %{common_descr}

    This package provides `ssmanager' and `ssurl` binaries.
}

%package local
Summary:            Shadowsocks local component

%description local
%{text -- \
    %{common_descr}

    This package provides `sslocal' binary, a local component of the shadowsocks split proxy
    server, and accompanying systemd service file and sample configuration file.
}

%package server
Summary:            Shadowsocks remote component

%description server
%{text -- \
    %{common_descr}

    This package provides `ssserver' binary, a remote component of the shadowsocks split proxy
    server.
}

%package service
Summary:            Shadowsocks universal binary

%description service
%{text -- \
    %{common_descr}

    This package provides shadowsocks all-in-one binary `ssservice', it can works as local, server
    (aka remote), manager, and genkey.

    Note: It is *not* shadowsocks systemd service.
}

%prep
%autosetup -n %{crate}-%{version} -p1
%cargo_prep
%{__mv} .cargo/config.toml .cargo/config.toml.orig
%{__perl} -p \
    -e 'BEGIN { $/ = undef; }; ' \
    -e 's{^(\[net\]\noffline\h*=\h*)true(\n)}{$1false$2}m;' \
    -e 's{^\[source\.crates-io\]\nregistry\h*=\h*"https://crates\.io"\h*\nreplace-with\h*=\h*"local-registry"\n}{}m; ' \
    .cargo/config.toml.orig > .cargo/config.toml
%{__cargo} fetch

%build
%cargo_build

%install
# Some .rs files have executable bit set. Some of such files starts with "#![..." construct.
# rpmbuild treats them as scripts and complains shebang is not absolute.
# Let's clear executable bit from the rust sources.
find . -type f -name '*.rs' -print0 | xargs -0 chmod ugo-x
%{cargo_install}
%{__rm} -rf %{buildroot}%{_datadir}/cargo
%{__mkdir_p} %{buildroot}/%{_pkgconfdir}/local
%{__mkdir_p} %{buildroot}/%{_pkgconfdir}/server
%{_install_D} -m u=rw,go=r LICENSE %{buildroot}/%{_pkglicdir}/LICENSE
%{_install_D} -m ugo=r     %{S:1}  %{buildroot}/%{_unitdir}/%{name}-local@.service
%{_install_D} -m u=rw,go=r %{S:2}  %{buildroot}/%{_sysusersdir}/%{name}.conf

%if %{with check}
%check
%cargo_test
%endif

%files
%license LICENSE
                                    %{_bindir}/ssmanager
                                    %{_bindir}/ssurl

%files local
%dir                                %{_pkglicdir}
                                    %{_pkglicdir}/LICENSE
                                    %{_bindir}/sslocal
                                    %{_sysusersdir}/%{name}.conf
                                    %{_unitdir}/shadowsocks-local@.service
%dir %attr( 0750, root, %{name} )   %{_pkgconfdir}
%dir %attr( 0750, root, %{name} )   %{_pkgconfdir}/local

%files server
%dir                                %{_pkglicdir}
                                    %{_pkglicdir}/LICENSE
                                    %{_bindir}/ssserver
%dir %attr( 0750, root, %{name} )   %{_pkgconfdir}
%dir %attr( 0750, root, %{name} )   %{_pkgconfdir}/server

%files service
%dir                                %{_pkglicdir}
                                    %{_pkglicdir}/LICENSE
                                    %{_bindir}/ssservice
%dir %attr( 0750, root, %{name} )   %{_pkgconfdir}
%dir %attr( 0750, root, %{name} )   %{_pkgconfdir}/server
%dir %attr( 0750, root, %{name} )   %{_pkgconfdir}/local

%changelog

* Tue Mar 18 2025 Van de Bugger - 1.22.0-0.vdb.2
- Minor changes in spec

* Thu Feb 20 2025 Van de Bugger - 1.22.0-0.vdb.1
- Shadowsocks v1.22.0
- sslocal, ssserver and ssservice moved to a dedicated packages
- Added systemd service shadowsocks-local

* Mon Sep 23 2024 Van de Bugger - 1.21.0-0.vdb.1
- Initial release