:license: Apache-2.0
:sources:
  - puppet.fw
  - '%{name}-rpmlintrc'
:patches:
  'puppet-4.8.1-gemspec.patch': '-p0'
  'puppet-4.8.1-change-default-cache-dir.patch': '-p1'
:summary: A network tool for managing many disparate systems
:description: |-
  Puppet lets you centrally manage every important aspect of your system
  using a cross-platform specification language that manages all the
  separate elements normally aggregated in different files, like users,
  cron jobs, and hosts, along with obviously discrete elements like
  packages, services, and files.
:main:
  :preamble: |-
    Conflicts:  puppet < 4
    Provides:   hiera-puppet >= 1.0.0
    Obsoletes:  hiera-puppet < 1.0.0
    Requires:   %{name}
    Requires:   %{rubygem rgen >= 0.6.6}
    Requires:   %{rubygem deep_merge}
    Requires:   %{rubygem pathspec}
    Recommends: rubygem(%{rb_default_ruby_abi}:%{mod_name})
    Recommends: %{rubygem ruby-augeas}
    Recommends: %{rubygem ruby-shadow}
:preamble: |-
  %define confdir %{_sysconfdir}/puppetlabs/%{mod_name}
  %define codedir %{_sysconfdir}/puppetlabs/code
  %define logdir %{_localstatedir}/log/puppetlabs/puppet
  %define vardir %{_localstatedir}/cache/puppetlabs/puppet
  %define _fwdefdir /etc/sysconfig/SuSEfirewall2.d/services
  %define _unitdir /usr/lib/systemd
  %{!?vim_data_dir:%global vim_data_dir %{_datadir}/vim/%(readlink %{_datadir}/vim/current)}

  # not really required, but we don't want to own their dirs
  BuildRequires: vim
  BuildRequires: emacs-nox

  BuildRequires: facter
  BuildRequires: fdupes
  BuildRequires: systemd
  BuildRequires: %{rubygem rgen >= 0.6.6}
  BuildRequires: %{rubygem deep_merge}
  BuildRequires: %{rubygem pathspec}
  BuildRequires: %{rubygem hiera}
  %{?systemd_requires}
  Requires(pre): /usr/sbin/groupadd
  Requires(pre): /usr/sbin/useradd
  Requires(pre): %fillup_prereq
  Requires:      rubygem(%{mod_name})
  Requires:      tar
  Requires:      facter
  Provides:      puppet-agent = 1.8.1
  #!BuildIgnore: rubygem(%{mod_name})
:post_install: |-
  gemdir="%{buildroot}$(gem env gemdir)/gems/%{mod_full_name}"
  install -d -m 755 %{buildroot}%{_sbindir}
  install -d -m 750 %{buildroot}%{vardir}
  install -d -m 750 %{buildroot}%{logdir}
  install -d -m 755 %{buildroot}%{_fwdefdir}
  install -d -m 755 %{buildroot}%{_unitdir}/system
  install -D -m 644 $gemdir/conf/puppet.conf %{buildroot}%{confdir}/puppet.conf
  for dir in hieradata manifests modules; do
      install -d -m 755 %{buildroot}%{codedir}/environments/production/$dir
  done
  install -D -m 644 $gemdir/conf/environment.conf %{buildroot}%{codedir}/environments/production/environment.conf
  install -d -m 755 %{buildroot}%{codedir}/modules
  install -d %{buildroot}%{_localstatedir}/adm/fillup-templates
  install -m 644 $gemdir/ext/redhat/client.sysconfig %{buildroot}/%{_localstatedir}/adm/fillup-templates/sysconfig.puppet
  ln -sf service %{buildroot}/%{_sbindir}/rcpuppet
  sed -i -e 's:/opt/puppetlabs/puppet:/usr:' $gemdir/ext/systemd/puppet.service
  install -m 644 $gemdir/ext/systemd/puppet.service  %{buildroot}%{_unitdir}/system/puppet.service
  install -m 644 %{SOURCE2} %{buildroot}/%{_fwdefdir}/puppet

  for mandirX in $(ls $gemdir/man); do
      for manpage in $(ls $gemdir/man/$mandirX); do
          install -Dm0444 $gemdir/man/$mandirX/$manpage %{buildroot}/%{_mandir}/$mandirX/$manpage
      done
  done

  %fdupes %{buildroot}%{_prefix}

  # create /var/run/puppetlabs directory
  mkdir -p %{buildroot}%{_tmpfilesdir}
  cat > %{buildroot}%{_tmpfilesdir}/puppet.conf <<EOF
  d /var/run/puppetlabs 0755 root root -
  EOF

  # Install vim syntax files
  install -Dp -m 644 $gemdir/ext/vim/ftdetect/%{mod_name}.vim %{buildroot}%{vim_data_dir}/ftdetect/%{mod_name}.vim
  install -Dp -m 644 $gemdir/ext/vim/ftplugin/%{mod_name}.vim %{buildroot}%{vim_data_dir}/ftplugin/%{mod_name}.vim
  install -Dp -m 644 $gemdir/ext/vim/indent/%{mod_name}.vim %{buildroot}%{vim_data_dir}/indent/%{mod_name}.vim
  install -Dp -m 644 $gemdir/ext/vim/syntax/%{mod_name}.vim %{buildroot}%{vim_data_dir}/syntax/%{mod_name}.vim

  # Install emacs mode files
  emacsdir=%{buildroot}%{_datadir}/emacs/site-lisp
  install -Dp -m 644 $gemdir/ext/emacs/puppet-mode.el $emacsdir/puppet-mode.el
  install -Dp -m 644 $gemdir/ext/emacs/puppet-mode-init.el $emacsdir/site-start.d/puppet-mode-init.el

  # The users should use upstream puppetserver package so we need to create symlinks to make puppetserver working with our agent
  mkdir -p %{buildroot}/opt/puppetlabs/puppet/{bin,lib/ruby/vendor_ruby}
  ln -s /usr/bin/puppet %{buildroot}/opt/puppetlabs/puppet/bin
  VENDOR_RUBY_PATH=$(gem env gemdir | sed 's/gems/vendor_ruby/')
  ln -s $VENDOR_RUBY_PATH/facter* %{buildroot}/opt/puppetlabs/puppet/lib/ruby/vendor_ruby
  for file in deep_merge hiera rgen pathspec; do
      ln -s $(gem env gemdir)/gems/$file-*/lib/${file}* %{buildroot}/opt/puppetlabs/puppet/lib/ruby/vendor_ruby
  done
  for file in hiera_puppet.rb puppet puppet.rb puppet_x.rb semver.rb; do
      ln -s $(gem env gemdir)/gems/%{mod_full_name}/lib/$file %{buildroot}/opt/puppetlabs/puppet/lib/ruby/vendor_ruby
  done
  # /opt/puppetlabs/lib/ruby/vendor_ruby/hiera directory needs to contain files from both the hiera and the puppet packages
  rm %{buildroot}/opt/puppetlabs/puppet/lib/ruby/vendor_ruby/hiera
  install -d -m 755 %{buildroot}/opt/puppetlabs/puppet/lib/ruby/vendor_ruby/hiera
  for file in $(ls %{buildroot}/$(gem env gemdir)/gems/hiera-*/lib/hiera/); do
      ln -s $(gem env gemdir)/gems/hiera-*/lib/hiera/${file} %{buildroot}/opt/puppetlabs/puppet/lib/ruby/vendor_ruby/hiera/
  done
  for file in $(ls $gemdir/lib/hiera); do
      ln -s $(gem env gemdir)/gems/%{mod_full_name}/lib/hiera/$file %{buildroot}/opt/puppetlabs/puppet/lib/ruby/vendor_ruby/hiera/
  done

  for gem in $(/usr/bin/ruby-find-versioned gem) ; do
      # Remove those dirs as they cause strange runtime dependencies
      gemdir="%{buildroot}$($gem env gemdir)/gems/%{mod_full_name}"
      rm -rf $gemdir/ext/{debian,gentoo,solaris,ips}
      # Remove bundled libraries
      rm -rf $gemdir/lib/puppet/vendor/{*rgen*,*deep_merge*,*pathspec*}
  done
:scripts:
  :pre: |-
    %service_add_pre puppet.service
  :preun: |-
    %service_del_preun puppet.service
  :postun: |-
    %service_del_postun puppet.service
  :post: |-
    %tmpfiles_create %{_tmpfilesdir}/puppet.conf
    %fillup_only -n puppet
    %service_add_post puppet.service
:filelist: |-
  %dir %{_sysconfdir}/puppetlabs
  %dir %{confdir}
  %{codedir}
  %config(noreplace) %{confdir}/%{mod_name}.conf
  %config(noreplace) %{codedir}/environments/production/environment.conf
  %config(noreplace) %{_fwdefdir}/%{mod_name}
  %{_mandir}/man?/*
  %{_sbindir}/rcpuppet
  %{_unitdir}/system/%{mod_name}.service
  %{_localstatedir}/adm/fillup-templates/sysconfig.puppet
  %dir %{_localstatedir}/log/puppetlabs
  %attr(0750,root,root) %dir %{logdir}
  %dir %{_localstatedir}/cache/puppetlabs
  %attr(0750,root,root) %dir %{vardir}
  %{_tmpfilesdir}/puppet.conf
  %dir /opt/puppetlabs
  /opt/puppetlabs/puppet
:custom_pkgs:
  :vim:
    :preamble: |-
      Summary:     Vim syntax files for Puppet manifests
      Group:       Productivity/Text/Editors
      Requires:    vim
      BuildArch:   noarch
      Supplements: packageand(vim:%{name})
      Provides:    puppet-vim = %{version}-%{release}
      Obsoletes:   puppet-vim < %{version}-%{release}
    :description: Vim ftdetect and syntax files for Puppet manifests
    :filelist: |-
      %dir %{vim_data_dir}/ftdetect
      %{vim_data_dir}/ftdetect/%{mod_name}.vim
      %{vim_data_dir}/ftplugin/%{mod_name}.vim
      %{vim_data_dir}/indent/%{mod_name}.vim
      %{vim_data_dir}/syntax/%{mod_name}.vim
  :emacs:
    :preamble: |-
      Summary:     Emacs syntax files for Puppet
      Group:       Productivity/Text/Editors
      Requires:    emacs
      BuildArch:   noarch
      Supplements: packageand(emacs:%{name})
      Provides:    puppet-emacs = %{version}-%{release}
    :description: Emacs syntax files for Puppet manifests
    :filelist: |-
      # emacs-mode files
      %{_datadir}/emacs/site-lisp/puppet-mode.el
      %{_datadir}/emacs/site-lisp/site-start.d/puppet-mode-init.el
