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

#------------------------------------------------------------------------
# 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   /etc/init.d

#------------------------------------------------------------------------
# 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)

#------------------------------------------------------------------------
# %{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" 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) \
}}

#------------------------------------------------------------------------
# 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}

#------------------------------------------------------------------------
# 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}


#------------------------------------------------------------------------
# %%make
#------------------------------------------------------------------------

# 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
#------------------------------------------------------------------------

# 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
#------------------------------------------------------------------------

# set these variables in specfile if needed
%_configure_cmd		./configure
%_configure_env \\\
    CFLAGS="${CFLAGS:-%optflags}" \\\
    CXXFLAGS="${CXXFLAGS:-%optflags}" \\\
    FFLAGS="${FFLAGS:-%optflags}"

%_configure_args \\\
	--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}

# configure macro
%configure \
  %{?__libtoolize:[ -f configure.in ] && %{__libtoolize} --copy --force ;} \
  %{?_configure_env} %{_configure_cmd} %{_target_platform} %{?_configure_args}

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

# 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"]')

# distribution version macros
%dist .mo6
%momonga 6

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