##
##  tor.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_dist  0.4.8.19
%define       V_opkg  0.4.8.19

#   package information
Name:         tor
Summary:      Anonymous Internet Communication System
URL:          http://www.torproject.org/
Vendor:       R. Dingledine & N. Mathewson
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Networking
License:      LGPL
Version:      %{V_opkg}
Release:      20251008

#   list of sources
Source0:      https://dist.torproject.org/tor-%{V_dist}.tar.gz
Source1:      rc.tor
Source2:      torrc

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc
PreReq:       OpenPKG, openpkg >= 20160101, tsocks
BuildPreReq:  openssl-threads, libevent, zlib
PreReq:       openssl-threads, libevent, zlib

%description
   Tor is a toolset for a wide range of organizations and people
   that want to improve their safety and security on the Internet.
   Using Tor can help you anonymize web browsing and publishing,
   instant messaging, IRC, SSH, and other applications that use the TCP
   protocol. Tor also provides a platform on which software developers
   can build new applications with built-in anonymity, safety, and
   privacy features. Your traffic is safer when you use Tor, because
   communications are bounced around a distributed network of servers,
   called onion routers. Instead of taking a direct route from source to
   destination, data packets on the Tor network take a random pathway
   through several servers that cover your tracks so no observer at any
   single point can tell where the data came from or where it's going.

%track
    prog tor = {
        version   = %{V_dist}
        url       = https://dist.torproject.org/
        regex     = tor-(\d+\.\d+(?:\.\d+)*)\.tar\.gz
    }

%prep
    %setup -q -n tor-%{V_dist}

%build
    libs=""
    case "%{l_platform -t}" in
        *-linux* ) libs="$libs -lrt" ;;
    esac
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags openssl-threads .}" \
    LDFLAGS="%{l_ldflags openssl-threads .}" \
    LIBS="$libs" \
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --with-ssl-dir=%{l_prefix}/lib/openssl-threads \
        --with-libevent-dir=%{l_prefix} \
        --disable-asciidoc
    %{l_make} %{l_mflags -O}

%install
    %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    mv $RPM_BUILD_ROOT%{l_prefix}/etc/tor/torrc.sample \
       $RPM_BUILD_ROOT%{l_prefix}/etc/tor/torrc
    %{l_shtool} subst \
        -e 's;exec tsocks;exec %{l_prefix}/bin/tsocks;' \
        $RPM_BUILD_ROOT%{l_prefix}/bin/torify
    %{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.tor} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE torrc} $RPM_BUILD_ROOT%{l_prefix}/etc/tor/
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/tor/db
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/tor/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/tor' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/tor/db'

%files -f files

%clean

%post
    #   after upgrade, restart service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} tor status 2>/dev/null`
    [ ".$tor_active" = .yes ] && %{l_rc} tor restart
    exit 0

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} tor stop 2>/dev/null
    rm -f $RPM_INSTALL_PREFIX/var/tor/tor.* >/dev/null 2>&1 || true
    rm -f $RPM_INSTALL_PREFIX/var/tor/db/*  >/dev/null 2>&1 || true
    exit 0

