%bcond_with systemd

%global _udevrulesdir /lib/udev/rules.d

# Don't provide or require anything from _docdir, per policy.
%global __provides_exclude_from ^%{_docdir}/.*$
%global __requires_exclude_from ^%{_docdir}/.*$

Summary:	The Linux Infrared Remote Control package.
Summary(ja):	赤外線リモートコントロールパッケージ
Name:		lirc
Version:	0.10.2
Release:	1%{?_dist_release}%{?with_systemd:.systemd}
Group:		system
Vendor:		Project Vine
Distribution:	Vine Linux
Packager:	owa, kazutaka

# lib/ciniparser* and lib/dictionary* are BSD, others GPLv2
License:	GPLv2 and BSD
URL:		https://www.lirc.org/
Source0:	https://prdownloads.sourceforge.net/lirc/%{name}-%{version}.tar.bz2
Source1:	lirc.init
Source2:	lircd.logrotate
Source3:	lirc.sysconfig
Source4:	99-remote-control-lirc.rules

Patch2:		0002-lirc-setup-Fix-crash-on-start-on-missing-lirc.config.patch

# Vine patch
Patch1000:	lirc-fix-build.patch

BuildRoot:	%{_tmppath}/%{name}-%{version}-root
BuildRequires:	perl
BuildRequires:	libusb1-devel
BuildRequires:	automake libtool
BuildRequires:	alsa-lib-devel
BuildRequires:  libXt-devel
BuildRequires:  libxslt
BuildRequires:	python3-devel
BuildRequires:  python3-rpm-macros
BuildRequires:	python3-setuptools
## Currently not supportd (yet) for Vine Linux
#BuildRequires:	libftdi-devel, libirman-devel
%if %{with systemd}
BuildRequires:	systemd-devel
%{?systemd_requires}
%else
Requires(post): /sbin/chkconfig
Requires(preun): /sbin/chkconfig
%endif

%description
LIRC stands for 'Linux Infra-red Remote Control'.

This package provides the daemons and some utilities to support
infra-red remote controls under Linux. 

%description -l ja
LIRC は 'Linux Infra-red Remote Control' の略です。

このパッケージには Linux で赤外線リモコンをサポートする為に
必要なデーモンといくつかのユーティリティが含まれています。


%package	devel
Summary:	Development files for LIRC
Summary(ja):	LIRCを用いた開発用のファイル
Group:		programming
Requires:	lirc = %{version}-%{release}

%description    devel
LIRC is a package that allows you to decode and send infra-red and
other signals of many (but not all) commonly used remote controls.
Included applications include daemons which decode the received
signals as well as user space applications which allow controlling a
computer with a remote control.  This package includes files for
developing applications that use LIRC.


%package -n dkms-%{name}
Summary:	Kernel modules for LIRC
Group:		system
Requires(post):	dkms
Requires(preun):dkms

%description -n	dkms-%{name}
This package provides the kernel modules for LIRC.

Install this package if the LIRC driver you are using requires
them and your kernel doesn't include them.

Note that lirc_gpio and lirc_parallel are in packages of their own.

%description -n	dkms-%{name} -l ja
このパッケージには LIRC 用のカーネルモジュールが含まれています。

もしあなたが使っている赤外線リモコン用のドライバがカーネルに
含まれていない場合は、このパッケージをインストールしてください。

注記: lirc_gpio と lirc_parallel は別のパッケージになっています。


%debug_packate


%prep
%autosetup -p1 -n %{name}-%{version}%{?tag:-}%{?tag}

sed -i -e 's/#effective-user/effective-user /' lirc_options.conf
sed -i -e '/^effective-user/s/=$/= lirc/' lirc_options.conf
sed -i '/User=/s/; *//' systemd/lircd.service
sed -i '/Group=/s/; *//' systemd/lircd.service
sed -i 's/; *user=/User=/' systemd/irexec.service
sed -i 's/; *group=/Group=/' systemd/irexec.service

sed -i -e 's|/usr/local/etc/|/etc/|' contrib/irman2lirc


%build
autoreconf -if
%configure \
	--disable-static \
	--disable-manage-devices \
	--enable-sandboxed \
	--with-x \
	--with-transmitter \
	--with-driver=userspace \
	--with-kerneldir=$(pwd)

make LANG=C.utf8 V=0 %{?_smp_mflags}


%install
%{__rm} -rf $RPM_BUILD_ROOT __docs
make -s V=0 LIBTOOLFLAGS="--silent %{?Wnone}" DESTDIR=$RPM_BUILD_ROOT install

chmod 755 $RPM_BUILD_ROOT%{_datadir}/lirc/contrib/irman2lirc
find $RPM_BUILD_ROOT%{_libdir}/ -name \*.la -delete

install -pm 755 contrib/irman2lirc $RPM_BUILD_ROOT%{_bindir}
install -Dpm 644 contrib/60-lirc.rules \
    $RPM_BUILD_ROOT%{_udevrulesdir}/60-lirc.rules
install -Dpm 644 %{SOURCE2} \
    $RPM_BUILD_ROOT%{_udevrulesdir}/99-remote-control-lirc.rules

%if %{with systemd}
mkdir -p $RPM_BUILD_ROOT/%{_tmpfilesdir}
echo "d %{_rundir}/lirc  0755  lirc  lirc  -" \
    > $RPM_BUILD_ROOT%{_tmpfilesdir}/lirc.conf
%else
%{__install} -Dpm 755 %{SOURCE1} $RPM_BUILD_ROOT%{_initdir}/lirc
%{__install} -Dpm 644 %{SOURCE3} $RPM_BUILD_ROOT%{_sysconfdir}/sysconfig/lirc
%endif

#%{__install} -m 644 -D %{SOURCE2} $RPM_BUILD_ROOT/etc/logrotate.d/lircd

## For DKMS sub packagtes
#
#for lircsrcdir in %{name} %{name}-parallel %{name}-gpio; do
for lircsrcdir in %{name}; do

install -d -m755 $RPM_BUILD_ROOT/usr/src/$lircsrcdir-%{version}-%{release}
cp -a Makefile Makefile.in Makefile.am \
	configure.ac config.status config.h \
	$RPM_BUILD_ROOT/usr/src/$lircsrcdir-%{version}-%{release}

# Makefiles call there to unnecessarily regenerate files
echo '#!/bin/true' > $RPM_BUILD_ROOT/usr/src/$lircsrcdir-%{version}-%{release}/configure
chmod +x $RPM_BUILD_ROOT/usr/src/$lircsrcdir-%{version}-%{release}/configure

done

cp -a drivers $RPM_BUILD_ROOT/usr/src/%{name}-%{version}-%{release}

# get modulelist
pushd drivers
drivers=$(echo lirc_* | sed "s/lirc_parallel //" | sed "s/lirc_gpio //")
popd

# Anssi 2009-03 empty directory
drivers="${drivers/lirc_cmdir /}"

cat > $RPM_BUILD_ROOT/usr/src/%{name}-%{version}-%{release}/dkms.conf <<EOF
PACKAGE_NAME="%{name}"
PACKAGE_VERSION="%{version}-%{release}"
MAKE[0]="droot=\\\$(pwd); for driver in $drivers; do cd \\\$droot/drivers/\\\$driver; make \
	KERNEL_LOCATION=\$kernel_source_dir AUTOMAKE=true AUTOCONF=true ACLOCAL=true; done"
CLEAN="droot=\\\$(pwd); for driver in $drivers; do cd \\\$droot/drivers/\\\$driver; make \
	clean AUTOMAKE=true AUTOCONF=true ACLOCAL=true; done"
AUTOINSTALL=yes
EOF

i=0
for module in $drivers; do
	cat >> $RPM_BUILD_ROOT/usr/src/%{name}-%{version}-%{release}/dkms.conf <<-EOF
	BUILT_MODULE_NAME[$i]="$module"
	BUILT_MODULE_LOCATION[$i]="drivers/$module"
	DEST_MODULE_LOCATION[$i]="/kernel/drivers/input/misc"
	EOF
	i=$((i+1))
done


%pre
getent group lirc >/dev/null || groupadd -r lirc
getent passwd lirc >/dev/null || \
    useradd -r -g lirc -d /var/log/lirc -s /sbin/nologin \
        -c "LIRC daemon user, runs lircd." lirc
usermod -a -G dialout lirc &> /dev/null || :
usermod -a -G lock lirc &> /dev/null || :
usermod -a -G input lirc &> /dev/null || :
exit 0

%post
%if %{with systemd}
%systemd_post lircd.service lircmd.service
systemd-tmpfiles --create %{_tmpfilesdir}/lirc.conf
%else
/sbin/chkconfig --add lirc
%endif
# If we're upgrading, move config files into their new location, if need be
if [ $1 -ge 2 ] ; then
  if [ -e %{_sysconfdir}/lircd.conf -a ! -e %{_sysconfdir}/lirc/lircd.conf ]; then
    mv %{_sysconfdir}/lircd.conf %{_sysconfdir}/lirc/lircd.conf
  fi
  if [ -e %{_sysconfdir}/lircmd.conf -a ! -e %{_sysconfdir}/lirc/lircmd.conf ]; then
    mv %{_sysconfdir}/lircmd.conf %{_sysconfdir}/lirc/lircmd.conf
  fi
fi

%preun
%if %{with systemd}
%systemd_preun lircd.service lircmd.service
%else
if [ $1 -eq 0 -o -x /bin/systemctl ] ; then
  /sbin/service lirc stop || :
  /sbin/chkconfig --del lirc || :
fi
%endif

%postun
%if %{with systemd}
%systemd_postun_with_restart lircd.service lircmd.service
%else
if [ "$1" -ge "1" ]; then
  /sbin/service lirc condrestart || :
fi
%endif

%post -n dkms-%{name}
dkms add     -m %{name} -v %{version}-%{release} --rpm_safe_upgrade &&
dkms build   -m %{name} -v %{version}-%{release} --rpm_safe_upgrade &&
dkms install -m %{name} -v %{version}-%{release} --rpm_safe_upgrade --force || :

%preun -n dkms-%{name}
dkms remove  -m %{name} -v %{version}-%{release} --rpm_safe_upgrade --all || :


%files
%defattr(-, root, root)
%license COPYING
%doc AUTHORS NEWS README
%doc %{_docdir}/lirc/*
#/etc/logrotate.d/lircd
%dir %{_sysconfdir}/lirc
%dir %{_sysconfdir}/lirc/lircd.conf.d
%ghost %config(noreplace) %{_sysconfdir}/lirc/*.conf
%ghost %config(noreplace) %{_sysconfdir}/lirc/irexec.lircrc
%ghost %config(noreplace) %{_sysconfdir}/lirc/lircd.conf.d/*
%{_bindir}/*
%{_sbindir}/*
%{_libdir}/*.so.*
%{_libdir}/lirc
%{_datadir}/lirc/
%{_mandir}/man?/*
%{_localstatedir}/lib/lirc
%{_udevrulesdir}/*
%{_libdir}/python%{python3_version}/site-packages/lirc
%{_libdir}/python%{python3_version}/site-packages/lirc-setup
%if %{with systemd}
%{_tmpfilesdir}/lirc.conf
%{_unitdir}/lirc*
%{_unitdir}/irexec.service
%else
%{_initdir}/lirc
%config(noreplace) %{_sysconfdir}/sysconfig/lirc
%endif

%files devel
%{_includedir}/lirc/
%{_includedir}/*.h
%{_libdir}/*.so
%{_libdir}/pkgconfig/*.pc

%files -n dkms-%{name}
%defattr(-,root,root)
/usr/src/%{name}-%{version}-%{release}


%changelog
* Mon Jul 08 2024 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 0.10.2-1
- new upstream release.
- dropped ldconfig scriptlets.

* Sun Aug 16 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 0.10.1-1
- new upstream release.
- added systemd support (disabled as default).

* Sat Jul 16 2016 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 0.9.4-1
- updated to 0.9.4.
- dropped Patch0.
- added a subpackage: lirc-devel.

* Fri Dec 27 2013 Yoji TOYODA <bsyamato@sea.plala.or.jp> 0.9.0-1
- update to 0.9.0
- remove Patch1 (lirc-0.8.6-standardized-remote-keycodes.patch)
- remove Patch2 (lirc-0.8.4-make-remote-names-all-unique.patch)
- remove Patch100 (lirc-0.8.7-mceusb-GV-MC7_RCKIT.patch)

* Sun Oct 17 2010 Kazutaka HARADA <kazutaka@vinelinux.org> 0.8.7-1
- new upstream release
- update Patch1, 2 and 100
- drop Patch3 (already merged by upstream)

* Fri Feb 05 2010 Shu KONNO <owa@bg.wakwak.com> 0.8.6-3
- rebuilt with python-2.6.4-3

* Wed Dec 23 2009 Kazutaka HARADA <kazutaka@vinelinux.org> 0.8.6-2
- add Patch3 to fix compilation error against kernel-2.6.32

* Sat Nov 14 2009 Kazutaka HARADA <kazutaka@dc4.so-net.ne.jp> 0.8.6-1
- new upstream release
- drop obsolete patches
- add new patches and configuration files from fedora package
- rewrite spec
- add dkms-lirc sub packages refer to mandriva package
  (but we don't build lirc-paralle and lirc-gpio sub packages)

* Sat Aug 09 2008 Shu KONNO <owa@bg.wakwak.com> 0.6.5-1vl5
- added lirc-0.6.5-config.patch for x86_64
- applied new versioning policy
- spec in utf-8

* Mon Feb 16 2004 Daisuke SUZUKI <daisuke@linux.or.jp> 0.6.5-0vl1
- initial build for Vine Linux

