#!/usr/bin/perl -w
# -*- cperl -*-

use strict;
use FindBin;


########################################################################



my $prefix = '/usr';
my $exec_prefix = "/usr";
my $bindir = "/usr/bin";
my $PACKAGE = "sampler";
my $installed = ! -e "$FindBin::Bin/Makefile";
my $driver = $installed ? "$bindir/$PACKAGE-cc" : "$FindBin::Bin/../instrumentor/driver/main";


########################################################################


my %macros = (
	      _sampler_datadir => '%{?sampler_datadir}%{!?sampler_datadir:%{_datadir}/sampler}',
	      _sampler_libdir => '%{?sampler_libdir}%{!?sampler_libdir:%{_libdir}/sampler}',
	      sampler_tooldir => "$FindBin::Bin",

	      sampler_cc => $driver,
	      sampler_cc_flags => '%{nil}',
	      sampler_cc_version => '%(rpm -q --qf "%%{version}" sampler-devel)',

	      'sampler_tags(s:)' => <<'EOF',
Requires: sampler >= 0.4
BuildRequires: sampler-devel >= 0.7.4
%global _sampler_source %{-s*}%{!-s:1}
Source%{_sampler_source}: %{name}-sampler.tar.gz
%{nil}
EOF

	      sampler_description => 'This package has been built with sampled bug feedback instrumentation.',

	      sampler_package => <<'EOF',
%package samplerinfo
Summary: Sampler information for package %{name}
Group: Development/Debug
AutoReqProv: 0
%description samplerinfo
This package provides sampler information for package %{name}.  You do
not need this to use %{name}.  Most users have no need to install this
package.
%files samplerinfo
%defattr(-,root,root)
%{_sampler_libdir}/sites/*
%{nil}
EOF

	      sampler_prep => 'tar xzf %{S:%{_sampler_source}}',

	      _sampler_sparsity => '%{?sampler_sparsity}%{!?sampler_sparsity:100}',
	      _sampler_reporting_host => '%{?sampler_reporting_host}%{!?sampler_reporting_host:www.example.org}',
	      _sampler_reporting_url => '%{?sampler_reporting_url}%{!?sampler_reporting_url:https://%{_sampler_reporting_host}/cgi-bin/sampler-upload}',

	      _sampler_name => '%{?sampler_name}%{!?sampler_name:%{name}}',

	      sampler_prebuild => <<'EOF',
export CC='%{sampler_cc} %{sampler_cc_flags}'
sed -e 's/@name@/%{name}/g' -e 's/@sparsity@/%{_sampler_sparsity}/g' -e 's!@reporting_url@!%{_sampler_reporting_url}!g' <sampler/application.schemas.in >sampler/sampler-%{name}.schemas
sed -e 's/@sampler_name@/%{_sampler_name}/g' -e 's/@sampler_cc_version@/%{sampler_cc_version}/g' -e 's/@name@/%{name}/g' -e 's/@version@/%{version}/g' -e 's/@release@/%{release}/g' -e 's!@sampler_datadir@!%{_sampler_datadir}!g' <sampler/config.in >sampler/config
sed -e 's!@sampler_datadir@!%{_sampler_datadir}!g' -e 's!@sampler_libdir@!%{_sampler_libdir}!g' -e 's!@name@!%{name}!g' <sampler/wrapper.in >sampler/wrapper
chmod a+x sampler/wrapper
%{nil}
EOF

	      sampler_wrapped => '%{_bindir}/%{name}',

	      sampler_install => <<'EOF',
install -d %{buildroot}%{_sampler_libdir}/applications/%{name}
install -m 644 sampler/config %{buildroot}%{_sampler_libdir}/applications/%{name}
mv %{buildroot}%{sampler_wrapped} %{buildroot}%{_sampler_libdir}/applications/%{name}/executable
install sampler/wrapper %{buildroot}%{sampler_wrapped}
install -d %{buildroot}%{_sysconfdir}/gconf/schemas
install sampler/sampler-%{name}.schemas %{buildroot}%{_sysconfdir}/gconf/schemas
%{sampler_tooldir}/find-sampler-info $RPM_BUILD_ROOT
%{nil}
EOF

	      sampler_files => '%attr(-, root, root) %{_sampler_libdir}/applications/%{name}',
	      sampler_files_gconf => '%attr(-, root, root) %{_sysconfdir}/gconf/schemas/sampler-%{name}.schemas',

	      sampler_post => 'env GCONF_CONFIG_SOURCE=`gconftool-2 --get-default-source` gconftool-2 --makefile-install-rule %{_sysconfdir}/gconf/schemas/sampler-%{name}.schemas >/dev/null'
);


########################################################################


while (my ($name, $value) = each %macros) {
    $value =~ s/\n(?=.)/\\\n/g;
    unshift @ARGV, '--define', "$name $value";
}

exec 'rpmbuild', @ARGV;
die "cannot exec rpmbuild: $!\n";
