%if 0%{?fedora} || 0%{?rhel} >= 9
%global scons scons
%else
%global scons scons-3
%endif

Name:           gpsd-latest
Version:        3.25
Release:        3%{?dist}
Epoch:          1
Summary:        Service daemon for mediating access to a GPS

License:        BSD-2-Clause
URL:            https://gpsd.gitlab.io/gpsd/index.html
Source0:        https://download-mirror.savannah.gnu.org/releases/gpsd/gpsd-%{version}.tar.gz
Source11:       gpsd.sysconfig

BuildRequires:  dbus-devel
BuildRequires:  dbus-glib-devel
BuildRequires:  ncurses-devel
BuildRequires:  xmlto
BuildRequires:  python3-devel
BuildRequires:  python3-setuptools
BuildRequires:  gtk3-devel
BuildRequires:  python3-scons
BuildRequires:  python3-gobject
BuildRequires:  python3-cairo
BuildRequires:  python3-pyserial
BuildRequires:  desktop-file-utils
BuildRequires:  bluez-libs-devel
BuildRequires:  pps-tools-devel
BuildRequires:  /usr/bin/c++
BuildRequires:  systemd
%ifnarch s390 s390x
BuildRequires:  libusb1-devel
%endif

Requires:       udev
%{?systemd_requires}

Conflicts:      gpsd < %{epoch}:%{version}-%{release}
Conflicts:      gpsd-clients < %{epoch}:%{version}-%{release}

%description 
gpsd is a service daemon that mediates access to a GPS sensor
connected to the host computer by serial or USB interface, making its
data on the location/course/velocity of the sensor available to be
queried on TCP port 2947 of the host computer.  With gpsd, multiple
GPS client applications (such as navigational and war-driving software)
can share access to a GPS without contention or loss of data.  Also,
gpsd responds to queries with a format that is substantially easier to
parse than NMEA 0183.  

This package contains the gpsd daemon and client applications.
Development files and python module are not provided for this version
of gpsd.

%prep
%setup -q -n gpsd-%{version}

# Add path to the private python gps module
sed -i 's|\( *\)\(import gps\)$|\1sys.path.insert(1, "%{_libdir}/gpsd%{version}")\n\1\2|' \
	 clients/*.py.in

# don't try reloading systemd when installing in the build root
sed -i 's|systemctl daemon-reload|true|' SConscript

iconv -f iso8859-1 -t utf8 NEWS > NEWS_ && mv NEWS_ NEWS

%build
export CCFLAGS="%{optflags}"
# scons ignores LDFLAGS. LINKFLAGS partially work (some flags like
# -spec=... are filtered)
export LINKFLAGS="%{__global_ldflags}"

# breaks with %%{_smp_mflags}
%{scons} \
    dbus_export=yes \
    systemd=yes \
    qt=no \
    debug=yes \
    leapfetch=no \
    prefix="" \
    sysconfdif=%{_sysconfdir} \
    bindir=%{_bindir} \
    includedir=%{_includedir} \
    libdir=%{_libdir}/gpsd%{version} \
    sbindir=%{_sbindir} \
    mandir=%{_mandir} \
    mibdir=%{_docdir}/gpsd \
    docdir=%{_docdir}/gpsd \
    pkgconfigdir=%{_libdir}/pkgconfig \
    icondir=%{_datadir}/gpsd \
    udevdir=$(dirname %{_udevrulesdir}) \
    unitdir=%{_unitdir} \
    target_python=python3 \
    python_shebang=%{python3} \
    python_libdir=%{_libdir}/gpsd%{version} \
    build

%install
# avoid rebuilding
export CCFLAGS="%{optflags}"
export LINKFLAGS="%{__global_ldflags}"

DESTDIR=%{buildroot} %{scons} install systemd_install udev-install

# use the old name for udev rules
mv %{buildroot}%{_udevrulesdir}/{25,99}-gpsd.rules

install -d -m 0755 %{buildroot}%{_sysconfdir}/sysconfig
install -p -m 0644 %{SOURCE11} \
    %{buildroot}%{_sysconfdir}/sysconfig/gpsd

# Install the .desktop files
desktop-file-install \
    --dir %{buildroot}%{_datadir}/applications \
    packaging/X11/xgps.desktop
desktop-file-install \
    --dir %{buildroot}%{_datadir}/applications \
    packaging/X11/xgpsspeed.desktop

# Missed in scons install 
install -p -m 0755 gpsinit %{buildroot}%{_sbindir}

# Remove shebang and fix permissions
sed -i '/^#!.*python/d' %{buildroot}%{_libdir}/gpsd%{version}/gps/{aio,}gps.py
chmod 644 %{buildroot}%{_libdir}/gpsd%{version}/gps/gps.py

# Remove unpackaged files
rm -f %{buildroot}%{_libdir}/gpsd%{version}/lib{gps*.so,gps.so.*}
rm -f %{buildroot}%{_libdir}/gpsd%{version}/*.egg-info
rm -rf %{buildroot}%{_libdir}/gpsd%{version}/pkgconfig
rm -rf %{buildroot}%{_includedir}
rm -rf %{buildroot}%{_mandir}/man{3,5}

rm -rf %{buildroot}%{_docdir}/gpsd

%post
%systemd_post gpsd.service gpsd.socket

%preun
%systemd_preun gpsd.service gpsd.socket

%postun
# Don't restart the service
%systemd_postun gpsd.service gpsd.socket

%files
%doc README.adoc NEWS
%license COPYING
%config(noreplace) %{_sysconfdir}/sysconfig/gpsd
%{_sbindir}/gpsd
%{_sbindir}/gpsdctl
%{_sbindir}/gpsinit
%{_bindir}/gpsmon
%{_bindir}/gpsctl
%{_bindir}/ntpshmmon
%{_bindir}/ppscheck
%{_unitdir}/gpsd.service
%{_unitdir}/gpsd.socket
%{_unitdir}/gpsdctl@.service
%{_udevrulesdir}/*.rules
%{_mandir}/man8/gpsd.8*
%{_mandir}/man8/gpsdctl.8*
%{_mandir}/man8/gpsinit.8*
%{_mandir}/man8/ppscheck.8*
%{_mandir}/man1/gpsmon.1*
%{_mandir}/man1/gpsctl.1*
%{_mandir}/man1/ntpshmmon.1*

%{_libdir}/gpsd%{version}/libgpsdpacket.so.*
%{_libdir}/gpsd%{version}/gps

%{_bindir}/cgps
%{_bindir}/gegps
%{_bindir}/gps2udp
%{_bindir}/gpscat
%{_bindir}/gpscsv
%{_bindir}/gpsdebuginfo
%{_bindir}/gpsdecode
%{_bindir}/gpspipe
%{_bindir}/gpsplot
%{_bindir}/gpsprof
%{_bindir}/gpsrinex
%{_bindir}/gpssnmp
%{_bindir}/gpssubframe
%{_bindir}/gpxlogger
%{_bindir}/lcdgps
%{_bindir}/xgps
%{_bindir}/xgpsspeed
%{_bindir}/gpsfake
%{_bindir}/ubxtool
%{_bindir}/zerk
%{_mandir}/man1/gegps.1*
%{_mandir}/man1/gps.1*
%{_mandir}/man1/gps2udp.1*
%{_mandir}/man1/gpscsv.1*
%{_mandir}/man1/gpsdebuginfo.1*
%{_mandir}/man1/gpsdecode.1*
%{_mandir}/man1/gpspipe.1*
%{_mandir}/man1/gpsplot.1*
%{_mandir}/man1/gpsprof.1*
%{_mandir}/man1/gpsrinex.1*
%{_mandir}/man1/gpssnmp.1*
%{_mandir}/man1/gpssubframe.1*
%{_mandir}/man1/gpxlogger.1*
%{_mandir}/man1/lcdgps.1*
%{_mandir}/man1/xgps.1*
%{_mandir}/man1/xgpsspeed.1*
%{_mandir}/man1/cgps.1*
%{_mandir}/man1/gpscat.1*
%{_mandir}/man1/gpsfake.1*
%{_mandir}/man1/ubxtool.1*
%{_mandir}/man1/zerk.1*
%{_datadir}/applications/*.desktop
%dir %{_datadir}/gpsd
%{_datadir}/gpsd/gpsd-logo.png

%changelog
* Tue Jun 13 2023 Miroslav Lichvar <mlichvar@redhat.com> - 1:3.25-3
- move libs and python module to private directory
- drop subpackages

* Thu May 11 2023 Miroslav Lichvar <mlichvar@redhat.com> - 1:3.25-2
- fix issues reported by rpmlint

* Tue May 9 2023 Miroslav Lichvar <mlichvar@redhat.com> - 1:3.25-1
- initial release for EPEL based on Fedora gpsd package