# File: irtrans-server.spec

# Copyright (C) 2019, 2022 Van de Bugger.
#
# Copying and distribution of this file, with or without modification, are permitted in any medium
# without royalty provided the copyright notice and this notice are preserved. This file is offered
# as-is, without any warranty.

# TODO: Clarify license for the following files:
#   firmware.c
#   ftd2xx.h

Source1000:     vdb.lua
%include        %{S:1000}

%global ver     6.10.23
%global rel     0.vdb.3

#global _default_patch_fuzz 2

Name:           irtrans-server
Version:        %{ver}
Release:        %{rel}%{?dist}
Summary:        IRTrans server
Group:          System Environment/Daemon
License:        BSD-3-Clause
URL:            http://www.irtrans.de
Source0:        http://www.irtrans.de/download/Server/Linux/irserver-src.tar.gz
Source1:        http://www.irtrans.de/download/Docs/2013/Softwarehandbuch_EN.pdf
Source10:       %{name}.sh
Source11:       irtrans.service
Source12:       %{name}.service
Source13:       irtrans-lirc.service
Source14:       %{name}.udev.rules
Source15:       %{name}-lg-tv.rem

%global services    irtrans.service %{name}.service irtrans-lirc.service

#
#   Patches
#
# Fix compiler warnings:
Patch0:         irserver-6.10.23-warnings.vdb.patch
# Add newline to the end of usage message, print usage to stdout:
Patch1:         irserver-6.10.23-usage.vdb.patch
# Move lircd socket file to /run/irtrans/lircd:
Patch2:         irserver-6.10.23-lircd-location.vdb.patch
# irserver renamed to irtrans-server:
Patch3:         irserver-6.10.23-irtrans-server.vdb.patch
# Add buttons * and # to Mediacenter remote definition.
Patch4:         irserver-6.10.23-remote-mediacenter.vdb.patch
# Let "make irserver_noccf" build for current platform.
Patch5:         irserver-6.10.23-bitness.vdb.patch

BuildRequires:  gcc
BuildRequires:  make
BuildRequires:  pkgconfig(libzip)
BuildRequires:  pkgconfig(zlib)

%if 0%{?fedora:1}%{?rhel:1}
BuildRequires:  systemd-rpm-macros
%endif

Requires(pre): %{_bindir}/getent
Requires(pre): %{_sbindir}/groupadd
Requires(pre): %{_sbindir}/usermod
Requires(pre): %{_sbindir}/useradd

%description
IR Trans is an infra-red receiver (and/or transmitter) manufactured by IRTrans
GmbH <https://irtrans.de>. It could be an external device or be integrated
into PC case (for example, Zalman HD 160 Home Theatre PC Encolsure has
built-in IR Trans receiver).

This package includes software required for using IR Trans. It built from
the source code published by the manifacturer. The original source code is
patched to drop proprietary component and bundled libraries; udev rules and
systemd unit files are added for better integration with modern GNI/Linux
distros.

%prep
%setup -q -c %{name}-%{ver}
# Drop bundled zlib, libzip and ccf.o, I'll use system libs instead:
find . -name '*.o' -delete
find . -name '*.a' -delete
%{__rm} zip.h zipconf.h
# Drop ccf.rem, it requires binary object file:
%{__rm} remotes/ccf.rem
# Drop some source files:
%{__rm} stdint.h    # Use system one.
%{__rm} WinTypes.h  # License is not specified; not actually used on Linux.
%{__rm} resource.h  # Windows-specific file.
%patch0 -p1
%patch1 -p1
%patch2 -p1
%patch3 -p1
%patch4 -p1
%patch5 -p1

%build
ZFLAGS=$( pkg-config --cflags zlib )
ZLIBS=$( pkg-config --libs zlib )
ZIPFLAGS=$( pkg-config --cflags libzip )
ZIPLIBS=$( pkg-config --libs libzip )
%{make_build} CFLAGS="-g ${ZFLAGS} ${ZIPFLAGS}" LIBS="${ZLIBS} ${ZIPLIBS}" irserver_noccf
    # -g: Turn debug insformation on, otherwise rmpbuild can't build debuginfo rpm and fails.
%{__cp} "irserver" "%{name}.bin"

{
    echo 's|%%{name}|%{name}|g'
    echo 's|%%{_bindir}|%{_bindir}|g'
    echo 's|%%{_sbindir}|%{_sbindir}|g'
    echo 's|%%{_libexecdir}|%{_libexecdir}|g'
    echo 's|%%{_rundir}|%{_rundir}|g'
} > subst.sed

subst() {
    local rc
    %{__sed} -f subst.sed $1 > $2
    # Make sure the service file does not have unexpanded macros:
    %{__grep} -E -e '%%{.*}' $2 || rc=$?
    [[ $rc -eq 1 ]] # grep should fail.
}

subst "%{S:10}" "%{name}"
subst "%{S:11}" "irtrans.service"
subst "%{S:12}" "%{name}.service"
subst "%{S:13}" "irtrans-lirc.service"
subst "%{S:14}" "90-irtrans.rules"
cp    "%{S:15}" remotes/lg-tv.rem

%install
%_install_D -m u=rwx,go=rx -t "%{buildroot}%{_sbindir}" "%{name}"
%_install_D -m u=rwx,go=rx -t "%{buildroot}%{_libexecdir}/irtrans" "%{name}.bin"
%_install_D -m u=rw,go=r   -t "%{buildroot}%{_unitdir}" %{services}
%_install_D -m u=rw,go=r   -t "%{buildroot}%{_sysconfdir}/udev/rules.d" "90-irtrans.rules"
%_install_D -m u=rw,go=r   -t "%{buildroot}%{_datadir}/irtrans/remotes" remotes/*.rem
%_install_D -m u=rw,go=r   -t "%{buildroot}%{_pkgdocdir}" "%{S:1}"

%files
%defattr(-, root, root, -)
          %{_sbindir}/%{name}
%dir      %{_libexecdir}/irtrans
          %{_libexecdir}/irtrans/*
          %{_unitdir}/*
          %{_sysconfdir}/udev/rules.d/90-irtrans.rules
%dir      %{_datadir}/irtrans
%dir      %{_datadir}/irtrans/remotes
          %{_datadir}/irtrans/remotes/*
%dir %doc %{_pkgdocdir}
     %doc %{_pkgdocdir}/*

%pre
#   Add irtrans group.
rc=0
%{_bindir}/getent group irtrans > /dev/null || rc=$?
case $rc in
    0)  # Do nothing, group exists.
        ;;
    2)  # Group does not exists. Add the group.
        %{_sbindir}/groupadd -r irtrans
        ;;
    *)  echo "Can't check group existence: getent exited with status $rc" >&2
        exit $rc
        ;;
esac
#   Add irtrans user.
#   To let irtrans user access the /dev/irtrans (/dev/ttyUSB0) device, there are two options:
#       1. Use udev/rules.d/irtrans.rules to set group of the device to irtrans.
#       2. Include irtrans user to dialout group.
#   The second option assumes `irtrans` user is able to access any tty device, while the first
#   option looks more restrictive. Let's use the first one.
rc=0
%{_bindir}/getent passwd irtrans > /dev/null || rc=$?
args=(                                              \
    -g irtrans                                      \
    -d %{_rundir}/irtrans                           \
    -s %{_sbindir}/nologin                          \
    -c "IRTrans daemon user, runs irtrans-server"   \
    irtrans                                         \
)
case $rc in
    0)  # User already exists.
        %{_sbindir}/usermod "${args[@]}"
        ;;
    2)  # User does not exists. Add the user.
        %{_sbindir}/useradd -r "${args[@]}"
        ;;
    *)  echo "Can't check user existence: getent exited with status $rc" >&2
        exit $rc
        ;;
esac

# Note: macros systemd_* do not handle upgrading renamed service(s).

%post
%systemd_post %{services}

%preun
%systemd_preun %{services}

%postun
%systemd_postun_with_restart %{services}

%changelog
* Sun Nov 27 2022 Van de Bugger - 6.10.23-0.vdb.3
- Patch added: many (not all) compiler warnings eliminated.

* Wed Feb  9 2022 Van de Bugger - 6.10.23-0.vdb.2
- lg-tv.rem remote description file added.
- Minor fixes in spec.

* Thu Oct 24 2019 Van de Bugger - 6.10.23-0.vdb.1
- irtrans server sources updated to 6.10.23.

* Thu Mar 14 2013 Van de Bugger - 0.1-1
- Initial release.

# end of file #