:license: Apache-2.0
:disable_docs: true

:sources:
- chef-server.rb
- chef-server.service
- chef-server.tmpfiles.d
- chef-server.init
- chef-server.logrotate
- chef-couchdb-cleanup

:patches:
  CHEF-2500-data_item_raw_data.patch: -p1
  sd_notify.patch: -p1
  import_public_key.patch: -p0

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

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

:main:
  :preamble: |-
    Requires:  rubygem-chef-server-api

:post_install: |-
  install -d -m 750 %{buildroot}%{_sysconfdir}/cron.daily
  install -D -m 755 %{SOURCE6} %{buildroot}%{_sysconfdir}/cron.daily/chef-couchdb-cleanup

  install -d -m 755 %{buildroot}%{_sysconfdir}/logrotate.d
  install -D -m 644 %{SOURCE5} %{buildroot}%{_sysconfdir}/logrotate.d/chef-server

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

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

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

  install -d -m 755 %{buildroot}%{_sbindir}
  ln -s %{_sysconfdir}/init.d/chef-server %{buildroot}%{_sbindir}/rcchef-server
  %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

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

:scripts:
  :pretrans: |-
    # /etc/chef/server.rb used to live in rubygem-chef-server, which likely
    # doesn't exist anymore and got moved to .rpmsave on upgrade; try to re-use
    # it
    if [ ! -f /etc/chef/server.rb -a -f /etc/chef/server.rb.rpmsave ]; then
      echo "re-using /etc/chef/server.rb.rpmsave as /etc/chef/server.rb"
      cp -a /etc/chef/server.rb.rpmsave /etc/chef/server.rb
    fi

  :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-server.service
    %endif

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

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

  :postun: |-
    %if 0%{?suse_version} >= 1230
    %service_del_postun chef-server.service
    %else
    %restart_on_update chef-server
    %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-server.conf
  %{_unitdir}/chef-server.service
  %else
  %{_sysconfdir}/init.d/chef-server
  %endif

  %{_sbindir}/rcchef-server

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

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

  %attr(0755, root, root) %{_sysconfdir}/cron.daily/chef-couchdb-cleanup
