File nut.spec of Package nut

#
# spec file for package nut
#
# Copyright (c) 2012 SUSE LINUX Products GmbH, Nuernberg, Germany.
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via http://bugs.opensuse.org/
#


Name:           nut
BuildRequires:  apache2-devel
BuildRequires:  avahi-devel
BuildRequires:  dbus-1-glib-devel
BuildRequires:  gd-devel
BuildRequires:  gpg-offline
BuildRequires:  libneon-devel
BuildRequires:  libopenssl-devel
BuildRequires:  libtool
BuildRequires:  libusb-devel
BuildRequires:  net-snmp-devel
BuildRequires:  openssl-devel
BuildRequires:  pkg-config
BuildRequires:  tcpd-devel
# for directory ownership:
BuildRequires:  pm-utils
# To fix end-of-line encoding:
BuildRequires:  dos2unix
# For FreeIPMI support
%if 0%{?ENABLE_IPMI}
BuildRequires:  freeipmi-devel
%endif
%if %suse_version > 1110
BuildRequires:  powerman-devel
# For doc build:
BuildRequires:  asciidoc
BuildRequires:  dblatex
%endif
Summary:        Network UPS Tools Core (Unbreakable Power Supply Monitoring)
License:        GPL-2.0+
Group:          Hardware/UPS
Version:        2.6.5
Release:        0
Url:            http://www.networkupstools.org/
Source:         http://www.networkupstools.org/source/2.6/%{name}-%{version}.tar.gz
Source1:        upsd.init
Source2:        README.SUSE
Source3:        upsd-early-powerdown.init
Source5:        nut.firewall.in
Source7:        nut.sleep
Source8:        http://www.networkupstools.org/source/2.6/%{name}-%{version}.tar.gz.sig
Source9:        %{name}.keyring
Patch:          nut-preconfig.patch
# PATCH-FIX-OPENSUSE nut-hal.patch sbrabec@suse.cz -- Fix hal callouts path.
Patch1:         nut-hal.patch
Patch3:         nut-notifyflag.patch
Patch4:         nut-powersave.patch
Patch6:         nut-udev.patch
Provides:       smartups = %version
Obsoletes:      smartups < %version
Conflicts:      apcupsd
BuildRoot:      %{_tmppath}/%{name}-%{version}-build
PreReq:         pwdutils /bin/chown %insserv_prereq pwdutils /bin/chown /bin/chmod %{_bindir}/fgrep %{_bindir}/grep %{_bindir}/sed %{_bindir}/head %{_bindir}/md5sum
Requires:       %{_bindir}/fgrep
Requires:       %{_bindir}/grep
Requires:       %{_bindir}/pgrep
Requires:       %{_bindir}/pkill
Requires:       %{_bindir}/readlink
Requires:       powersave
Requires:       usbutils
Requires(post): udev
# for update from openSUSE <= 11.3, SLE <= 11
Provides:       nut-classic = %{version}
Obsoletes:      nut-classic < %{version}
Obsoletes:      nut-hal < %{version}
%define apache_serverroot %(%{_sbindir}/apxs2 -q datadir 2>/dev/null || %{_sbindir}/apxs -q PREFIX)
%define CGIPATH		%{apache_serverroot}/cgi-bin
%define HTMLPATH	%{apache_serverroot}/htdocs
%define MODELPATH       %{_prefix}/lib/ups/driver
%define STATEPATH       %{_localstatedir}/lib/ups
%define CONFPATH	%{_sysconfdir}/ups
%define USER		upsd
%define GROUP		daemon
%define LBRACE (
%define RBRACE )
%define QUOTE "
%define BACKSLASH \\
# Collect all devices listed in ups-nut-device.fdi:
%define USBHIDDRIVERS    %(zcat %{S:0} | tr a-z A-Z | fgrep -a -A1 USBHID-UPS | sed -n 's/.*ATTR{IDVENDOR}==%{QUOTE}%{BACKSLASH}%{LBRACE}[^%{QUOTE}]*%{BACKSLASH}%{RBRACE}%{QUOTE}, ATTR{IDPRODUCT}==%{QUOTE}%{BACKSLASH}%{LBRACE}[^%{QUOTE}]*%{BACKSLASH}%{RBRACE}%{QUOTE}, MODE=.*/modalias%{LBRACE}usb:v%{BACKSLASH}1p%{BACKSLASH}2d*dc*dsc*dp*ic*isc*ip*%{RBRACE}/p' | tr '%{BACKSLASH}n' ' ')
%define USBNONHIDDRIVERS %(zcat %{S:0} | tr a-z A-Z | fgrep -a -A1 _USB       | sed -n 's/.*ATTR{IDVENDOR}==%{QUOTE}%{BACKSLASH}%{LBRACE}[^%{QUOTE}]*%{BACKSLASH}%{RBRACE}%{QUOTE}, ATTR{IDPRODUCT}==%{QUOTE}%{BACKSLASH}%{LBRACE}[^%{QUOTE}]*%{BACKSLASH}%{RBRACE}%{QUOTE}, MODE=.*/modalias%{LBRACE}usb:v%{BACKSLASH}1p%{BACKSLASH}2d*dc*dsc*dp*ic*isc*ip*%{RBRACE}/p' | tr '%{BACKSLASH}n' ' ')
# Package provides driver for USB HID UPSes, but people can live with hal addon:
Enhances:       %USBHIDDRIVERS
# Package provides the only avalailable driver for other USB UPSes:
Supplements:    %USBNONHIDDRIVERS

%description
Core package of Network UPS Tools.

Network UPS Tools is a collection of programs which provide a common
interface for monitoring and administering UPS hardware.

Detailed information about supported hardware can be found in
/usr/share/doc/packages/nut.

%package drivers-net
Summary:        Network UPS Tools - Extra Networking Drivers (for Network Monitoring)
Group:          Hardware/UPS
Requires:       %{name} = %{version}
Enhances:       %{name}
# for update from openSUSE <= 10.3, SLE <= 10
Provides:       nut:%{MODELPATH}/snmp-ups

%description drivers-net
Networking drivers for the Network UPS Tools. You will need them
together with nut to provide UPS networking support.

Network UPS Tools is a collection of programs which provide a common
interface for monitoring and administering UPS hardware.

Detailed information about supported hardware can be found in
/usr/share/doc/packages/nut.

%package -n libupsclient1
Summary:        Network UPS Tools Library (Unbreakable Power Supply Monitoring)
Group:          System/Libraries

%description -n libupsclient1
Shared library for the Network UPS Tools.

Network UPS Tools is a collection of programs which provide a common
interface for monitoring and administering UPS hardware.

Detailed information about supported hardware can be found in
/usr/share/doc/packages/nut.

%package cgi
Summary:        Network UPS Tools Web Server Support (UPS Status Pages)
Group:          Hardware/UPS
Requires:       %{name} = %{version}
Enhances:       %{name}
Supplements:    packageand(%{name}:apache2)
# for update from openSUSE <= 10.3, SLE <= 10
Provides:       nut:/srv/www/cgi-bin/upsstats.cgi

%description cgi
Web server support package for the Network UPS Tools.

Predefined URL is http://localhost/nut/index.html

Network UPS Tools is a collection of programs which provide a common
interface for monitoring and administering UPS hardware.

Detailed information about supported hardware can be found in
/usr/share/doc/packages/nut.

%package devel
Summary:        Network UPS Tools (Unbreakable Power Supply Monitoring)
Group:          Development/Libraries/C and C++
Requires:       %{name} = %{version}
Requires:       openssl-devel

%description devel
Network UPS Tools is a collection of programs which provide a common
interface for monitoring and administering UPS hardware.

Detailed information about supported hardware can be found in
/usr/share/doc/packages/nut.

%prep
%gpg_verify %{S:8}
%setup -q
cp -a %{S:1} %{S:2} %{S:3} %{S:5} %{S:7} .
mv upsd-early-powerdown.init upsd-early-powerdown
%patch
%patch1 -p1
%patch3
%if %suse_version < 1110
%patch4
%endif
%if %suse_version < 1030
%patch6 -R
%endif
sed -i "s/@UPSD_USER@/%{USER}/g" upsd.init conf/upsmon.conf.sample
%if %suse_version < 1030
sed -i 's:/usr/lib/pm-utils/functions:/etc/pm/functions:' nut.sleep
%endif
sed -i s:/usr/local/ups/bin:/bin: conf/upssched.conf.sample.in
[ -f docs/nut-qa.txt ]
dos2unix docs/nut-qa.txt

%build
autoreconf -f -i
%configure --disable-static --with-pic --libexecdir=%_prefix/lib\
	--sysconfdir=%{CONFPATH}\
	--datadir=%{_datadir}/nut\
	--with-all\
	--with-dev\
	--without-hal\
%if ! 0%{?ENABLE_IPMI}
	--without-ipmi\
%endif
%if %suse_version <= 1110
	--without-powerman\
%endif
	--with-linux-hiddev=%{_includedir}/linux/hiddev.h\
	--with-htmlpath=%{HTMLPATH}\
	--with-cgipath=%{CGIPATH}\
	--with-statepath=%{STATEPATH}\
	--with-drvpath=%{MODELPATH}\
	--with-user=%{USER}\
	--with-group=%{GROUP} \
	--with-udev-dir=/etc/udev
make %{?jobs:-j%jobs}
PORT=$(sed -n 's/#define PORT //p' config.log)
if test "$PORT" = 3493 ; then
    PORT=nut
fi
sed s/@PORT@/$PORT/ <nut.firewall.in >nut.firewall

%install
make DESTDIR=$RPM_BUILD_ROOT install
mkdir -p $RPM_BUILD_ROOT%{STATEPATH}
# SuSE rc
mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/init.d
mkdir -p $RPM_BUILD_ROOT%{_sbindir}
install -m 755 upsd.init $RPM_BUILD_ROOT%{_sysconfdir}/init.d/upsd
ln -sf ../..%{_sysconfdir}/init.d/upsd $RPM_BUILD_ROOT%{_sbindir}/rcupsd
mkdir -p $RPM_BUILD_ROOT/etc/logrotate.d
install -m 644 scripts/logrotate/nutlogd $RPM_BUILD_ROOT/etc/logrotate.d/
mkdir -p $RPM_BUILD_ROOT%{STATEPATH}
%if %suse_version < 1110
mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/dbus-1/system.d
install -m 644 packaging/opensuse/upsd.dbus-1 $RPM_BUILD_ROOT%{_sysconfdir}/dbus-1/system.d/upsd.conf
%endif
rename .sample "" $RPM_BUILD_ROOT%{_sysconfdir}/ups/*.sample
%if %suse_version >= 1030
mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/sysconfig/SuSEfirewall2.d/services
cp nut.firewall $RPM_BUILD_ROOT%{_sysconfdir}/sysconfig/SuSEfirewall2.d/services/nut
%endif
%if %suse_version >= 1030
mkdir -p $RPM_BUILD_ROOT%{_prefix}/lib/pm-utils/sleep.d
install nut.sleep $RPM_BUILD_ROOT%{_prefix}/lib/pm-utils/sleep.d/99nut
%else
mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/pm/sleep.d
install nut.sleep $RPM_BUILD_ROOT%{_sysconfdir}/pm/sleep.d/99nut
%endif
mkdir $RPM_BUILD_ROOT/bin
mv $RPM_BUILD_ROOT%{_bindir}/upssched-cmd $RPM_BUILD_ROOT/bin/upssched-cmd
# Rename web pages to not conflict with apache2-example-pages or user home page:
mkdir $RPM_BUILD_ROOT%{HTMLPATH}/nut $RPM_BUILD_ROOT%{CGIPATH}/nut
mv $RPM_BUILD_ROOT%{HTMLPATH}/*.{html,png} $RPM_BUILD_ROOT%{HTMLPATH}/nut/
mv $RPM_BUILD_ROOT%{CGIPATH}/*.cgi $RPM_BUILD_ROOT%{CGIPATH}/nut
%{__rm} -f %{buildroot}%{_libdir}/*.la
mkdir -p $RPM_BUILD_ROOT/etc/bash_completion.d
install -m0644 scripts/misc/nut.bash_completion $RPM_BUILD_ROOT/etc/bash_completion.d/

%pre
usr/sbin/useradd -r -g %{GROUP} -s /bin/false \
  -c "UPS daemon" -d /sbin %{USER} 2>/dev/null || :

%post
# Be sure that all files are owned by a dedicated user.
bin/chown -R %{USER}:%{GROUP} %{STATEPATH}
%{fillup_and_insserv -f -n upsd}
# Generate initial passwords.
if usr/bin/fgrep -q "password = @UPSD_INITIAL_MASTER_PASSWORD@" %{CONFPATH}/{upsmon.conf,upsd.users} ; then
  UPSD_INITIAL_MASTER_PASSWORD=$(usr/bin/head -c 20 /dev/urandom | usr/bin/md5sum | usr/bin/head -c 10)
  usr/bin/sed -i s/@UPSD_INITIAL_MASTER_PASSWORD@/$UPSD_INITIAL_MASTER_PASSWORD/ %{CONFPATH}/{upsmon.conf,upsd.users}
fi
if usr/bin/fgrep -q "password = @UPSD_INITIAL_SLAVE_PASSWORD@" %{CONFPATH}/upsd.users ; then
  UPSD_INITIAL_SLAVE_PASSWORD=$(usr/bin/head -c 20 /dev/urandom | usr/bin/md5sum | usr/bin/head -c 10)
  usr/bin/sed -i s/@UPSD_INITIAL_SLAVE_PASSWORD@/$UPSD_INITIAL_SLAVE_PASSWORD/ %{CONFPATH}/upsd.users
fi
# Be sure that all files are owned by a dedicated user.
bin/chown %{USER}:root %{CONFPATH}/upsd.conf %{CONFPATH}/upsmon.conf %{CONFPATH}/upsd.users
bin/chmod 600 %{CONFPATH}/upsd.conf %{CONFPATH}/upsmon.conf %{CONFPATH}/upsd.users
# Install HALT_POWERDOWN_INSERT
if usr/bin/grep -q '^HALT_POWERDOWN_INSERT="\(%{_sysconfdir}/init\.d/upsd try-powerdown\|\)"' etc/sysconfig/shutdown ; then
  usr/bin/sed -i 's:^\(HALT_POWERDOWN_INSERT="\).*"[[:space:]]*:\1%{_sysconfdir}/init.d/upsd try-powerdown":' etc/sysconfig/shutdown
else
 # User configured Suspend-to-disc. Keep it unchanged and be silent.
  if ! usr/bin/grep -q '^HALT_POWERDOWN_INSERT="/usr/bin/powersave -U' etc/sysconfig/shutdown ; then
    echo >&2 "nut: WARNING: Unknown value of HALT_POWERDOWN_INSERT in %{_sysconfdir}/sysconfig/shutdown."
    echo >&2 "     Keeping unchanged. Should be:"
    echo >&2 "HALT_POWERDOWN_INSERT=\"%{_sysconfdir}/init.d/upsd try-powerdown\""
    echo >&2 "or"
    echo >&2 "HALT_POWERDOWN_INSERT=\"/usr/bin/powersave -U\""
  fi
fi
# Migrate Suspend to Disc to the new convention (bnc#449861):
# It was never on by default, but documentation up to 11.0 recommends
# "shutdown -z +0" for suspend to disc. It was discontinued before 11.0.
if fgrep "shutdown -z +0" /etc/ups/upsmon.conf ; then
sed -i 's:/sbin/shutdown -z +0:/usr/bin/powersave -U:;s:shutdown -z +0:/usr/bin/powersave -U:' /etc/ups/upsmon.conf
fi
# Silently remove %{_sysconfdir}/sysconfig/smartups. File is now obsolete and
# values are copied to not replaced config files.
# %{_sysconfdir}/sysconfig/smartups was last present in SuSE Linux 10.0
rm -f %{_sysconfdir}/sysconfig/smartups
# And finally trigger udev to set permissions according to newly installed rules files.
/sbin/udevadm trigger --subsystem-match=usb --property-match=DEVTYPE=usb_device

%preun
%{stop_on_removal upsd}

%postun
%{restart_on_update upsd}
# Remove HALT_POWERDOWN_INSERT
if test $1 -eq 0 -a ! -f %{_sysconfdir}/init.d/upsd ; then
  if usr/bin/grep -q '^HALT_POWERDOWN_INSERT="\(%{_sysconfdir}/init\.d/upsd try-powerdown\|/usr/bin/powersave -U\)"' etc/sysconfig/shutdown ; then
    usr/bin/sed -i 's:^\(HALT_POWERDOWN_INSERT="\)\(%{_sysconfdir}/init\.d/upsd try-powerdown\|/usr/bin/powersave -U\)":\1":' etc/sysconfig/shutdown
  else
    echo >&2 "nut: WARNING: Unknown value of HALT_POWERDOWN_INSERT in %{_sysconfdir}/sysconfig/shutdown."
    echo >&2 "     Keeping unchanged. Should be:"
    echo >&2 "HALT_POWERDOWN_INSERT=\"\""
  fi
  if test -f %{_sysconfdir}/init.d/upsd-early-powerdown ; then
    echo >&2 "nut: WARNING: You may want to remove your %{_sysconfdir}/init.d/upsd-early-powerdown."
  fi
fi
%insserv_cleanup

%post -n libupsclient1 -p /sbin/ldconfig

%postun -n libupsclient1 -p /sbin/ldconfig

%clean
rm -rf $RPM_BUILD_ROOT

%files
%defattr(-,root,root)
%doc AUTHORS COPYING ChangeLog MAINTAINERS NEWS README README.SUSE UPGRADING upsd-early-powerdown docs/*.txt docs/cables
/bin/*
/etc/bash_completion.d/*
/etc/logrotate.d/*
%{_bindir}/*
%{_datadir}/nut
%doc %{_mandir}/man5/*.*
%doc %{_mandir}/man8/*.*
%exclude %doc %{_mandir}/man8/netxml-ups*.*
%exclude %doc %{_mandir}/man8/snmp-ups*.*
%dir %{_prefix}/lib/ups
%if %suse_version >= 1030
%{_prefix}/lib/pm-utils/sleep.d/99nut
%else
%{_sysconfdir}/pm/sleep.d/99nut
%endif
%{_sbindir}/*
%if %suse_version < 1110
%{_sysconfdir}/dbus-1/system.d/upsd.conf
%endif
%if %suse_version >= 1030
%config(noreplace) %{_sysconfdir}/sysconfig/SuSEfirewall2.d/services/nut
%endif
%{_sysconfdir}/init.d/upsd
%dir %{_sysconfdir}/udev
%dir %{_sysconfdir}/udev/rules.d
%config(noreplace) %{_sysconfdir}/udev/rules.d/*.rules
%config(noreplace) %{CONFPATH}/hosts.conf
%config(noreplace) %attr(600,%{USER},root) %{CONFPATH}/upsd.conf
%config(noreplace) %attr(600,%{USER},root) %{CONFPATH}/upsd.users
%config(noreplace) %attr(600,%{USER},root) %{CONFPATH}/upsmon.conf
%dir %{CONFPATH}
%config(noreplace) %{CONFPATH}/nut.conf
%config(noreplace) %{CONFPATH}/ups.conf
%config(noreplace) %{CONFPATH}/upsset.conf
%config(noreplace) %{CONFPATH}/upssched.conf
%dir %{MODELPATH}
%{MODELPATH}/*
%exclude %{MODELPATH}/snmp-ups
%exclude %{MODELPATH}/netxml-ups
%attr(700,%{USER},%{GROUP}) %{STATEPATH}

%files drivers-net
%defattr(-,root,root)
%{MODELPATH}/snmp-ups
%{MODELPATH}/netxml-ups
%doc %{_mandir}/man8/netxml-ups*.*
%doc %{_mandir}/man8/snmp-ups*.*

%files -n libupsclient1
%defattr(-,root,root)
%{_libdir}/*.so.*

%files cgi
%defattr(-,root,root)
%{CGIPATH}/nut
%{HTMLPATH}/nut
%config(noreplace) %{CONFPATH}/upsstats-single.html
%config(noreplace) %{CONFPATH}/upsstats.html

%files devel
%defattr(-,root,root)
%{_includedir}/*.h
%{_libdir}/*.so
%{_libdir}/pkgconfig/*.pc
%doc %{_mandir}/man3/*.*

%changelog