# Generated from <%= package.spec.file_name %> by gem2rpm -*- rpm-spec -*-
%global gemname <%= spec.name %>

<% if nongem -%>
%global ruby_sitelib %(ruby -rrbconfig -e "puts Config::CONFIG['sitelibdir']")
<% end -%>
%global gemdir %(ruby -rubygems -e 'puts Gem::dir' 2>/dev/null)
%global geminstdir %{gemdir}/gems/%{gemname}-%{version}
<%# TODO: Try to check against spec.required_ruby_version -%>
%global rubyabi 1.8

Summary: <%= spec.summary.gsub(/\.$/, "") %>
Name: rubygem-%{gemname}
Version: <%= spec.version %>
Release: 1%{?dist}
Group: Development/Languages
License: <%= spec.licenses.join(" and ") %>
<% if spec.homepage -%>
URL: <%= spec.homepage %>
<% end -%>
Source0: <%= download_path %>%{gemname}-%{version}.gem
Requires: ruby(abi) = %{rubyabi}
<% for req in spec.required_rubygems_version -%>
Requires: ruby(rubygems) <%= req %>
<% end -%>
<% for d in spec.runtime_dependencies -%>
<% for req in d.requirement -%>
Requires: rubygem(<%= d.name %>) <%= req  %>
<% end -%>
<% end -%>
BuildRequires: ruby(abi) = %{rubyabi}
<% for req in spec.required_rubygems_version -%>
BuildRequires: ruby(rubygems) <%= req %>
<% end -%>
<%# TODO: Unfortunatelly this do not match with ruby(abi) yet -%>
<% for req in spec.required_ruby_version -%>
BuildRequires: ruby<%= "-devel" unless spec.extensions.empty? %> <%= req %>
<% end -%>
<% if spec.extensions.empty? -%>
BuildArch: noarch
<% end -%>
Provides: rubygem(%{gemname}) = %{version}

%description
<%= spec.description %>

<% if doc_subpackage -%>
%package doc
Summary: Documentation for %{name}
Group: Documentation
Requires: %{name} = %{version}-%{release}
BuildArch: noarch

%description doc
Documentation for %{name}
<% end # if doc_subpackage -%>

<% if nongem -%>
%package -n ruby-%{gemname}
Summary: <%= spec.summary.gsub(/\.$/, "") %>
Group: Development/Languages
Requires: rubygem(%{gemname}) = %{version}
<% spec.files.select{ |f| spec.require_paths.include?(File::dirname(f)) }.reject { |f| f =~ /\.rb$/ }.collect { |f| File::basename(f) }.each do |p| -%>
Provides: ruby(<%= p %>) = %{version}
<% end -%>
%description -n ruby-%{gemname}
<%= spec.description -%>
<% end # if nongem -%>

%prep
%setup -q -c -T
mkdir -p .%{gemdir}
<% unless spec.extensions.empty? -%>
export CONFIGURE_ARGS="--with-cflags='%{optflags}'"
<% end -%>
gem install --local --install-dir .%{gemdir} \
<% unless spec.executables.nil? or spec.executables.empty? -%>
            --bindir .%{_bindir} \
<% end -%>
<% unless spec.extensions.empty? -%>
            -V \
<% end -%>
            --force %{SOURCE0}

%build

%install
mkdir -p %{buildroot}%{gemdir}
cp -pa .%{gemdir}/* \
        %{buildroot}%{gemdir}/
<% unless spec.executables.nil? or spec.executables.empty? -%>

mkdir -p %{buildroot}%{_bindir}
cp -pa .%{_bindir}/* \
        %{buildroot}%{_bindir}/
<% end -%>

<% unless spec.executables.empty? -%>
find %{buildroot}%{geminstdir}/bin -type f | xargs chmod a+x
<% end -%>
<% if nongem -%>
mkdir -p %{buildroot}%{ruby_sitelib}
<% spec.files.select{ |f| spec.require_paths.include?(File::dirname(f)) }.each do |p| -%>
ln -s %{gemdir}/gems/%{gemname}-%{version}/<%= p %> %{buildroot}%{ruby_sitelib}
<% end -%>
<% end # if nongem -%>

%files
%dir %{geminstdir}
<% unless spec.executables.nil? or spec.executables.empty? -%>
<% for f in spec.executables -%>
%{_bindir}/<%= f %>
<% end -%>
%{geminstdir}/bin
<% end -%>
%{geminstdir}/lib
<% unless spec.extensions.empty? -%>
<%# TODO: Is it possible to delete the folder specified by the spec.extensions? -%>
%exclude %{geminstdir}/ext
<% end -%>
<% unless doc_subpackage -%>
%doc %{gemdir}/doc/%{gemname}-%{version}
<% for f in spec.extra_rdoc_files -%>
%doc %{geminstdir}/<%= f %>
<% end -%>
<% end -%>
%exclude %{gemdir}/cache/%{gemname}-%{version}.gem
%{gemdir}/specifications/%{gemname}-%{version}.gemspec

<% if doc_subpackage -%>
%files doc
%doc %{gemdir}/doc/%{gemname}-%{version}
<% for f in spec.extra_rdoc_files -%>
%doc %{geminstdir}/<%= f %>
<% end -%>
<% end # if doc_subpackage -%>

<% if nongem -%>
%files -n ruby-%{gemname}
%{ruby_sitelib}/*
<% end # if nongem -%>

%changelog
* <%= Time.now.strftime("%a %b %d %Y") %> <%= packager %> - <%= spec.version %>-1
- Initial package
