##
##  samba.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
Name:         samba
Summary:      SMB/CIFS Server
URL:          http://www.samba.org/
Vendor:       Andrew Tridgell
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Filesystem
License:      GPL
Version:      3.6.25
Release:      20150808

#   package options
%option       with_pam   no
%option       with_swat  no
%option       with_acl   no
%option       with_ldap  no
%option       with_ads   no

#   option sanity
%if "%{with_ads}" == "yes"
%undefine     with_ldap
%define       with_ldap  yes
%endif

#   list of sources
Source0:      http://us1.samba.org/samba/ftp/samba-%{version}.tar.gz
Source1:      smb.conf
Source2:      smb.hosts
Source3:      rc.samba
Patch0:       samba.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  openssl, popt, perl
PreReq:       openssl, popt, perl
%if "%{with_pam}" == "yes"
BuildPreReq:  PAM
PreReq:       PAM
%endif
%if "%{with_ldap}" == "yes"
BuildPreReq:  openldap
PreReq:       openldap
%endif
%if "%{with_ads}" == "yes"
BuildPreReq:  KERBEROS
PreReq:       KERBEROS
%endif

%description
    Samba is an open source software suite that provides seamless file
    and print services to SMB/CIFS clients plus name resolution services
    to NetBIOS clients. The Samba software suite is a collection of
    programs that implements the Server Message Block (SMB) protocol
    for UNIX systems. This protocol is sometimes also referred to as
    the Common Internet File System (CIFS) and is the network protocol
    which provides filesharing and printing services to MSCLIENT 3.0 for
    DOS, Windows for Workgroups (LanManager), Windows 95/98/ME, Windows
    NT/2000/XP/2003, OS/2, MacOS DAVE and Linux smbfs clients.

%track
    prog samba = {
        version   = %{version}
        url       = http://download.samba.org/samba/ftp/
        regex     = samba-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0
    %{l_shtool} subst \
        -e '/WINBINDD_SOCKET_DIR/s;/tmp/\.winbindd;%{l_prefix}/var/samba/tmp/.winbindd;' \
        nsswitch/winbind_struct_protocol.h
    perl -e '$txt = join("", <STDIN>); $txt =~ s/(\$\(LIBS\))((?:[^\n]*\\\n)*[^\n]*)(\n)/$2 $1$3/sg; print STDOUT $txt;' \
        <source3/Makefile.in >source3/Makefile.in.new && mv source3/Makefile.in.new source3/Makefile.in

%build
    cd source3
    CC="%{l_cc}"
    CFLAGS="%{l_cflags -O}"
    CPPFLAGS="-I`pwd`/iniparser/src %{l_cppflags openssl} -DOPENSSL_DISABLE_OLD_DES_SUPPORT"
    LDFLAGS="%{l_ldflags}"
    LIBS=""
%if "%{with_pam}" == "yes"
    CPPFLAGS="$CPPFLAGS -I`%{l_rc} --query pam_incdir`"
    LDFLAGS="$LDFLAGS -L`%{l_rc} --query pam_libdir`"
%endif
%if "%{with_ldap}" == "yes"
    LIBS="$LIBS -lssl -lcrypto"
%endif
    options=""
    case "%{l_platform -t}" in
        *-linux2.[46]* | *-linux3.* ) options="--with-smbmount" ;;
    esac
    export CC
    export CFLAGS
    export CPPFLAGS
    export LDFLAGS
    export LIBS
    ./configure \
        --cache-file=./config.cache \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --libexecdir=%{l_prefix}/libexec/samba \
        --localstatedir=%{l_prefix}/var/samba/run \
        --sysconfdir=%{l_prefix}/etc/samba \
        --libdir=%{l_prefix}/lib/samba \
        --with-privatedir=%{l_prefix}/etc/samba \
        --with-configdir=%{l_prefix}/etc/samba \
        --with-lockdir=%{l_prefix}/var/samba/run/locks \
        --with-piddir=%{l_prefix}/var/samba/run \
        --with-privatedir=%{l_prefix}/var/samba/run/private \
        --with-swatdir=%{l_prefix}/share/samba \
        --disable-shared-libs \
        --with-static-libs=libtalloc,libtdb,libnetapi,libaddns,libsmbclient,libsmbsharemodes,libwbclient \
        --without-included-popt \
        --without-libtalloc \
        --without-libtdb \
        --without-libnetapi \
        --without-libaddns \
        --without-libsmbclient \
        --without-libsmbsharemodes \
        --with-winbind \
%if "%{with_acl}" == "yes"
        --with-acl-support \
%else
        --without-acl-support \
%endif
%if "%{with_pam}" == "yes"
        --with-pam \
%else
        --without-pam \
%endif
%if "%{with_ldap}" == "yes"
        --with-ldap \
%else
        --without-ldap \
%endif
%if "%{with_ads}" == "yes"
        --with-ads \
        --with-krb5=%{l_prefix} \
%else
        --without-ads \
%endif
        --without-wbclient \
        --disable-dnssd \
        --disable-fam \
        --disable-avahi \
        --disable-pie \
        --disable-cups \
        $options
    %{l_make} %{l_mflags}

%install
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/samba \
        $RPM_BUILD_ROOT%{l_prefix}/var/samba/spool \
        $RPM_BUILD_ROOT%{l_prefix}/var/samba/netlogon \
        $RPM_BUILD_ROOT%{l_prefix}/var/samba/tmp \
        $RPM_BUILD_ROOT%{l_prefix}/var/samba/profiles
    ( cd source3
      %{l_make} %{l_mflags} \
          DESTDIR=$RPM_BUILD_ROOT \
          LIBDIR=%{l_prefix}/lib/samba \
          installbin installservers installman installscripts installdat installmodules
%if "%{with_swat}" == "yes"
      %{l_make} %{l_mflags} installswat DESTDIR=$RPM_BUILD_ROOT
%endif
    ) || exit $?
    l_hostname=`%{l_shtool} echo -e %h`
    l_domainname=`%{l_shtool} echo -e %d`
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        -e "s;@l_hostname@;$l_hostname;g" \
        -e "s;@l_domainname@;$l_domainname;g" \
        %{SOURCE smb.conf} \
        %{SOURCE smb.hosts} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/samba/
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.samba} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    strip $RPM_BUILD_ROOT%{l_prefix}/sbin/* >/dev/null 2>&1 || true
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/samba/*' \
        '%attr(1777,%{l_musr},%{l_mgrp}) %{l_prefix}/var/samba/spool' \
        '%attr(1777,%{l_musr},%{l_mgrp}) %{l_prefix}/var/samba/profiles'

%files -f files

%clean

%pre
    #   before upgrade, save status and stop service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} samba status 2>/dev/null | tee %{l_tmpfile}`
    %{l_rc} samba stop 2>/dev/null
    exit 0

%post
%if "%{with_pam}" == "yes"
    if [ $1 -eq 1 ]; then
        #   after install, add PAM configuration entry
        $RPM_INSTALL_PREFIX/sbin/pamtool --add --smart --name=smbd
    fi
%endif
    if [ $1 -eq 2 ]; then
        #   after upgrade, restore status
        { eval `cat %{l_tmpfile}`; rm -f %{l_tmpfile}; true; } >/dev/null 2>&1
        [ ".$samba_active" = .yes ] && %{l_rc} samba start
    fi
    exit 0

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} samba stop 2>/dev/null
    rm -f $RPM_INSTALL_PREFIX/var/samba/run/log.[sn]mbd* >/dev/null 2>&1 || true
%if "%{with_pam}" == "yes"
    #   remove PAM configuration entry
    $RPM_INSTALL_PREFIX/sbin/pamtool --remove --smart --name=smbd
%endif
    exit 0

