# Ubuntu called their own software nplan in the archive due to name conflicts
%global old_ubuntu_name nplan
# Ubuntu calls their own software netplan.io in the archive due to name conflicts
%global ubuntu_name netplan.io

# If the definition isn't available for python3_pkgversion, define it
%{?!python3_pkgversion:%global python3_pkgversion 3}

# If this isn't defined, define it
%{?!_systemdgeneratordir:%global _systemdgeneratordir /usr/lib/systemd/system-generators}

# Force auto-byte-compilation to Python 3
%global __python %{__python3}

#global snapdate 20180313
%global commit fefbe675c053fd7d7f0f231c0e0ebf263e8aa9bb
%global shortcommit %(c=%{commit}; echo ${c:0:7})

Name:           netplan
Version:        0.34%{?snapdate:+git%{snapdate}.%{shortcommit}}
Release:        0%{?dist}.2
Summary:        Network configuration tool using YAML

License:        GPLv3
URL:            http://netplan.io/

%if 0%{?snapdate}
Source0:        https://github.com/CanonicalLtd/%{name}/archive/%{commit}/%{name}-%{shortcommit}.tar.gz
%else
Source0:        https://github.com/CanonicalLtd/%{name}/archive/%{version}/%{name}-%{version}.tar.gz
%endif

# Backports from master
Patch0001:      0001-Makefile-Rework-to-be-more-distribution-agnostic-and.patch

BuildRequires:  pkgconfig(bash-completion)
BuildRequires:  pkgconfig(systemd)
BuildRequires:  pkgconfig(glib-2.0)
BuildRequires:  pkgconfig(yaml-0.1)
BuildRequires:  pkgconfig(uuid)
BuildRequires:  %{_bindir}/pandoc
BuildRequires:  python%{python3_pkgversion}-devel
# For tests
BuildRequires:  python%{python3_pkgversion}-coverage
BuildRequires:  python%{python3_pkgversion}-yaml
BuildRequires:  python%{python3_pkgversion}-pycodestyle
BuildRequires:  python%{python3_pkgversion}-pyflakes

# /usr/sbin/netplan is a Python 3 script that requires PyYAML
Requires:       python%{python3_pkgversion}-yaml

# netplan supports either systemd or NetworkManager as backends to configure the network
Requires:       systemd
%if 0%{?fedora} || 0%{?mageia} || 0%{?rhel} >= 8
Recommends:     NetworkManager
%endif

# For handling Wi-Fi networks
Requires:       wpa_supplicant

# Provide the package name that Ubuntu uses for it too...
Obsoletes:      %{old_ubuntu_name} < %{version}-%{release}
Provides:       %{old_ubuntu_name} = %{version}-%{release}
Provides:       %{old_ubuntu_name}%{?_isa} = %{version}-%{release}
Provides:       %{ubuntu_name} = %{version}-%{release}
Provides:       %{ubuntu_name}%{?_isa} = %{version}-%{release}

%description
netplan reads network configuration from /etc/nplan/*.yaml which are written by administrators,
installers, cloud image instantiations, or other OS deployments. During early boot, it generates
backend specific configuration files in /run to hand off control of devices to a particular
networking daemon.

Currently supported backends are systemd-networkd and NetworkManager.


%prep
%if 0%{?snapdate}
%autosetup -n %{name}-%{commit} -p1
%else
%autosetup -p1
%endif

# Drop -Werror
sed -e "s/-Werror//g" -i Makefile

%if 0%{?el7}
# Remove undefined options for RHEL 7
sed -e "s/-Werror=incompatible-pointer-types//g" -i Makefile
%endif

%build
%make_build CFLAGS="%{optflags}"


%install
%make_install ROOTPREFIX=%{_prefix}

# Pre-create the config directory
mkdir -p %{buildroot}%{_sysconfdir}/%{name}

%check
make check


%files
%license COPYING
%doc debian/changelog
%doc %{_docdir}/%{name}/
%{_sbindir}/%{name}
%{_datadir}/%{name}/
%{_unitdir}/%{name}*.service
%{_systemdgeneratordir}/%{name}
%{_mandir}/man5/%{name}.5*
%dir %{_sysconfdir}/%{name}
%{_prefix}/lib/%{name}/
%{_datadir}/bash-completion/completions/%{name}

%changelog
* Sun Mar 18 2018 Neal Gompa <ngompa13@gmail.com> - 0.34-0.2
- Backported upstreamed version of patch to fix build on Fedora

* Tue Mar 13 2018 Neal Gompa <ngompa13@gmail.com> - 0.34-0.1
- Update to 0.34

* Wed Mar  7 2018 Neal Gompa <ngompa13@gmail.com> - 0.33-0.1
- Rebase to 0.33

* Sat Nov  4 2017 Neal Gompa <ngompa13@gmail.com> - 0.30-1
- Rebase to 0.30

* Sun Jul  2 2017 Neal Gompa <ngompa13@gmail.com> - 0.23~17.04.1-1
- Initial packaging