:license: Apache-2.0
:disable_docs: true

:sources:
- chef-client.rb
- chef-client.service
- chef-client.tmpfiles.d
- chef-client.init
- chef-client.logrotate
- chef-create-amqp-passwd
- zypper-retry
- rubygem-chef-rpmlintrc

:patches:
  CHEF-672-load-libraries-recursively.patch: -p1
  CHEF-867-use-exclusive-file-locks.patch: -p0
  CHEF-867-comment-additions.patch: -p1
  CHEF-3617-create-full-path-on-run-lock.patch: -p0
  CHEF-867-use-cloexec-if-we-can.patch: -p1
  CHEF-3938-dont-disable-gpg-checks-in-zypper.patch: -p0
  CHEF-4010-ensure-lock-release.patch: -p0
  CHEF-4322-only-if-there-are-actual-changes.patch: -p1
  CHEF-4322-expose-file-edited.patch: -p1
  CHEF-4090-refactor-zypper-package-provider.patch: -p0
  CHEF-4090-solve-merge-conflict.patch: -p0
  non-interactive-zypper.patch: -p0
  ask-rpm-before-zypper.patch: -p0
  call-zypper-retry.patch: -p1
  disable-chef11-deprecation.patch: -p0
  enable-zypper-check-gpg.patch: -p0
  deep-merge-ruby2.1.patch: -p0
  pr2052-change-default-service-mapping-for-sles-to-systemd.patch: -p1
  pr2187-change-default-group-mapping-for-sles.patch: -p1
  create_client_with_public_key.patch: -p0

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

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

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

:pre_install: |-
  # We need to rebuild files list as we create new files that are nort covered by the gemspec
  perl -p -i -e 's;s\.files = .*$;s.files = %w(Rakefile LICENSE README.rdoc) + Dir.glob("{distro,lib,tasks,spec}/**/*", File::FNM_DOTMATCH).reject {|f| File.directory?(f) };' %{mod_full_name}/*.gemspec

  %gem_build

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

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

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

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

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

  install -d -m 755 %{buildroot}%{_bindir}
  install -D -m 750 %{SOURCE7} %{buildroot}%{_bindir}/zypper-retry

  install -d -m 755 %{buildroot}%{_sbindir}
  install -D -m 750 %{SOURCE6} %{buildroot}%{_sbindir}/chef-create-amqp-passwd

  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 755 %{buildroot}%{_mandir}/man1
  cp distro/common/man/man1/* %{buildroot}%{_mandir}/man1/

  install -d -m 755 %{buildroot}%{_mandir}/man8
  cp distro/common/man/man8/* %{buildroot}%{_mandir}/man8/

  rm -rf %{buildroot}%{gem_base}/gems/%{mod_full_name}/distro

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

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

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

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

  %{_sbindir}/rcchef-client

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

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

  %attr(0750, root, root) %{_bindir}/zypper-retry
  %attr(0750, root, chef) %{_sbindir}/chef-create-amqp-passwd

  %doc %{_mandir}/man1/*
  %doc %{_mandir}/man8/*
