#
# spec file for package vagrant
#
# Copyright (c) 2018 SUSE LINUX GmbH, Nuernberg, Germany.
# Copyright (c) 2012 Laurent Bigonville <bigon@debian.org>, License GPL-2.0+
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via https://bugs.opensuse.org/
#


%define mod_name vagrant
%define mod_full_name %{mod_name}-%{version}
#
# Use
#
%global vim_data_dir %{_datadir}/vim/site/plugin/

Name:           vagrant
Version:        2.2.2
Release:        0
Summary:        Tool for building and distributing virtualized development environments
License:        MIT
Group:          Development/Languages/Ruby
Url:            https://github.com/hashicorp/vagrant
Source0:        https://github.com/hashicorp/vagrant/archive/v%{version}.tar.gz#/%{mod_name}-%{version}.tar.gz
Source11:       vagrant.1
Source98:       README.SUSE
Source99:       %{name}-rpmlintrc
Provides:       rubygem-vagrant = %{version}
Obsoletes:      rubygem-vagrant < %{version}
Recommends:     vagrant-libvirt
#
Patch2:         0002-bin-vagrant-silence-warning-about-installer.patch
Patch3:         0003-Use-a-private-temporary-dir.patch
Patch4:         0004-linux-cap-halt-don-t-wait-for-shutdown-h-now-to-fini.patch
Patch9:         0009-plugins-don-t-abuse-require_relative.patch
#
#
# try building with ruby2.4
Patch23:        0023-vbox-fix-package.patch
Patch24:        0024-do-not-depend-on-wdm.patch
Patch25:        0025-do-not-abuse-relative-paths-in-docker-plugin.patch
Patch26:        0026-do-not-abuse-relative-paths-in-plugins.patch

BuildRoot:      %{_tmppath}/%{name}-%{version}-build
# force only one ruby version
%define rb_build_versions %rb_default_ruby
%define rb_build_abi %rb_default_ruby_abi
#  s.required_ruby_version     = "~> 2.2", "< 2.6"
BuildRequires:  %{ruby >= 2.2.0}
# temporary override, so that we can build with ruby 2.6
# FIXME: this will be the default with vagrant >= 2.2.3, remove the comment then
BuildRequires:  %{ruby < 2.7}
#
#
#
BuildRequires:  %{rubygem bundler}
#  s.add_dependency "bcrypt_pbkdf", "~> 1.0.0"
BuildRequires:  %{rubygem bcrypt_pbkdf:1.0.0 }
#  s.add_dependency "childprocess", "~> 0.6.0"
BuildRequires:  %{rubygem childprocess:0.6 }
#  s.add_dependency "ed25519", "~> 1.2.4"
BuildRequires:  %{rubygem ed25519:1.2.4 }
#  s.add_dependency "erubis", "~> 2.7.0"
BuildRequires:  %{rubygem erubis:2.7 }
#  s.add_dependency "i18n", "~> 1.1.1"
BuildRequires:  %{rubygem i18n:1.1.1 }
#  s.add_dependency "listen", "~> 3.1.5"
BuildRequires:  %{rubygem listen:3.1.5 }
#  s.add_dependency "hashicorp-checkpoint", "~> 0.1.5"
BuildRequires:  %{rubygem hashicorp-checkpoint:0.1.5 }
#  s.add_dependency "log4r", "~> 1.1.9", "< 1.1.11"
BuildRequires:  %{rubygem log4r < 1.1.11 }
BuildRequires:  %{rubygem log4r >= 1.1.9 }
#  s.add_dependency "net-ssh", "~> 5.0.0"
BuildRequires:  %{rubygem net-ssh:5.0}
#  s.add_dependency "net-sftp", "~> 2.1"
BuildRequires:  %{rubygem net-sftp:2.1 }
#  s.add_dependency "net-scp", "~> 1.2.0"
BuildRequires:  %{rubygem net-scp:1.2 }
#  s.add_dependency "rb-kqueue", "~> 0.2.0"
BuildRequires:  %{rubygem rb-kqueue:0.2 }
#  s.add_dependency "rest-client", ">= 1.6.0", "< 3.0"
BuildRequires:  %{rubygem rest-client < 3.0}
BuildRequires:  %{rubygem rest-client >= 1.6}
#  s.add_dependency "rubyzip", "~> 1.2.2"
BuildRequires:  %{rubygem rubyzip:1.2.2}
# Intentionally removed, wdm only works on Windows
# BuildRequires:  %{rubygem wdm }
#  s.add_dependency "winrm", "~> 2.1"
BuildRequires:  %{rubygem winrm:2.1 }
#  s.add_dependency "winrm-fs", "~> 1.0"
BuildRequires:  %{rubygem winrm-fs:1 }
#  s.add_dependency "winrm-elevated", "~> 1.1"
BuildRequires:  %{rubygem winrm-elevated:1.1 }
#  s.add_dependency "vagrant_cloud", "~> 2.0.0"
BuildRequires:  %{rubygem vagrant_cloud >= 2.0.0 }
BuildRequires:  %{rubygem vagrant_cloud < 2.1.0 }

# devel dependencies:
#  s.add_development_dependency "rake", "~> 12.0.0"
BuildRequires:  %{rubygem rake:12.0 }
#  s.add_development_dependency "rspec", "~> 3.5.0"
BuildRequires:  %{rubygem rspec:3.5 }
#  s.add_development_dependency "rspec-its", "~> 1.2.0"
BuildRequires:  %{rubygem rspec-its:1.2 }
#  s.add_dependency "ruby_dep", "<= 1.3.1"
BuildRequires:  %{rubygem ruby_dep <= 1.3.1 }
#  s.add_development_dependency "webmock", "~> 2.3.1"
BuildRequires:  %{rubygem webmock:2.3 }
#  s.add_development_dependency "fake_ftp", "~> 0.1.1"
BuildRequires:  %{rubygem fake_ftp:0.1.1 }

# Prevent have choice for rubygem(ruby:2.1.0:mime-types) >= 1.16
BuildRequires:  %{rubygem mime-types >= 2}
# Prevent have choice for rubygem(ruby:2.4.0:addressable) >= 2.3.6
BuildRequires:  %{rubygem addressable >= 2.4 }
# Prevent have choice for rubygem(ruby:2.4.0:builder) >= 2.1.2
BuildRequires:  %{rubygem builder >= 3.1 }

BuildRequires:  ruby-macros >= 5

#
#
#  s.add_dependency "bcrypt_pbkdf", "~> 1.0.0"
Requires:       %{rubygem bcrypt_pbkdf:1.0.0 }
#  s.add_dependency "childprocess", "~> 0.6.0"
Requires:       %{rubygem childprocess:0.6}
#   s.add_dependency "ed25519", "~> 1.2.4"
Requires:       %{rubygem ed25519}
# Prevent have choice for rubygem(ruby:2.2.0:erubis)
#  s.add_dependency "erubis", "~> 2.7.0"
Requires:       %{rubygem erubis:2.7}
Requires:       %{rubygem i18n}
Requires:       %{rubygem listen}
Requires:       %{rubygem hashicorp-checkpoint}
Requires:       %{rubygem log4r}
Requires:       %{rubygem net-ssh}
Requires:       %{rubygem net-sftp}
# Prevent have choice for rubygem(ruby:2.2.0:net-scp)
Requires:       %{rubygem net-scp >= 1.2}
Requires:       %{rubygem rb-kqueue}
Requires:       %{rubygem rest-client}
Requires:       %{rubygem winrm}
Requires:       %{rubygem winrm-fs}
Requires:       %{rubygem winrm-elevated}
Requires:       %{rubygem vagrant_cloud}
# We don't require rubygem mime-types since it's pulled in transitively
#
Requires:       bsdtar
Requires:       curl
Requires:       openssh
#
%description
Vagrant is a tool for building and distributing virtualized development
environments.

%package vim
Summary:        Vagrantfile syntax files for the vim editor
Group:          Development/Languages/Ruby
Supplements:    packageand(vagrant:vim)
BuildRequires:  vim
Requires:       vagrant = %{version}
Requires:       vim
BuildArch:      noarch

%description   vim
Optional dependency offering vim syntax files for Vagrantfile

%package emacs
Summary:        Vagrantfile syntax files for the emacs editor
Group:          Development/Languages/Ruby
Supplements:    packageand(vagrant:emacs_program)
BuildRequires:  emacs-nox
Requires:       emacs_program
Requires:       vagrant = %{version}
BuildArch:      noarch

%description   emacs
Optional dependency offering emacs syntax files for Vagrantfile

%package bash-completion
Summary:        Vagrant bash autocompletion
Group:          Development/Languages/Ruby
Supplements:    packageand(vagrant:bash)
BuildRequires:  bash
BuildRequires:  bash-completion
Requires:       bash
Requires:       bash-completion
Requires:       vagrant = %{version}
BuildArch:      noarch

%description  bash-completion
Optional dependency offering bash completion for vagrant

%prep
%setup -q -n %{mod_full_name}
%patch2 -p1
%patch3 -p1
%patch4 -p1
%patch9 -p1
#
#
#
%patch23 -p1
%patch24 -p1
%patch25 -p1
%patch26 -p1

cp %{SOURCE98} .

%build
mv %{mod_name}.gemspec %{mod_full_name}.gemspec
%gem_build
mv %{mod_full_name}.gem %{_sourcedir}

%install
# Tumbleweed OR Leap 42.2+
%if %suse_version >= 1320 || 0%{?sle_version} >= 120200
%gem_install -f --no-symlink-binaries
%else
%gem_install -f
%endif

mkdir -p %{buildroot}/%{_mandir}/man1
install -m 644 %{SOURCE11} %{buildroot}%{_mandir}/man1/vagrant.1

install -D -d -m 0755                           \
  %{buildroot}%{_sysconfdir}/bash_completion.d/ \
  %{buildroot}%{_datadir}/emacs/site-lisp/

install -d -m 0777 %{buildroot}%{vim_data_dir}/

install -d -m 0755 %{buildroot}%{_datadir}vagrant-plugins/ \
	%{buildroot}%{_datadir}vagrant-plugins/plugins.d

mv %{buildroot}%{gem_base}/gems/%{mod_full_name}/contrib/bash/completion.sh  %{buildroot}%{_sysconfdir}/bash_completion.d/%{mod_name}.sh
mv %{buildroot}%{gem_base}/gems/%{mod_full_name}/contrib/vim/vagrantfile.vim %{buildroot}%{vim_data_dir}/%{mod_name}.vim
mv %{buildroot}%{gem_base}/gems/%{mod_full_name}/contrib/emacs/vagrant.el    %{buildroot}%{_datadir}/emacs/site-lisp/%{mod_name}.el

# Tumbleweed OR Leap 42.2+
%if %suse_version >= 1320 || 0%{?sle_version} >= 120000
mv -v %{buildroot}%{_bindir}/vagrant.%{rb_default_ruby_suffix} %{buildroot}%{_bindir}/vagrant
%endif

mkdir -p %{buildroot}%{_datadir}/%{name}
mv %{buildroot}%{gem_base}/gems/%{mod_full_name}/keys %{buildroot}%{_datadir}/%{name}/keys
mv %{buildroot}%{gem_base}/gems/%{mod_full_name}/plugins %{buildroot}%{_datadir}/%{name}/plugins
mv %{buildroot}%{gem_base}/gems/%{mod_full_name}/templates %{buildroot}%{_datadir}/%{name}/templates
chmod -x %{buildroot}%{_datadir}/%{name}/templates/locales/en.yml
install -m 644 version.txt %{buildroot}%{_datadir}/%{name}

mkdir -p %{buildroot}%{_datadir}/%{name}-plugins/plugins.d
echo "{}" > %{buildroot}%{_datadir}/%{name}-plugins/plugins.json

rm -f %{buildroot}%{gem_base}/gems/%{mod_full_name}/.travis.yml
rm -f %{buildroot}%{gem_base}/gems/%{mod_full_name}/.gitignore
rm -f %{buildroot}%{gem_base}/gems/%{mod_full_name}/test/vagrant-spec/boxes/.keep
rm -f %{buildroot}%{gem_base}/gems/%{mod_full_name}/bin/vagrant.orig
rm -f %{buildroot}%{_bindir}/vagrant.orig.%{rb_default_ruby_suffix}
rm -f %{buildroot}%{gem_base}/gems/%{mod_full_name}/lib/vagrant/util.rb.orig

# SLE 12
%if 0%{?suse_version} == 1315 && !0%{?is_opensuse}
rm -f %{buildroot}%{gem_base}/gems/%{mod_full_name}/lib/vagrant/box.rb.orig
rm -f %{buildroot}%{gem_base}/gems/%{mod_full_name}/%{name}.gemspec.orig
%endif

# change shebang in /usr/lib64/ruby/gems/*/gems/vagrant-2.1.1/.runner.sh
sed -i '1 s/^.*$/#!\/bin\/bash/' %{buildroot}%{gem_base}/gems/%{mod_full_name}/.runner.sh

# change shebang in /etc/bash_completion.d/vagrant.sh
sed -i '1d' %{buildroot}%{_sysconfdir}/bash_completion.d/%{mod_name}.sh
# move file to %{_datadir}/bash-completion/completions/ to avoid warnings
install -d 755 %{buildroot}%{_datadir}/bash-completion/completions/
mv -v %{buildroot}%{_sysconfdir}/bash_completion.d/%{mod_name}.sh %{buildroot}%{_datadir}/bash-completion/completions/

%post
# link directories to avoid the following error:
# /usr/lib64/ruby/2.5.0/pathname.rb:444:in `open': No such file or directory @ dir_initialize - /usr/lib64/ruby/gems/2.5.0/gems/vagrant-2.1.2/plugins (Errno::ENOENT)
ln -s %{_datadir}/%{name}/keys %{gem_base}/gems/%{mod_full_name}/keys
ln -s %{_datadir}/%{name}/plugins %{gem_base}/gems/%{mod_full_name}/plugins
ln -s %{_datadir}/%{name}/templates %{gem_base}/gems/%{mod_full_name}/templates

%postun
rm -f %{gem_base}/gems/%{mod_full_name}/keys
rm -f %{gem_base}/gems/%{mod_full_name}/plugins
rm -f %{gem_base}/gems/%{mod_full_name}/templates

%files
%defattr(-,root,root,-)
%doc CHANGELOG.md LICENSE README.SUSE
%{_bindir}/vagrant
%{gem_base}/cache/%{mod_full_name}.gem
%{gem_base}/doc/%{mod_full_name}/
%{gem_base}/gems/%{mod_full_name}/
%{gem_base}/specifications/%{mod_full_name}.gemspec
%{_datadir}/%{name}
%dir %{_datadir}/%{name}-plugins
%dir %{_datadir}/%{name}-plugins/plugins.d
%{_datadir}/%{name}-plugins/plugins.json
%{_mandir}/man1/vagrant.*

%files vim
%defattr(-,root,root,-)
%{vim_data_dir}/%{mod_name}.vim

%files emacs
%defattr(-,root,root,-)
%{_datadir}/emacs/site-lisp/%{mod_name}.el

%files bash-completion
%defattr(-,root,root,-)
%{_datadir}/bash-completion/completions/%{mod_name}.sh

%changelog