##
##  rust.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 1.90.0
%define       V_dist 1.90.0

#   package information
Name:         rust
Summary:      Rust Programming Language
URL:          http://www.rust-lang.org/
Vendor:       Graydon Hoare, Mozilla
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Language
License:      MIT/Apache
Version:      %{V_opkg}
Release:      20250920

#   list of sources
Source0:      https://static.rust-lang.org/dist/rust-%{V_dist}-x86_64-unknown-linux-gnu.tar.xz
Source1:      https://static.rust-lang.org/dist/rust-%{V_dist}-x86_64-unknown-freebsd.tar.xz
Source2:      https://static.rust-lang.org/dist/rust-std-%{V_dist}-wasm32-wasip1.tar.xz
Source3:      https://static.rust-lang.org/dist/rust-std-%{V_dist}-wasm32-wasip2.tar.xz

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

%description
    Rust is a systems programming language that runs blazingly fast,
    prevents segfaults, and guarantees thread safety.

%track
    prog rust = {
        version   = %{version}
        url       = https://forge.rust-lang.org/infra/other-installation-methods.html
        regex     = rust-(\d+\.\d+\.\d+)-x86_64-unknown-freebsd\.tar\.xz
    }

%prep
    %setup -q -T -c
    case "%{l_platform -t}" in
        amd64-linux*   ) tarball="%{SOURCE0}" ;;
        amd64-freebsd* ) tarball="%{SOURCE1}" ;;
        * ) echo "platform \"%{l_platform -t}\" not supported"; exit 1 ;;
    esac
    %{l_xz} -d -c $tarball | %{l_tar} xf -
    mv rust-*/* .
    %{l_xz} -d -c %{SOURCE2} | %{l_tar} xf -
    %{l_xz} -d -c %{SOURCE3} | %{l_tar} xf -

%build

%install
    %{l_bash} ./install.sh \
        --destdir=$RPM_BUILD_ROOT \
        --prefix=%{l_prefix}
    (   cd rust-std-%{V_dist}-wasm32-wasip1
        %{l_bash} ./install.sh \
            --destdir=$RPM_BUILD_ROOT \
            --prefix=%{l_prefix}
    ) || exit $?
    (   cd rust-std-%{V_dist}-wasm32-wasip2
        %{l_bash} ./install.sh \
            --destdir=$RPM_BUILD_ROOT \
            --prefix=%{l_prefix}
    ) || exit $?
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/src
    mv  $RPM_BUILD_ROOT%{l_prefix}/share/man \
        $RPM_BUILD_ROOT%{l_prefix}/man
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/doc
    for prog in cargo cargo-clippy cargo-fmt clippy-driver rust-gdb rust-lldb rustc rustdoc rustfmt; do
        (   echo "#!/bin/sh"
            echo "PATH=%{l_prefix}/bin:\$PATH"
            echo "export PATH"
            echo "LD_LIBRARY_PATH=%{l_prefix}/lib/rustlib:%{l_prefix}/lib:/usr/lib:/lib"
            echo "export LD_LIBRARY_PATH"
            echo "SSL_CERT_FILE=%{l_prefix}/etc/x509/public-ca.crt.pem"
            echo "export SSL_CERT_FILE"
            echo "exec %{l_prefix}/bin/$prog.exe \${1+\"\$@\"}"
        ) >$prog.sh
        mv  $RPM_BUILD_ROOT%{l_prefix}/bin/$prog \
            $RPM_BUILD_ROOT%{l_prefix}/bin/$prog.exe
        %{l_shtool} install -c -m 755 \
            $prog.sh $RPM_BUILD_ROOT%{l_prefix}/bin/$prog
    done
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std}

%files -f files

%clean

