%bcond_with systemd

Summary:	smartmontools - for monitoring S.M.A.R.T. disks and devices
Summary(ja):	smartmontools - S.M.A.R.T.ディスクとデバイスの監視
Name:		smartmontools
Version:	7.3
Release:	1%{?_dist_release}%{?with_systemd:.systemd}
License:	GPLv2+
Group:		system
URL:		http://smartmontools.sourceforge.net/
Vendor:         Project Vine
Distribution:   Vine Linux

Source0:	http://downloads.sourceforge.net/smartmontools/%{name}-%{version}.tar.gz
Source1:	ChangeLog.upstream
Source2:	smartmontools.sysconfig
Source3:	smartd.init
# https://github.com/smartmontools/smartmontools/raw/master/smartmontools/drivedb.h
Source5:	drivedb.h
Patch0:		smartmontools-5.42-vinerelease.patch
BuildRoot:	%{_tmppath}/%{name}-%{version}-root
Obsoletes:	smartctl
Obsoletes:	smartd
Obsoletes:	ucsc-smartsuite
Obsoletes:	smartsuite
Requires:	fileutils
BuildRequires:	readline-devel ncurses-devel automake util-linux groff gettext
BuildRequires:	libcap-ng-devel

%if %{with systemd}
BuildRequires:  systemd systemd-devel
%{?systemd_requires}
%else
Requires(post):	/sbin/chkconfig
Requires: chkconfig
%endif

#Packager:       Bruce Allen <smartmontools-support@lists.sourceforge.net>
# Copyright (C) 2002-4 Bruce Allen <smartmontools-support@lists.sourceforge.net>
# Home page: http://smartmontools.sourceforge.net/
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any later
# version.
# 
# You should have received a copy of the GNU General Public License (for
# example COPYING); if not, write to the Free Software Foundation, Inc., 675
# Mass Ave, Cambridge, MA 02139, USA.
#
# This code was originally developed as a Senior Thesis by Michael Cornwell
# at the Concurrent Systems Laboratory (now part of the Storage Systems
# Research Center), Jack Baskin School of Engineering, University of
# California, Santa Cruz. http://ssrc.soe.ucsc.edu/

%description
smartmontools controls and monitors storage devices using the
Self-Monitoring, Analysis and Reporting Technology System (S.M.A.R.T.)
built into ATA and SCSI Hard Drives.  This is used to check the
reliability of the hard drive and to predict drive failures.  The suite
is derived from the smartsuite package, and contains two utilities.  The
first, smartctl, is a command line utility designed to perform simple
S.M.A.R.T. tasks.  The second, smartd, is a daemon that periodically
monitors smart status and reports errors to syslog.  The package is
compatible with the ATA/ATAPI-5 specification.  Future releases will be
compatible with the ATA/ATAPI-6 andATA/ATAPI-7 specifications.  The
package is intended to incorporate as much "vendor specific" and
"reserved" information as possible about disk drives.  man smartctl and
man smartd will provide more information. The most recent versions of
this package and additional information can be found at the URL:
http://smartmontools.sourceforge.net/

%description -l ja
smartmontoolsはATAおよびSCSIハードドライブに組み込まれたSelf-Monitoring,
Analysis and Reporting Technology (S.M.A.R.T.)システムを用いてストレージ
デバイスを制御・監視するツールです。ハードドライブの信頼性を点検し、ドラ
イブの故障を予測するために用いられます。smartmontoolsはsmartsuiteパッケ
ージから派生したもので、2つのユーティリティからなります。1つ目、smartctl
はS.M.A.R.T.タスクを処理するために設計されたコマンドラインユーティリティ
です。2つ目のsmartdは周期的にS.M.A.R.T.状態を監視し、システムログにエラ
ーを出力するデーモンです。このパッケージはATA/ATAPI-5仕様に準拠していま
す。将来のリリースではATA/ATAPI-6およびATA/ATAPI-7仕様にも準拠する予定
です。このパッケージはディスクドライブに関するベンダ特有および予約された
情報も可能な限り取り扱えることを重視しています。より詳しい情報はman
smartctlおよびman smartdで得られます。最新のバージョン及び追加情報につい
ての情報は次のURLにあります：http://smartmontools.sourceforge.net/


%debug_package


%prep
%setup -q
%patch0 -p0
cp -f %{SOURCE5} .

# fix encoding
for fe in AUTHORS ChangeLog*
do
  iconv -f iso-8859-1 -t utf-8 <$fe >$fe.new
  touch -r $fe $fe.new
  mv -f $fe.new $fe
done


%build
autoreconf -i
%configure \
	--without-selinux \
	--with-libcap-ng=yes \
%if %{with systemd}
	--with-libsystemd \
	--with-systemdsystemunitdir=%{_unitdir} \
	--with-systemdenvfile=%{_sysconfdir}/sysconfig/%{name} \
%endif
	%{nil}

# update SOURCE5 on maintainer's machine prior commiting, there's no internet connection on builders
%make_build update-smart-drivedb
./update-smart-drivedb -s - -u sf drivedb.h ||:
cp drivedb.h ../drivedb.h ||:

%make_build CXXFLAGS="$RPM_OPT_FLAGS -fpie" LDFLAGS="-pie -Wl,-z,relro,-z,now"


%install
rm -rf %{buildroot}
make DESTDIR=%{buildroot} install

rm -f examplescripts/Makefile*
chmod a-x -R examplescripts/*

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

mkdir -p %{buildroot}/%{_sysconfdir}/smartd_warning.d

%if %{with systemd}
install -D -p -m 644 %{SOURCE2} $RPM_BUILD_ROOT%{_sysconfdir}/sysconfig/smartmontools
rm -rf %{buildroot}/etc/{rc.d,init.d}
%else
rm -rf %{buildroot}%{_unitdir}
mkdir -p %{buildroot}/etc/rc.d/init.d
install -m755 %{SOURCE3} -p %{buildroot}/etc/rc.d/init.d/smartd
%endif


%clean
rm -rf %{buildroot}

%if !%{with systemd}
%pre
if [ ! -f /etc/smartd.conf ]; then
	echo "Note that you can use a configuration file /etc/smartd.conf to control the"
	echo "startup behavior of the smartd daemon.  See man 8 smartd for details."
fi

# Now see if we should tell user to set service to start on boot	
/sbin/chkconfig --list smartd > /dev/null 2> /dev/null
printmessage=$?

if [ $printmessage -ne 0 ] ; then
	echo "Run \"/sbin/chkconfig --add smartd\", to start smartd service on system boot"
else
	echo "smartd will continue to start up on system boot"
fi
%endif

%post
%if %{with systemd}
%systemd_post smartd.service
%else
# if smartd is already running, restart it with the new daemon
if [ -f /var/lock/subsys/smartd ]; then
        /etc/rc.d/init.d/smartd restart 1>&2
	echo "Restarted smartd services"
else
# else tell the user how to start it
        echo "Run \"/etc/rc.d/init.d/smartd start\" to start smartd service now."
fi
%endif

%preun
%if %{with systemd}
%systemd_preun smartd.service
%else
# if uninstalling the final copy, stop and remove any links	
if [ "$1" = "0" -o -x /bin/systemctl ]; then
  if [ -f /var/lock/subsys/smartd ]; then
    /etc/rc.d/init.d/smartd stop 1>&2
    echo "Stopping smartd services"
  fi

# see if any links remain, and kill them if they do
  /sbin/chkconfig --list smartd > /dev/null 2> /dev/null
  notlinked=$?
	
  if [ $notlinked -eq 0 ]; then
    /sbin/chkconfig --del smartd
    echo "Removing chkconfig links to smartd boot-time startup scripts"
  fi
fi
%endif

%if %{with systemd}
%postun
%systemd_postun_with_restart smartd.service
%endif


%files
%defattr(-,root,root)
%license COPYING
%doc AUTHORS ChangeLog* INSTALL NEWS README TODO
%doc smartd.conf examplescripts
%config(noreplace) %{_sysconfdir}/smartd.conf
%dir %{_sysconfdir}/smartd_warning.d
%attr(755,root,root)%{_sysconfdir}/smartd_warning.sh
%attr(755,root,root) %{_sbindir}/smartd
%attr(755,root,root) %{_sbindir}/smartctl
%attr(755,root,root) %{_sbindir}/update-smart-drivedb
%attr(644,root,root) %{_mandir}/man?/*
%{_datadir}/%{name}
%if %{with systemd}
%{_unitdir}/smartd.service
%config(noreplace) %{_sysconfdir}/sysconfig/smartmontools
%else
%attr(755,root,root) /etc/rc.d/init.d/smartd
%endif


# Maintainers / Developers Key:
# [BA] Bruce Allen
# [EB] Erik Inge Bols# [SB] Stanislav Brabec
# [PC] Peter Cassidy
# [CD] Capser Dik
# [CF] Christian Franke
# [GF] Guilhem Fr騷ou
# [DG] Douglas Gilbert
# [GG] Guido Guenther
# [DK] David Kirkby
# [KM] Kai M臾isarai
# [EM] Eduard Martinescu
# [FM] Fr馘駻ic L. W. Meunier
# [KS] Keiji Sawada
# [SS] Sergey Svishchev
# [PW] Phil Williams

%changelog
* Sat Jun 10 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 7.3-1
- new upstream release.

* Wed Apr 08 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 7.1-1
- new upstream release.
- shrank %%changelog: old logs are ChangeLog.upstream.

* Tue Jul  5 2016 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 6.5-1
- new upstream release.

* Fri Nov 22 2013 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 6.2-1
- new upstream release.

* Fri Mar 23 2012 MATSUBAYASHI Kohji <shaolin@vinelinux.org> - 5.42-1
- new upstream release; spec revamp

* Tue Aug  4 2009 MATSUBAYASHI Kohji <shaolin@vinelinux.org> - 5.38-2
- spec in UTF-8

* Sun Apr 27 2008 KANEKO Seiji <giraffe@m2.pbc.ne.jp> 5.38-1vl5
- upstream release

* Wed Jan 02 2008 Shu KONNO <owa@bg.wakwak.com> 5.37-0vl2
- rebuilt for VineSeed

* Tue May 29 2007 Kazutaka HARADA <kazutaka@dc4.so-net.ne.jp> 5.37-0vl1
- upstream release

* Thu Jun 1 2006 KANEKO Seiji <giraffe@m2.pbc.ne.jp> 5.36-1vl2
- upstream release
- update Patch0
- delete noreplace attribute from %%config

* Sun Nov 28 2004 KANEKO Seiji <giraffe@m2.pbc.ne.jp> 5.33-1vl2
- add Patch0: smartmontools-vinerelease.patch
  check /etc/vine-release in /etc/rc.d/init.d/smartd

* Fri Oct 15 2004 KANEKO Seiji <giraffe@m2.pbc.ne.jp> 5.33-1vl1
- rebuild for VineSeed
- remove warnings about man page on %pre script
- remove distribution-specific macros and scripts
- remove unnecessary %clean script
- remove date macro
- moved install-time "chkconfig --list" from %post to %pre
- add Summary(ja) and description -l ja
- remove Summary and description other than English and Japanese
