%global debug_package %{nil}

Summary:	PiAware, software to send ADS-B messages to FlightAware
Name:		piaware
Version:	6.0
Release:	1%{?dist}
License:	BSD
URL:		https://github.com/flightaware/piaware
Source0:	https://github.com/flightaware/piaware/archive/v%{version}.tar.gz#/%name-%version.tar.gz
Source2:	%{name}-sysconfig
Source3:	%{name}-tmpfiles.conf
Source4:	%{name}-config

# Symlink to tcllauncher for piaware apps, rather than copying and renaming tcllauncher N times
Patch0:		piaware-lns.patch

# Force systemd detection
Patch1:		piaware-systemd.patch

# Look for faup1090 in /usr/libexec
Patch2:		piaware-3.7.0-faup1090.patch

# Enable sudo rule for netstat
Patch3:		piaware-sudo.patch

# Fix path to fa-mlat-client
Patch4:		piaware-mlat-client.patch

%{?systemd_requires}
BuildRequires:	systemd
BuildRequires:	gcc
BuildRequires:	openssl-perl systemd-units tcl
Requires:	tcllauncher tcltls itcl tcllib
Requires:	dump1090 mlat-client
Requires:	tcl(abi) = 8.6

%{!?tcl_version: %global tcl_version %(echo 'puts $tcl_version' | tclsh)}
%{!?tcl_sitelib: %global tcl_sitelib %{_datadir}/tcl%{tcl_version}}

%description
The basic aim of the piaware package is to forward data read from an ADS-B
receiver to FlightAware.

%prep
%autosetup -p1

%build
echo "Move along, nothing to see here..."

%install
mkdir -p %{buildroot}/usr/bin
%make_install

mkdir -p %{buildroot}%{_tmpfilesdir}
install -m 0644 %{SOURCE3} %{buildroot}%{_tmpfilesdir}/%{name}.conf
install -d -m 0755 %{buildroot}/run/%{name}/

mkdir -p %{buildroot}%{_localstatedir}/cache/%{name}
install -D -m640 %{SOURCE4} %{buildroot}/%{_sysconfdir}/%{name}/config
install -D -m644 %{SOURCE2} %{buildroot}/%{_sysconfdir}/sysconfig/piaware
install -D -m640 etc/piaware.sudoers %{buildroot}/%{_sysconfdir}/sudoers.d/piaware

# Put lib/* into tcl_sitelib, fix perms
mkdir -p %{buildroot}/%{tcl_sitelib}
find %{buildroot}/usr/lib -type f -exec chmod 644 {} \;
mv %{buildroot}/usr/lib/piaware_packages %{buildroot}/%{tcl_sitelib}/
mv %{buildroot}/usr/lib/fa_adept_codec   %{buildroot}/%{tcl_sitelib}/
mv %{buildroot}/usr/lib/piaware          %{buildroot}/%{tcl_sitelib}/
mv %{buildroot}/usr/lib/piaware-config   %{buildroot}/%{tcl_sitelib}/
mv %{buildroot}/usr/lib/piaware-status   %{buildroot}/%{tcl_sitelib}/
mv %{buildroot}/usr/lib/pirehose         %{buildroot}/%{tcl_sitelib}/


%pre
getent group piaware >/dev/null 2>&1 || groupadd -r piaware 2>&1 || :
getent passwd piaware >/dev/null 2>&1 || useradd -r -l -g piaware \
    -d %{_localstatedir}/empty/%{name} -s /sbin/nologin -c "PiAware daemon" piaware >/dev/null 2>&1 || :

%post
%systemd_post piaware.service
%systemd_post generate-pirehose-cert.service

%preun
%systemd_preun piaware.service
%systemd_preun generate-pirehose-cert.service

%postun
%systemd_postun_with_restart piaware.service
%systemd_postun_with_restart generate-pirehose-cert.service

%files
%license LICENSE.txt
%dir %attr(0750,piaware,piaware) %{_sysconfdir}/%{name}/
%config(noreplace) %attr(0640,piaware,piaware) %{_sysconfdir}/%{name}/config
%config(noreplace) %{_sysconfdir}/sysconfig/piaware
%config(noreplace) %{_sysconfdir}/sudoers.d/piaware
%{_bindir}/*
%{tcl_sitelib}/*
%{_mandir}/man1/*
%{_unitdir}/*.service
%{_tmpfilesdir}/%{name}.conf
%dir %attr(0755,piaware,piaware) %{_localstatedir}/cache/%{name}/
%dir /run/%{name}/

%changelog
* Sat Sep 04 2021 Greg Wildman <greg@techno.co.za> - 6.0-1
- Update to latest version

* Thu Oct 29 2020 Greg Wildman <greg@techno.co.za> - 4.0-1
- Update to latest version

* Tue Jun 02 2020 Greg Wildman <greg@techno.co.za> - 3.8.1-1
- Update to latest version

* Thu Sep 26 2019 Greg Wildman <greg@techno.co.za> - 3.7.2-1
- Update to latest version

* Sun Apr 21 2019 Greg Wildman <greg@techno.co.za> - 3.7.0.1-1
- Update to latest version

* Wed Sep 26 2018 Greg Wildman <greg@techno.co.za> - 3.6.3-1
- Update to latest version

* Sat Aug 18 2018 Greg Wildman <greg@techno.co.za> - 3.6.2-6
- Update to latest version
- Change to run under own user