# ---
# ## used by gem2rpm
# :summary: this is a custom summary
# ## used by gem2rpm
# :description: |-
#   this is a custom description
#
#   it can be multiline
# ## used by gem2rpm
# :license: MIT or Ruby
# ## used by gem2rpm and gem_packages
# :version_suffix: -x_y
# ## used by gem2rpm
# :preamble: |-
#   BuildRequires: foobar
#   Requires: foobar
# ## used by gem2rpm
# :patches:
# - foo.patch: -p1
#   bar.patch: 
# ## used by gem2rpm
# :sources:
# - foo.desktop
# - bar.desktop
# :gem_install_args: '....'
# ## used by gem2rpm
# :pre_install: |-
#   %if 0%{?use_system_libev}
#   export USE_VENDORED_LIBEV="no"
#   %endif
# ## used by gem2rpm
# :post_install: |-
#   # delete custom files here or do other fancy stuff
#   install -D -m 0644 %{S:1} %{buildroot}%{_bindir}/gem2rpm-opensuse
# ## used by gem_packages
# :main:
#   :preamble: |-
#     Requires: util-linux
#     Recommends: pwgen
#   :filelist: |-
#     /usr/bin/gem2rpm-opensuse
# ## used by gem_packages
# :custom:
#   apache:
#     :preamble: |-
#       Requires: .....
#     :filelist: |-
#       /etc/apache2/conf.d/passenger.conf
#     :summary: Custom summary is optional
#     :description: |-
#       Custom description is optional
#
#       bar
---
:license: MIT and GPL-2.0
:preamble: |-
  %define apxs /usr/sbin/apxs2
  %define apache_libexecdir %(%{apxs} -q LIBEXECDIR)
  %define apache_sysconfdir %(%{apxs} -q SYSCONFDIR)
  %define apache_mmn        %(MMN=$(%{apxs} -q LIBEXECDIR)_MMN; test -x $MMN && $MMN)
  %if 0%{?suse_version} > 1220
  %bcond_without systemd
  %define passenger_tempdir /run/passenger
  %else
  %bcond_with    systemd
  %define passenger_tempdir /var/run/passenger
  %endif
  %bcond_without systemlibs
  BuildRequires:  apache2-devel
  BuildRequires:  boost-devel
  BuildRequires:  gcc-c++
  %if 0%{?suse_version} >= 1110
  BuildRequires:  libcurl-devel
  %else
  BuildRequires:  curl-devel
  %endif
  BuildRequires:  zlib-devel
  %if %{with systemlibs}
  #BuildRequires:  libeio-devel
  #BuildRequires:  libev-devel
  %endif
  BuildRequires:  %{rubygem rake}
  # TODO: move to subpackage
  Recommends:     packageand(apache2:rubygem-passenger-apache2)
  Recommends:     packageand(nginx:rubygem-passenger-nginx)
  Requires:       rubygem(passenger) = %{version}
  Recommends:     rubygem(%{rb_default_ruby_abi}:passenger) = %{version}
:sources:
- mod_passenger.conf
- mod_passenger_root.include
- nginx_passenger.conf
- nginx_passenger_root.include
- passenger.systemd.conf
:patches:
  #  passenger-4.0.14_missing_includes.patch: -p1
  passenger-4.0.50_fix_shebang.patch: -p0
  passenger-4.0.50_load_system_passenger_libs.patch: -p0
  passenger-4.0.50_paths.patch: -p1
  passenger-4.0.53_fix-default-temp-dir.patch: -p0
:pre_install: |-
  # TODO:
  # currently -lev gets lost when using system libev
  %if %{with systemlibs}
  export USE_VENDORED_LIBEV="yes"
  export USE_VENDORED_LIBEIO="yes"
  %endif
:post_install: |-
  # avoid BUILDROOT leaking into the binaries
  pushd %{mod_full_name}
    export PATH="%{_sbindir}:$PATH"

    #compiling nginx stuff
    rake nginx:clean nginx CACHING=no

    #compiling apache2 stuff
    rake apache2 CACHING="no"
    install -D -m 0755 buildout/apache2/mod_passenger.so %{buildroot}%{apache_libexecdir}/mod_passenger.so

    for i in $(/usr/bin/ruby-find-versioned) ; do
      rubysuffix="${i#/usr/bin/ruby}"
      /usr/bin/rake$rubysuffix native_support
      # we dont calculate the arch here as passenger build seems to use the running host arch and not the arch that ruby was build with.
      rubydir=$($i -r rubygems -e 'puts "#{Gem.ruby_engine}-#{RUBY_VERSION}-"')
      gemdir="%{buildroot}$(/usr/bin/gem$rubysuffix env gemdir)/gems/%{mod_full_name}/lib"
      cp -v buildout/ruby/$rubydir*/*.so $gemdir
      /usr/bin/rake$rubysuffix native_support:clean
    done
    find buildout -name \*.o -print -delete
    mkdir -p %{buildroot}%{_libdir}/passenger/%{version}/buildout/
    cp -av buildout/support-binaries/ %{buildroot}%{_libdir}/passenger/%{version}/buildout/
    cp -av helper-scripts/ bin/ resources/ %{buildroot}%{_libdir}/passenger/%{version}/
  popd
  #
  # webserver configs
  install -D -m 0644 %{S:1} %{buildroot}%{apache_sysconfdir}/conf.d/mod_passenger.conf
  install -D -m 0644 %{S:2} %{buildroot}%{apache_sysconfdir}/conf.d/mod_passenger_root.include
  install -D -m 0644 %{S:3} %{buildroot}/etc/nginx/conf.d/passenger.conf
  install -D -m 0644 %{S:4} %{buildroot}/etc/nginx/conf.d/passenger_root.include
  #
  sed -i -e "s,@passengertmpdir@,%{passenger_tempdir},"           %{buildroot}%{apache_sysconfdir}/conf.d/mod_passenger.conf
  sed -i -e "s,@PassengerRoot@,%{_libdir}/passenger/%{version}/," %{buildroot}%{apache_sysconfdir}/conf.d/mod_passenger_root.include

  sed -i -e "s,@passengertmpdir@,%{passenger_tempdir},"           %{buildroot}/etc/nginx/conf.d/passenger.conf
  sed -i -e "s,@PassengerRoot@,%{_libdir}/passenger/%{version}/," %{buildroot}/etc/nginx/conf.d/passenger_root.include
  #
  %if %{with systemd}
  install -D -m 0644 %{S:5} %{buildroot}/usr/lib/tmpfiles.d/passenger.conf
  %endif
:filelist: |-
  %if %{with systemd}
  /usr/lib/tmpfiles.d/passenger.conf
  %endif
  %dir %{_libdir}/passenger/
  %{_libdir}/passenger/%{version}/
:scripts:
  :post: |-
    %if %{with systemd}
    systemd-tmpfiles --create /usr/lib/tmpfiles.d/passenger.conf || true
    %endif
:main:
  :preamble: |-
    Recommends:     packageand(apache2:rubygem-passenger-apache2)
    Recommends:     packageand(nginx:rubygem-passenger-nginx)
:custom_pkgs:
  apache2:
    :preamble: |-
      Requires:       %{apache_mmn}
      Requires:       apache2
      Requires:       rubygem-passenger = %{version}
      Summary:        Passenger apache module
      Group:          Development/Languages/Ruby
    :filelist: |-
     %config(noreplace) %{apache_sysconfdir}/conf.d/mod_passenger.conf
     %config            %{apache_sysconfdir}/conf.d/mod_passenger_root.include
     %{apache_libexecdir}/mod_passenger.so
  nginx:
    :preamble: |-
      Requires:       nginx
      Requires:       rubygem-passenger = %{version}
      Summary:        Passenger Nginx module
      Group:          Development/Languages/Ruby
    :filelist: |-
      %dir /etc/nginx/
      %dir /etc/nginx/conf.d/
      %config(noreplace) /etc/nginx/conf.d/passenger.conf
      %config            /etc/nginx/conf.d/passenger_root.include
