##
##  @l_prefix@/etc/openpkg/rpmmacros -- OpenPKG RPM macros for @l_prefix@ hierarchy
##  Copyright (c) 2000-2003 Cable & Wireless Deutschland GmbH
##  Copyright (c) 2000-2003 The OpenPKG Project <http://www.openpkg.org/>
##  Copyright (c) 2000-2003 Ralf S. Engelschall <rse@engelschall.com>
##
##  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.
##
##  The purpose of this macro set is to override the default
##  macros in the OpenPKG RPM installation for the @l_prefix@ hierarchy.
##

#   the platform identification
%l_target                %{_target}
%l_location              @LOC@

#   the *S*uper-user user/group name/id pair
%l_susr                  @SUSR@
%l_sgrp                  @SGRP@
%l_suid                  %((cat /etc/passwd; ypcat passwd) 2>/dev/null | grep "^%{l_susr}:" | head -1 | awk -F: '{ print $3; }')
%l_sgid                  %((cat /etc/group;  ypcat group)  2>/dev/null | grep "^%{l_sgrp}:" | head -1 | awk -F: '{ print $3; }')

#   the *M*anagement user/group name/id pair
%l_musr                  @MUSR@
%l_mgrp                  @MGRP@
%l_muid                  %((cat /etc/passwd; ypcat passwd) 2>/dev/null | grep "^%{l_musr}:" | head -1 | awk -F: '{ print $3; }')
%l_mgid                  %((cat /etc/group;  ypcat group)  2>/dev/null | grep "^%{l_mgrp}:" | head -1 | awk -F: '{ print $3; }')

#   the *R*restricted user/group name/id pair
%l_rusr                  @RUSR@
%l_rgrp                  @RGRP@
%l_ruid                  %((cat /etc/passwd; ypcat passwd) 2>/dev/null | grep "^%{l_rusr}:" | head -1 | awk -F: '{ print $3; }')
%l_rgid                  %((cat /etc/group;  ypcat group)  2>/dev/null | grep "^%{l_rgrp}:" | head -1 | awk -F: '{ print $3; }')

#   the *N*on-priviledged user/group name/id pair
%l_nusr                  @NUSR@
%l_ngrp                  @NGRP@
%l_nuid                  %((cat /etc/passwd; ypcat passwd) 2>/dev/null | grep "^%{l_nusr}:" | head -1 | awk -F: '{ print $3; }')
%l_ngid                  %((cat /etc/group;  ypcat group)  2>/dev/null | grep "^%{l_ngrp}:" | head -1 | awk -F: '{ print $3; }')

#   backward compatibility (openpkg < 20020204)
%l_fsusr                 %{l_musr}
%l_fsgrp                 %{l_mgrp}
%l_npuid                 %{l_nuid}
%l_npgid                 %{l_ngid}

#   the two root directories
%l_root_install          @l_prefix@
%l_root_rpm              @l_prefix@/RPM

#   define the installation hierarchy
%l_prefix                %{l_root_install}
%l_exec_prefix           %{l_root_install}
%l_bindir                %{l_root_install}/bin
%l_sbindir               %{l_root_install}/sbin
%l_libexecdir            %{l_root_install}/libexec
%l_datadir               %{l_root_install}/share
%l_sysconfdir            %{l_root_install}/etc
%l_sharedstatedir        %{l_root_install}/com
%l_localstatedir         %{l_root_install}/var
%l_libdir                %{l_root_install}/lib
%l_includedir            %{l_root_install}/include
%l_infodir               %{l_root_install}/info
%l_mandir                %{l_root_install}/man
%l_docdir                %{l_root_install}/doc

#   override the internal RPM hierarchy variables
%_usr                    %{l_root_rpm}
%_var                    %{l_root_rpm}
%_usrsrc                 %{l_root_rpm}
%_topdir                 %{l_root_rpm}
%_dbpath                 %{l_root_rpm}/DB
%_builddir               %{l_root_rpm}/TMP
%_tmppath                %{l_root_rpm}/TMP
%_sourcedir              %{l_root_rpm}/SRC/%{name}
%_specdir                %{l_root_rpm}/SRC/%{name}
%_rpmdir                 %{l_root_rpm}/PKG
%_srcrpmdir              %{l_root_rpm}/PKG
%_defaultdocdir          %{l_docdir}

#   complement the %{SOURCE<n>} and %{PATCH<n>} with %{SOURCE <file>} and %{PATCH <file>}
%SOURCE()                %(f="%1"; if [ -f "%{_specdir}/$f" ]; then echo "%{_specdir}/$f"; else echo "%{_sourcedir}/$f"; fi)
%PATCH()                 %(f="%1"; if [ -f "%{_specdir}/$f" ]; then echo "%{_specdir}/$f"; else echo "%{_sourcedir}/$f"; fi)

#   override RPM's absolute tool paths with relative ones
#   (because if you upgrade RPM it would hard-code tools in own hierarchy, too)
%__awk                   awk
%__bzip2                 %{_bzip2bin}
%__cat                   cat
%__chgrp                 chgrp
%__chmod                 chmod
%__chown                 chown
%__cp                    cp
%__cpio                  cpio
%__grep                  grep
%__gzip                  %{_gzipbin}
%__id                    id
%__install               install
%__ln_s                  ln -s
%__make                  make
%__mkdir                 mkdir
%__mkdir_p               %{l_prefix}/lib/openpkg/shtool mkdir -p
%__mv                    mv
%__patch                 %{_patchbin}
%__perl                  perl
%__pgp                   %{_pgpbin}
%__rm                    rm
%__rsh                   rsh
%__sed                   sed
%__ssh                   ssh
%__tar                   %{_tarbin}

#   provide fixed paths to tools (standard)
%_gzipbin                %{l_prefix}/lib/openpkg/gzip
%_bzip2bin               %{l_prefix}/lib/openpkg/bzip2
%_patchbin               %{l_prefix}/lib/openpkg/patch
%_tarbin                 %{l_prefix}/lib/openpkg/tar

#   provide fixed paths to tools (additional)
%l_rpmtool               %{l_prefix}/lib/openpkg/rpmtool
%l_shtool                %{l_prefix}/lib/openpkg/shtool
%l_curl                  %{l_prefix}/lib/openpkg/curl
%l_bash                  %{l_prefix}/lib/openpkg/bash

#   provide smart paths to tools
%l_tool_locate()         %(if [ ".%{expand:%%{?with_%1}}" != . ]; then tool="%{expand:%%{with_%1}}"; elif [ -f "%{l_bindir}/%1" ]; then tool="%{l_bindir}/%1"; elif [ -f "%{l_prefix}/lib/openpkg/%1" ]; then tool="%{l_prefix}/lib/openpkg/%1"; else tool="%1"; fi; echo $tool)
%l_gzip                  %{l_tool_locate gzip}
%l_bzip2                 %{l_tool_locate bzip2}
%l_tar                   %{l_tool_locate tar}
%l_cc                    %{l_tool_locate cc}
%l_cxx                   %{l_tool_locate c++}
%l_make                  %{l_tool_locate make}
%l_patch                 %{l_tool_locate patch}
%l_sed                   %{l_tool_locate sed}
%l_grep                  %{l_tool_locate grep}
%l_awk                   %{l_tool_locate awk}
%l_autoconf              %{l_tool_locate autoconf}
%l_automake              %{l_tool_locate automake}
%l_libtool               %{l_tool_locate libtool}
%l_flex                  %{l_tool_locate flex}
%l_bison                 %{l_tool_locate bison}
%l_perl                  %{l_tool_locate perl}
%l_xmkmf                 %{l_tool_locate xmkmf}

#   tool options
%l_mflags(O)             %(%{l_sane_env}; %{l_rpmtool} flags -m %{-O:-O} %{l_make})
%l_cflags(O)             %(%{l_sane_env}; %{l_rpmtool} flags -c %{-O:-O} %{l_cc})
%l_cxxflags(O)           %(%{l_sane_env}; %{l_rpmtool} flags -c %{-O:-O} %{l_cxx})
%l_cppflags()            %(flags="-I%{l_prefix}/include"; for pkg in . %*; do test ".$pkg" = .. && continue; flags="$flags -I%{l_prefix}/include/$pkg"; done; echo $flags)
%l_ldflags()             %(flags="-L%{l_prefix}/lib"; for pkg in . %*; do test ".$pkg" = .. && continue; flags="$flags -L%{l_prefix}/lib/$pkg"; done; echo $flags)

#   determine the current username
%l_whoami                %((id -un) 2>/dev/null || (whoami) 2>/dev/null || (who am i | cut "-d " -f1) 2>/dev/null || echo $LOGNAME)

#   macros for dynamically generating a %files list
%l_files_defattr         '%defattr(-,%{l_musr},%{l_mgrp})'
%l_files_all             '%{l_prefix}'
%l_files_noshared        '%not %dir {%{l_prefix},%{l_prefix}/*,%{l_prefix}/etc/rc.d,%{l_prefix}/man/*}'
%l_files_std(o:g:)       %l_files_defattr %l_files_all %l_files_noshared

#   path to local build root
%l_buildroot             %{_tmppath}/%{name}-%{version}-root

#   executable path for %post/%pre
%_install_script_path    @l_prefix@/bin:@l_prefix@/sbin:/bin:/sbin:/usr/bin:/usr/sbin

#   override the name scheme for RPM files
%_rpmfilename            %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}-%%{OS}-@LOC@.rpm

#   override the payload compression for the CPIO parts of an RPM 
#   (we use the equivalent of "bzip2 -9")
%_source_payload         w9.bzdio
%_binary_payload         w9.bzdio

#   package signature type is OpenPGP via GnuPG
%_signature              gpg

#   two macro-processor add-ons (should be part of forthcoming RPM releases)
%ifdef()                 %if %{expand:%%{?%{1}:1}%%{!?%{1}:0}}
%ifndef()                %if %{expand:%%{?%{1}:0}%%{!?%{1}:1}}

#   macro for package option configuration
%option()                %{expand:%%{!?%{1}:%%global %*}} \
Provides: %{name}::%{1} = %(echo '%{expand:%%{%{1}}}' | sed -e 's;%%;%%%%;g' -e 's; ;%%20;g' -e 's;	;%%09;g')

#   macro-processor add-on for testing external conditions
%l_test(pfd)             %(if [ ".%{?-p:yes}" = .yes ]; then %{l_prefix}/bin/rpm -q %1 >/dev/null 2>&1; rc=$?; elif [ ".%{?-f:yes}" = .yes ]; then test -f %1; rc=$?; elif [ ".%{?-d:yes}" = .yes ]; then test -d %1; rc=$?; fi; if [ ".$rc" = .0 ]; then echo "yes"; else echo "no"; fi)

#   helper macros for creating local copies of GNU config scripts
%update_config_scripts   chmod u+w config.guess config.sub; cp %{l_prefix}/lib/openpkg/config.guess .; cp %{l_prefix}/lib/openpkg/config.sub .

#   default list of vendor source fetching locations
%l_fetch_mirror_0        -
%l_fetch_mirror_1        -
%l_fetch_mirror_2        -
%l_fetch_mirror_3        -
%l_fetch_mirror_4        -
%l_fetch_mirror_5        -
%l_fetch_mirror_6        -
%l_fetch_mirror_7        -
%l_fetch_mirror_8        -
%l_fetch_mirror_9        -
%l_fetch_backup_0        -
%l_fetch_backup_1        -
%l_fetch_backup_2        -
%l_fetch_backup_3        -
%l_fetch_backup_4        -
%l_fetch_backup_5        -
%l_fetch_backup_6        -
%l_fetch_backup_7        -
%l_fetch_backup_8        -
%l_fetch_backup_9        ftp://ftp.openpkg.org/sources/DST/%{name}/

#   sane build environment setup
%l_build_path            %{l_prefix}/bin:%{l_prefix}/sbin:@l_build_path@
%l_build_ldlp            %{l_prefix}/lib:@l_build_ldlp@
%l_build_ulim            @l_build_ulim@
%l_build_shell_cmd       %{l_prefix}/lib/openpkg/bash
%l_build_shell_opt       --norc --noprofile --posix

#   sane build environment for manual usage in macros (currently just $PATH)
%l_sane_env              PATH="%{l_build_path}"; export PATH; LD_LIBRARY_PATH="%{l_build_ldlp}"; export LD_LIBRARY_PATH

#   sane build environment for %prep/%build/%install
%_buildshell             env -i %{l_build_shell_cmd} %{l_build_shell_opt}
%___build_pre \
HOME="%{l_prefix}"\
SHELL="%{l_build_shell_cmd}"\
TERM="%(echo $TERM)"\
TMPDIR="%{_tmppath}"\
TEMPDIR="%{_tmppath}"\
USER="%(echo $USER)"\
LOGNAME="%(echo $LOGNAME)"\
PATH="%{l_build_path}"\
LD_LIBRARY_PATH="%{l_build_ldlp}"\
export HOME SHELL TMPDIR TEMPDIR USER LOGNAME PATH LD_LIBRARY_PATH\
RPM_SOURCE_DIR=\"%{u2p:%{_sourcedir}}\"\
RPM_BUILD_DIR=\"%{u2p:%{_builddir}}\"\
RPM_OPT_FLAGS=\"%{optflags}\"\
RPM_ARCH=\"%{_arch}\"\
RPM_OS=\"%{_os}\"\
RPM_DOC_DIR=\"%{_docdir}\"\
RPM_PACKAGE_NAME=\"%{name}\"\
RPM_PACKAGE_VERSION=\"%{version}\"\
RPM_PACKAGE_RELEASE=\"%{release}\"\
export RPM_SOURCE_DIR RPM_BUILD_DIR RPM_OPT_FLAGS RPM_ARCH RPM_OS RPM_DOC_DIR\
export RPM_PACKAGE_NAME RPM_PACKAGE_VERSION RPM_PACKAGE_RELEASE\
%{?buildroot:RPM_BUILD_ROOT=\"%{u2p:%{buildroot}}\"\
export RPM_BUILD_ROOT}\
%{l_build_ulim}\
umask 022\
%{verbose:set -x}%{!verbose:exec >/dev/null}\
cd %{u2p:%{_builddir}}

