File apcupsd.spec of Package apcupsd

#
# spec file for package apcupsd
#
# Copyright (c) 2014 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:           apcupsd
BuildRequires:  apache2-devel
BuildRequires:  automake
BuildRequires:  distribution-release
BuildRequires:  dos2unix
BuildRequires:  fdupes
BuildRequires:  gcc-c++
BuildRequires:  gconf2-devel
BuildRequires:  gd-devel
BuildRequires:  gnome-vfs2-devel
BuildRequires:  gtk2-devel
BuildRequires:  mailx
BuildRequires:  ncurses-devel
BuildRequires:  net-snmp-devel
BuildRequires:  tcpd-devel
BuildRequires:  update-desktop-files
BuildRequires:  xorg-x11-devel
%if 0%{?suse_version} > 1220
BuildRequires:  gpg-offline
%endif
%if 0%{?suse_version} > 1220
BuildRequires:  systemd-sysvinit
%else
%if 0%{?suse_version} < 1210
BuildRequires:  sysvinit
%else
BuildRequires:  sysvinit-init
%endif
%endif
%if %suse_version >= 1310
BuildRequires:  systemd
%else
BuildRequires:  pm-utils
%endif
Summary:        APC UPS Daemon (Powerful Daemon for APC UPSs)
License:        GPL-2.0
Group:          Hardware/UPS
Version:        3.14.11
Release:        0
Url:            http://www.apcupsd.com/
Source:         http://downloads.sourceforge.net/project/apcupsd/apcupsd%20-%20Stable/%{version}/apcupsd-%{version}.tar.gz
Source1:        apcupsd.init
Source2:        README.SUSE
Source4:        %{name}.hibernate
Source5:        %{name}.sysconfig
Source6:        %{name}.logrotate
Source7:        apcupsd-httpd.conf
Source8:        http://downloads.sourceforge.net/project/apcupsd/apcupsd%20-%20Stable/%{version}/apcupsd-%{version}.tar.gz.sig
Source9:        %{name}.keyring
Source10:       %{name}.firewall
# PATCH-FIX-OPENSUSE apcupsd-suse.patch sbrabec@suse.cz -- Do not perform halt script alternation on install.
Patch:          apcupsd-suse.patch
# PATCH-FIX-OPENSUSE apcupsd-gpp.patch sbrabec@suse.cz -- Build against C++ unfriendly system tcpd.h. See below.
Patch1:         apcupsd-gpp.patch
# PATCH-FEATURE-OPENSUSE apcupsd-hibernate.patch sbrabec@suse.cz -- Support for hibernation on powerfail.
Patch2:         apcupsd-hibernate.patch
# PATCH-FEATURE-OPENSUSE apcupsd-powersave.patch sbrabec@suse.cz -- Call powersave-notify in distros where it is available.
Patch3:         apcupsd-powersave.patch
# PATCH-FIX-OPENSUSE apcupsd-3.14.6-undef-MIB-for-new-net-snmp.diff seife@opensuse.org -- Fix build with newer net-snmp by undefining MIB.
Patch9:         apcupsd-3.14.6-undef-MIB-for-new-net-snmp.diff
# PATCH-FIX-OPENSUSE apcupsd-3.14.1-cloexec.patch cristian.rodriguez@opensuse.org -- Close the socket on exec - avoid leaked file descriptors, patch from Fedora.
Patch10:        apcupsd-3.14.1-cloexec.patch
# PATCH-FIX-OPENSUSE apcupsd-3.14.8-systemd.patch p.drouand@gmail.com -- systemd support
Patch11:        apcupsd-3.14.8-systemd.patch
# PATCH-FIX-OPENSUSE apcupsd-3.14.8-cxxld.patch p.drouand@gmail.com -- fix FTBFS, c++ linking needs -lstdc++ explicitly
Patch12:        apcupsd-3.14.8-cxxld.patch
# PATCH-FIX-OPENSUSE apcupsd-3.14.9-fixgui.patch rhbz#578276 p.drouand@gmail.com -- fix crash in gui
Patch13:        apcupsd-3.14.9-fixgui.patch
# PATCH-FIX-OPENSUSE fix-fsf-adress.patch p.drouand@gmail.com -- FSF adress is incorrect
Patch14:        fix-fsf-adress.patch
# PATCH-FEATURE-OPENSUSE apcupsd-pm-utils.patch sbrabec@suse.cz -- Use pm-utils in openSUSE < 13.1 and SLE < 12
Patch15:        apcupsd-pm-utils.patch
# PATCH-FIX-OPENSUSE apcupsd-3.14.10-nodbg.patch rhbz#1053324 matwey.kornilov@gmail.com -- reduce amount of debug messages
Patch16:        apcupsd-3.14.10-nodbg.patch
Requires:       /usr/bin/mail
Requires(post):  %fillup_prereq
# sed and grep are still prerequred to finish upgrade from sysvinit scripts:
PreReq:         /usr/bin/sed /usr/bin/grep
%if 0%{?suse_version} > 1220
%{?systemd_requires}
%else
PreReq:         %insserv_prereq
%endif
Suggests:       logrotate
BuildRoot:      %{_tmppath}/%{name}-%{version}-build
%define apache_serverroot %(/usr/sbin/apxs2 -q datadir 2>/dev/null || /usr/sbin/apxs -q PREFIX)

%description
Controls and monitors the status of an APC UPS under Linux. Allows your
computer or server to run for a specified length of time on UPS power
then executes a controlled shutdown in the case of an extended power
failure. Find APC on the Internet at http://www.apc.com/.

APC also made their PowerChute plus available for download at
http://www.apc.com/tools/download/.

%package cgi
Summary:        Web interface for apcupsd
Group:          Hardware/UPS
Requires:       %{name} = %{version}

%description cgi
A CGI interface to the APC UPS monitoring daemon.

%package gui

Summary:        APC UPS Monitor GUI (for APC UPSs)
Group:          Hardware/UPS
Requires:       %{name} = %{version}

%description gui
Controls and monitors the status of an APC UPS under Linux. Allows your
computer or server to run for a specified length of time on UPS power
then executes a controlled shutdown in the case of an extended power
failure. Find APC on the Internet at http://www.apc.com/.

APC also made their PowerChute plus available for download at
http://www.apc.com/tools/download/.

%prep
%if 0%{?gpg_verify:1}
%gpg_verify %{S:8}
%endif
%setup -q
#This is needed as apcupsd doesn't recognize ppc64 correctly
cp /usr/share/automake*/config.{guess,sub} autoconf/
cp -a %{S:1} platforms/suse/apcupsd.in
cp -a %{S:2} %{S:4} %{S:5} .
%patch
# To be surely in sync with tcpd API, build against system header. It requires a patch.
rm include/tcpd.h
%patch1
%patch2
%if %suse_version < 1110
%patch3
%endif
chmod -x kernstodo
%patch9
%patch10 -p1
%patch11 -p1
%patch12 -p1
%patch13 -p1
%patch14 -p1
sed -i '/^#!/d' %{SOURCE1}
%if %suse_version < 1310
%patch15
%endif
%patch16 -p1

%build
%configure\
	--sysconfdir=%{_sysconfdir}/%{name}\
	--with-libwrap\
	--enable-all\
	--enable-nls\
	--with-cgi-bin=%{apache_serverroot}/cgi-bin
make %{?_smp_mflags}

%install
%makeinstall
install -m744 platforms/apccontrol \
              $RPM_BUILD_ROOT%{_sysconfdir}/%{name}/apccontrol
%if 0%{?suse_version} > 1220
ln -sf /usr/sbin/service $RPM_BUILD_ROOT/%{_sbindir}/rc%{name}
%else
mkdir -p $RPM_BUILD_ROOT%{_initrddir}
ln -sf ../../etc/init.d/%{name} $RPM_BUILD_ROOT/%{_sbindir}/rc%{name}
%endif
%suse_update_desktop_file gapcmon
chmod 644 $RPM_BUILD_ROOT/%{_datadir}/pixmaps/*.png
# Cleanup for later doc macro processing
chmod -x examples/*.c
rm examples/*.in
find examples users_guide -type f | xargs chmod 644
# fix end-of-line encoding
dos2unix -o examples/status/SmartUPS-vs-650.status examples/snoopdecode.c examples/rpt/Smart-UPS-1500.rpt \
	examples/rpt/Back-UPS-ES-550.rpt examples/rpt/Back-UPS-CS-650.rpt examples/rpt/Back-UPS-BR-800.rpt
# hid-ups.rpt is duplicate by BackUPS.rpt
%fdupes -s examples/rpt/

rm -r $RPM_BUILD_ROOT/%{_datadir}/hal
%if %suse_version >= 1310
mkdir -p $RPM_BUILD_ROOT/usr/lib/systemd/system-sleep
sed "s:@PWRFAILDIR@:%{_sysconfdir}/%{name}:g" <%{name}.hibernate >$RPM_BUILD_ROOT/usr/lib/systemd/system-sleep/apcupsd.sh
chmod +x $RPM_BUILD_ROOT/usr/lib/systemd/system-sleep/apcupsd.sh
%else
mkdir -p $RPM_BUILD_ROOT/etc/pm/sleep.d
sed "s:@PWRFAILDIR@:%{_sysconfdir}/%{name}:g" <%{name}.hibernate >$RPM_BUILD_ROOT/etc/pm/sleep.d/99apcupsd
chmod +x $RPM_BUILD_ROOT/etc/pm/sleep.d/99%{name}
%endif
mkdir -p $RPM_BUILD_ROOT/var/adm/fillup-templates
cp %{name}.sysconfig $RPM_BUILD_ROOT/var/adm/fillup-templates/sysconfig.%{name}

# systemd support and remove initd support for opensuse 12.2 and higher
%if 0%{?suse_version} > 1220
install -p -D -m644 %{name}.service $RPM_BUILD_ROOT%{_unitdir}/%{name}.service
install -p -D -m755 %{name}_shutdown $RPM_BUILD_ROOT/usr/lib/systemd/system-shutdown/%{name}_shutdown
rm $RPM_BUILD_ROOT/etc/init.d/%{name}
%endif

install -d %{buildroot}%{_sysconfdir}/logrotate.d
install -m0644 %{SOURCE6} %{buildroot}%{_sysconfdir}/logrotate.d/%{name}
install -d %{buildroot}%{_sysconfdir}/apache2/conf.d/
install -m0644 %{SOURCE7} %{buildroot}%{_sysconfdir}/apache2/conf.d/%{name}.conf

# firewall config
install -m 644 -D %{SOURCE5} %{buildroot}/%{_sysconfdir}/sysconfig/SuSEfirewall2.d/services/%{name}

%pre
%if 0%{?suse_version} > 1220
%service_add_pre %{name}.service
%endif

%post
%if 0%{?suse_version} > 1220
%{fillup_only}
%service_add_post %{name}.service
# Remove HALT_POWERDOWN_INSERT, it is not needed with systemd (it was last time filled in 13.1).
# We need to handle only the last form, which was already used in SLE11.
if test -f etc/sysconfig/shutdown ; then
  if usr/bin/grep -q '^HALT_POWERDOWN_INSERT=' etc/sysconfig/shutdown ; then
    if ! usr/bin/grep -q '^HALT_POWERDOWN_INSERT=""' etc/sysconfig/shutdown ; then
      if usr/bin/grep -q '^HALT_POWERDOWN_INSERT="/etc/init\.d/apcupsd try-powerdown"' etc/sysconfig/shutdown ; then
        usr/bin/sed -i 's:^\(HALT_POWERDOWN_INSERT="\)/etc/init\.d/apcupsd try-powerdown":\1":' etc/sysconfig/shutdown
      else
        echo >&2 "apcupsd: WARNING: Unknown value of HALT_POWERDOWN_INSERT in /etc/sysconfig/shutdown."
        echo >&2 "         Keeping unchanged. If it still exists, it should be:"
        echo >&2 "HALT_POWERDOWN_INSERT=\"\""
      fi
    fi
  fi
fi
# User installed file, by default in docdir. Not needed any more, and will never be.
rm -f etc/init.d/apcupsd-early-powerdown
%else
%{fillup_and_insserv %{name}}
# HALT_POWERDOWN_INSERT is supported since SuSE Linux 10.1, SLES 10.
# Install HALT_POWERDOWN_INSERT
if usr/bin/grep -q '^HALT_POWERDOWN_INSERT="\(/etc/apcupsd/apccontrol killpower\|/etc/init\.d/apcupsd try-powerdown\|\)"' etc/sysconfig/shutdown ; then
  usr/bin/sed -i 's:^\(HALT_POWERDOWN_INSERT="\).*"[[:space:]]*:\1/etc/init.d/apcupsd try-powerdown":' etc/sysconfig/shutdown
else
  echo >&2 "%{name}: WARNING: Unknown value of HALT_POWERDOWN_INSERT in /etc/sysconfig/shutdown."
  echo >&2 "         Keeping unchanged. Should be:"
  echo >&2 "HALT_POWERDOWN_INSERT=\"/etc/init.d/apcupsd try-powerdown\""
fi
%endif

%preun
%if 0%{?suse_version} > 1220
%service_del_preun %{name}.service
%else
%{stop_on_removal apcupsd}
%endif

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

%files
%defattr(-,root,root)
%doc COPYING ChangeLog examples ReleaseNotes README.SUSE
%{_sbindir}/%{name}
%{_sbindir}/apcaccess
%{_sbindir}/apctest
%{_sbindir}/smtp
%{_sbindir}/rc%{name}
%if 0%{?suse_version} <= 1220
%{_initrddir}/%{name}
%else
%{_unitdir}/%{name}.service
/usr/lib/systemd/system-shutdown/%{name}_shutdown
%endif
%if %suse_version >= 1310
/usr/lib/systemd/system-sleep/%{name}.sh
%else
/etc/pm/sleep.d/99%{name}
%endif
%dir %{_sysconfdir}/%{name}
%config(noreplace) %{_sysconfdir}/%{name}/changeme
%config(noreplace) %{_sysconfdir}/%{name}/commfailure
%config(noreplace) %{_sysconfdir}/%{name}/commok
%config(noreplace) %{_sysconfdir}/%{name}/onbattery
%config(noreplace) %{_sysconfdir}/%{name}/offbattery
%config(noreplace) %{_sysconfdir}/logrotate.d/apcupsd
%config(noreplace) %{_sysconfdir}/%{name}/%{name}.conf
%config(noreplace) %attr(0644,root,root) %{_sysconfdir}/sysconfig/SuSEfirewall2.d/services/%{name}
%attr(0755,root,root) %{_sysconfdir}/apcupsd/apccontrol
%{apache_serverroot}/cgi-bin/multimon.cgi
%{apache_serverroot}/cgi-bin/upsfstats.cgi
%{apache_serverroot}/cgi-bin/upsstats.cgi
%{apache_serverroot}/cgi-bin/upsimage.cgi
%doc %{_mandir}/man?/*.*
/var/adm/fillup-templates/sysconfig.*

%files gui
%defattr(-,root,root)
%{_bindir}/gapcmon
%{_datadir}/applications/gapcmon.desktop
%{_datadir}/pixmaps/*.png

%files cgi
%defattr(-,root,root,-)
%config(noreplace) %{_sysconfdir}/apache2/conf.d/%{name}.conf
%config(noreplace) %{_sysconfdir}/%{name}/%{name}.css
%config(noreplace) %{_sysconfdir}/%{name}/hosts.conf
%config(noreplace) %{_sysconfdir}/%{name}/multimon.conf

%changelog