#########################################################################
##########            rpm macros for Momonga Linux            ###########
#########################################################################

#------------------------------------------------------------------------
# distribution version macros
#------------------------------------------------------------------------
%dist .mo7
%momonga 7


#------------------------------------------------------------------------
# change default compression type and level for binary package payloads
#------------------------------------------------------------------------
%_source_payload w9.gzdio
%_binary_payload w7.xzdio


#------------------------------------------------------------------------
# use SHA-256 for FILEDIGESTS instead of default MD5
#------------------------------------------------------------------------
%_source_filedigest_algorithm 8
%_binary_filedigest_algorithm 8


#------------------------------------------------------------------------
# overrides directory macros
#------------------------------------------------------------------------
%_prefix          /usr
%_sysconfdir      /etc
%_localstatedir   /var
%_exec_prefix     %{expand:%%{_prefix}}
%_bindir          %{expand:%%{_exec_prefix}/bin}
%_sbindir         %{expand:%%{_exec_prefix}/sbin}
%_libexecdir      %{expand:%%{_exec_prefix}/libexec}
%_datadir         %{expand:%%{_prefix}/share}
%_sharedstatedir  %{expand:%%{_prefix}/com}
%_libdir          %{expand:%%{_exec_prefix}/%%{_lib}}
%_includedir      %{expand:%%{_prefix}/include}
%_infodir         %{expand:%%{_datadir}/info}
%_mandir          %{expand:%%{_datadir}/man}
%_initscriptdir   %{_sysconfdir}/init.d


#------------------------------------------------------------------------
# additional directory macros
#------------------------------------------------------------------------
%_fmoddir	  %{_libdir}/gfortran/modules


#------------------------------------------------------------------------
# macros that are expanded to newline
#------------------------------------------------------------------------
%_newline \
%{nil}

%_nl	%_newline

%usagi_ipv6  0
%_ipv6  1


#------------------------------------------------------------------------
# additional perl macros
#------------------------------------------------------------------------

# from Artur Frysiak <wiget@t17.ds.pwr.wroc.pl>
%perl_privlib  %(eval "`perl -V:installprivlib`"; echo $installprivlib)
%perl_sitelib  %(eval "`perl -V:installsitelib`"; echo $installsitelib)

# perl requires macro that skips /usr/share/doc
%__perl_requires        /usr/lib/rpm/momonga/perl.req


#------------------------------------------------------------------------
# additional ruby macros
#------------------------------------------------------------------------
%ruby_libdir %(ruby -rrbconfig -e 'puts Config::CONFIG["rubylibdir"]')
%ruby_archdir %(ruby -rrbconfig -e 'puts Config::CONFIG["archdir"]')
%ruby_sitearchdir %(ruby -rrbconfig -e 'puts Config::CONFIG["sitearchdir"]')
%ruby_sitelibdir %(ruby -rrbconfig -e 'puts Config::CONFIG["sitelibdir"]')


#------------------------------------------------------------------------
# %{echo:...} replacement
#------------------------------------------------------------------------

### Description:
###
###   %echonl writes arguments to stderr just like builtin %{echo:...},
###   but newline is appended by default (like echo(1)).
###   If "-n" options is supplied, trailing newline is not printed.
###
### Usage:
###     %echonl [ -n ] ...
###

%echonl(n) %{?-n:%{echo:%*}}%{!?-n:%{echo:%*\
}}


#------------------------------------------------------------------------
# append text to existing macro
#------------------------------------------------------------------------

### Description:
###
###   Define macro just like builtin %define (or %global),
###   but if that macro is already defined,
###   supplied arguments are append to the end of the macro.
###   If macro is not yet defined, just works like %define (or %global).
###
### Examples:
###
###   (1) If macro "%hoge" is not defined,
###
###           %append hoge buroro 
###
###       is equivalent to
###
###           %define hoge buroro
###
###   (2) If macro "%hoge" has already been defined as "fuga",
###       
###           %append hoge buroro
###
###       makes "%hoge" to "fuga buroro".
###
###   (3) Append "--host=alpha-momonga-linux" to %_configure_args:
###
###           %append _configure_args -- --host=alpha-momonga-linux
###
###       Since macro arguments are processed by getopt(3),
###       any arguments that begin with '-' are treated by options.
###       So, adding '--' before arguments is required.
###
%append() \
%define __macro_name %1 \
%{expand:%%global %__macro_name %%{?%__macro_name} %(set -- %*; shift; echo $*)}


#------------------------------------------------------------------------
# comment out macro
#------------------------------------------------------------------------

### Description:
###
###   This macro and all remaining characters up to newline are removed.
###

%comment()		%nil


#------------------------------------------------------------------------
# include local configuration file if exists
#------------------------------------------------------------------------
%_specoptdir		%(echo $HOME)/rpmbuild/specopt
%_specoptfile		%_specoptdir/%name.specopt
%include_specopt \
%{!?name:%{warn:"%%include_specopt" must be defined **after** Name: \
\
}} \
%{?name:\
%{expand: \
    %(if [ -f %_specoptfile ]; then \
        /bin/cat %_specoptfile ; \
    fi) \
}}


#------------------------------------------------------------------------
# (OBSOLETE) define %NoSource() and %NoPatch()
#------------------------------------------------------------------------

###  Usage: 
###   
###    %NoSource N srcname md5sum
###    %NoPatch N srcname md5sum
###
###  Description:
###
###    %NoSource is expanded to
###
###       SourceN: srcname
###       NoSource: N
###
###    and md5sum is registered in %__checksumN to be checked 
###    by %_checksum_cmd in %prep section.
###

%NoSource() \
%{expand:%%global __checksum%1 %3} \
%{?__nosource:%{expand:%%global __nosource %__nosource %1}} \
%{!?__nosource:%{expand:%%global __nosource %1}} \
Source%1: %2 \
%{!?_src:NoSource: %1}\
%{nil}

%NoPatch() \
%{expand:%%global __patch_checksum%1 %3} \
%{?__nopatch:%{expand:%%global __nopatch %__nopatch %1}} \
%{!?__nopatch:%{expand:%%global __nopatch %1}} \
Patch%1: %2 \
%{!?_src:NoPatch: %1}\
%{nil}


#------------------------------------------------------------------------
# (OBSOLETE) check checksums defined by %NoSource() and %NoPatch()
#------------------------------------------------------------------------
%_checksum_cmd		/usr/bin/md5sum
%_checksum_cmd_args	--check

%__spec_prep_checksum \
%{?__nosource:\
set %{expand:%(for n in %__nosource; do \\\
                 echo -n "%%__checksum$n %%SOURCE$n "; \\\
               done) \\\
     } \
checksum=$@ \
} \
%{?__nopatch:\
set %{expand:%(for n in %__nopatch; do \\\
                 echo -n "%%__patch_checksum$n %%PATCH$n "; \\\
               done) \\\
     } \
patch_checksum=$@ \
} \
set -- $checksum $patch_checksum; \
while [ $# -gt 0 ]; do \
  echo "$1  $2" | %{_checksum_cmd} %{_checksum_cmd_args}; \
  shift 2; \
done \
%{nil}

%__spec_prep_template	#!%{__spec_prep_shell}\
%{__spec_prep_pre}\
%{__spec_prep_checksum}\
%{nil}


#------------------------------------------------------------------------
# additional make macro
#------------------------------------------------------------------------

# set these variables in specfile if needed
%_make_env	%nil
%_make_args	%nil
%_make_target	%nil

# make macro
%make() \\\
  %define ___make_cmdln_args %* \
  %{?_smp_mflags:\
    %{?_make_env} %__make %{_smp_mflags} %{?_make_args} %{___make_cmdln_args} %{?_make_target} || \\\
  } \\\
  %{?_make_env} %__make %{?_make_args} %{___make_cmdln_args} %{?_make_target}


#------------------------------------------------------------------------
# overwrite makeinstall macro
#------------------------------------------------------------------------

# set these variables in specfile if needed
%_makeinstall_target	install
%_makeinstall_env	%nil
%_makeinstall_args \\\
	prefix=%{?buildroot:%{buildroot}}%{_prefix} \\\
	exec_prefix=%{?buildroot:%{buildroot}}%{_exec_prefix} \\\
	bindir=%{?buildroot:%{buildroot}}%{_bindir} \\\
	sbindir=%{?buildroot:%{buildroot}}%{_sbindir} \\\
	sysconfdir=%{?buildroot:%{buildroot}}%{_sysconfdir} \\\
	datadir=%{?buildroot:%{buildroot}}%{_datadir} \\\
	includedir=%{?buildroot:%{buildroot}}%{_includedir} \\\
	libdir=%{?buildroot:%{buildroot}}%{_libdir} \\\
	libexecdir=%{?buildroot:%{buildroot}}%{_libexecdir} \\\
	localstatedir=%{?buildroot:%{buildroot}}%{_localstatedir} \\\
	sharedstatedir=%{?buildroot:%{buildroot}}%{_sharedstatedir} \\\
	mandir=%{?buildroot:%{buildroot}}%{_mandir} \\\
	infodir=%{?buildroot:%{buildroot}}%{_infodir}

# makeinstall macro
%makeinstall \\\
    %{?_makeinstall_env} %__make %{?_makeinstall_args} %{?_makeinstall_target}


#------------------------------------------------------------------------
# overwrite configure macro
#------------------------------------------------------------------------
%configure \
  CFLAGS="${CFLAGS:-%optflags}" ; export CFLAGS ; \
  CXXFLAGS="${CXXFLAGS:-%optflags}" ; export CXXFLAGS ; \
  FFLAGS="${FFLAGS:-%optflags -I%_fmoddir}" ; export FFLAGS ; \
  ./configure --build=%{_build} --host=%{_host} \\\
	--target=%{_target_platform} \\\
	--program-prefix=%{?_program_prefix} \\\
	--prefix=%{_prefix} \\\
	--exec-prefix=%{_exec_prefix} \\\
	--bindir=%{_bindir} \\\
	--sbindir=%{_sbindir} \\\
	--sysconfdir=%{_sysconfdir} \\\
	--datadir=%{_datadir} \\\
	--includedir=%{_includedir} \\\
	--libdir=%{_libdir} \\\
	--libexecdir=%{_libexecdir} \\\
	--localstatedir=%{_localstatedir} \\\
	--sharedstatedir=%{_sharedstatedir} \\\
	--mandir=%{_mandir} \\\
	--infodir=%{_infodir}

#------------------------------------------------------------------------
# create debuginfo sub package macros
#------------------------------------------------------------------------
%install %{?_enable_debug_packages:%{?buildsubdir:%{debug_package}}}\
%%install\
%{nil}


#------------------------------------------------------------------------
# overwrite __os_install_post macro
#------------------------------------------------------------------------
%__os_install_post    \
    %{_rpmconfigdir}/momonga/brp-compress \
    %{_rpmconfigdir}/momonga/brp-strip \
    %{_rpmconfigdir}/momonga/brp-strip-shared \
    %{_rpmconfigdir}/brp-strip-static-archive \
    %{_rpmconfigdir}/momonga/brp-strip-comment-note \
    %{_rpmconfigdir}/brp-python-bytecompile %{__python} \
    %{_rpmconfigdir}/momonga/brp-python-hardlink \
    %{!?__jar_repack:%{_rpmconfigdir}/momonga/brp-java-repack-jars} \
    %{_rpmconfigdir}/momonga/modify-init.d \
    %{_rpmconfigdir}/momonga/modify-la \
%{nil}


#------------------------------------------------------------------------
# generic auto req/prov filtering macros
#------------------------------------------------------------------------
# prevent anything matching from being scanned for provides
%filter_provides_in(P) %{expand: \
%global __filter_prov_cmd %{?__filter_prov_cmd} %{__grep} -v %{-P} '%*' | \
}

# prevent anything matching from being scanned for requires
%filter_requires_in(P) %{expand: \
%global __filter_req_cmd %{?__filter_req_cmd} %{__grep} -v %{-P} '%*' | \
}

# filter anything matching out of the provides stream
%filter_from_provides() %{expand: \
%global __filter_from_prov %{?__filter_from_prov} | %{__sed} -e '%*' \
}

# filter anything matching out of the requires stream
%filter_from_requires() %{expand: \
%global __filter_from_req %{?__filter_from_req} | %{__sed} -e '%*' \
}

# actually set up the filtering bits 
%filter_setup %{expand: \
%global _use_internal_dependency_generator 0 \
%global __deploop() while read FILE; do /usr/lib/rpm/rpmdeps -%{1} ${FILE}; done | /bin/sort -u \
%global __find_provides /bin/sh -c "%{?__filter_prov_cmd} %{__deploop P} %{?__filter_from_prov}" \
%global __find_requires /bin/sh -c "%{?__filter_req_cmd}  %{__deploop R} %{?__filter_from_req}" \
}
