:license: Apache-2.0
:disable_docs: true

:sources:
- chef-solr.rb
- chef-solr.service
- chef-solr.tmpfiles.d
- chef-solr.init
- chef-solr.logrotate

:preamble: |-
  %if 0%{?suse_version} >= 1230
  BuildRequires:  pkgconfig(systemd)
  %endif

  Requires:       jre >= 1.7.0
  Requires:       logrotate
  Requires:       rabbitmq-server
  BuildRequires:  fastjar
  BuildRequires:  fdupes
  %if 0%{?suse_version} > 1110
  Requires(pre):  pwdutils
  %else
  Requires(pre):  shadow-utils
  %endif
  PreReq:         rubygem(chef-solr) = %{version}
  #!BuildIgnore:  rubygem(chef-solr)

:main:
  :preamble: |-
    Requires:  rubygem-chef-solr

:pre_install: |-
  %gem_unpack

:post_install: |-
  install -d -m 755 %{buildroot}%{_sysconfdir}/logrotate.d
  install -D -m 644 %{SOURCE5} %{buildroot}%{_sysconfdir}/logrotate.d/chef-solr

  install -d -m 750 %{buildroot}%{_sysconfdir}/chef
  install -D -m 644 %{SOURCE1} %{buildroot}%{_sysconfdir}/chef/solr.rb

  %if 0%{?suse_version} >= 1230
  install -D -m 644 %{SOURCE3} %{buildroot}/usr/lib/tmpfiles.d/chef-solr.conf
  install -D -m 0644 %{SOURCE2} %{buildroot}%{_unitdir}/chef-solr.service

  install -d -m 755 %{buildroot}%{_sbindir}
  ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rcchef-solr
  %else
  install -d -m 755 %{buildroot}%{_sysconfdir}/init.d
  install -D -m 750 %{SOURCE4} %{buildroot}%{_sysconfdir}/init.d/chef-solr

  install -d -m 755 %{buildroot}%{_sbindir}
  ln -s %{_sysconfdir}/init.d/chef-solr %{buildroot}%{_sbindir}/rcchef-solr
  %endif

  install -d -m 750 %{buildroot}%{_localstatedir}/log/chef
  install -d -m 750 %{buildroot}%{_localstatedir}/lib/chef
  install -d -m 750 %{buildroot}%{_localstatedir}/cache/chef
  %if 0%{?suse_version} < 1230
  install -d -m 750 %{buildroot}%{_localstatedir}/run/chef
  %endif

  install -d -m 750 %{buildroot}%{_sysconfdir}/chef/solr-jetty

  install -d -m 750 %{buildroot}%{_localstatedir}/cache/chef/solr/data
  install -d -m 750 %{buildroot}%{_localstatedir}/lib/chef/solr
  install -d -m 750 %{buildroot}%{_localstatedir}/lib/chef/solr/solr-jetty

  source=$PWD

  pushd %{buildroot}%{_localstatedir}/lib/chef/solr
    tar zxvf $source/solr/solr-home.tar.gz
  popd

  pushd %{buildroot}%{_localstatedir}/lib/chef/solr/solr-jetty
    tar zxvf $source/solr/solr-jetty.tar.gz
  popd

  rm %{buildroot}%{_localstatedir}/lib/chef/solr/solr-jetty/webapps/._solr.war
  rm %{buildroot}%{_localstatedir}/lib/chef/solr/solr-jetty/work/.git-dont-ignore
  rm %{buildroot}%{_localstatedir}/lib/chef/solr/solr-jetty/logs/.git-dont-ignore

  mv %{buildroot}%{_localstatedir}/lib/chef/solr/solr-jetty/etc/*.xml %{buildroot}/%{_sysconfdir}/chef/solr-jetty/
  rm -rf %{buildroot}%{_localstatedir}/lib/chef/solr/solr-jetty/etc
  ln -s %{_sysconfdir}/chef/solr-jetty %{buildroot}%{_localstatedir}/lib/chef/solr/solr-jetty/etc

  find %{buildroot} -name "*.orig" -delete -print
  %fdupes %{buildroot}

:scripts:
  :pre: |-
    getent group chef >/dev/null || groupadd -r chef
    getent passwd chef >/dev/null || useradd  -r -g chef -d %{_localstatedir}/lib/chef -s /sbin/nologin -c "Opscode chef daemon" chef

    %if 0%{?suse_version} >= 1230
    %service_add_pre chef-solr.service
    %endif

  :preun: |-
    %if 0%{?suse_version} >= 1230
    %service_del_preun chef-solr.service
    %else
    %stop_on_removal chef-solr
    %endif

  :post: |-
    %if 0%{?suse_version} >= 1230
    systemd-tmpfiles --create /usr/lib/tmpfiles.d/chef-solr.conf || true
    %service_add_post chef-solr.service
    %else
    %fillup_and_insserv chef-solr
    %endif

  :postun: |-
    %if 0%{?suse_version} >= 1230
    %service_del_postun chef-solr.service
    %else
    %restart_on_update chef-solr
    %insserv_cleanup
    %endif

:filelist: |-
  %dir %attr(0750, chef, chef) %{_localstatedir}/log/chef
  %dir %attr(0750, chef, chef) %{_localstatedir}/lib/chef
  %dir %attr(0750, chef, chef) %{_localstatedir}/cache/chef
  %if 0%{?suse_version} < 1230
  %ghost %attr(0750, chef, chef) %{_localstatedir}/run/chef
  %endif

  %if 0%{?suse_version} >= 1230
  /usr/lib/tmpfiles.d/chef-solr.conf
  %{_unitdir}/chef-solr.service
  %else
  %{_sysconfdir}/init.d/chef-solr
  %endif

  %{_sbindir}/rcchef-solr

  %dir %attr(0750, root, chef) %{_sysconfdir}/chef
  %config(noreplace) %attr(0640, root, chef) %{_sysconfdir}/chef/solr.rb

  %config %attr(0640, root, chef) %{_sysconfdir}/logrotate.d/chef-solr

  %dir %attr(0750, root, chef) %{_sysconfdir}/chef/solr-jetty
  %dir %attr(0750, chef, chef) %{_localstatedir}/cache/chef/solr
  %dir %attr(0750, chef, chef) %{_localstatedir}/cache/chef/solr/data

  %dir %attr(0750, chef, chef) %{_localstatedir}/lib/chef/solr
  %dir %attr(0750, chef, chef) %{_localstatedir}/lib/chef/solr/conf
  %dir %attr(0750, chef, chef) %{_localstatedir}/lib/chef/solr/data
  %dir %attr(0750, chef, chef) %{_localstatedir}/lib/chef/solr/solr-jetty

  %attr(-, chef, chef) %{_localstatedir}/lib/chef/solr/conf/*
  %attr(-, chef, chef) %{_localstatedir}/lib/chef/solr/data/*
  %attr(-, chef, chef) %{_localstatedir}/lib/chef/solr/solr-jetty/*

  %config(noreplace) %attr(0644, chef, chef) %{_localstatedir}/lib/chef/solr/conf/solrconfig.xml
  %config %{_sysconfdir}/chef/solr-jetty/*
