# Tests by default. No tests: rpmbuild -ba --define 'dotests 0' *.spec
%{!?dotests: %define dotests 1}
%define _libdir64 %{_prefix}/lib64
%define python_sitearch %{_libdir}/python3.9/site-packages
%define python_sitearch64 %{_libdir64}/python3.9/site-packages

# Current version of librrd.so is 8 (may have to include old versions 4 and 2)
# Previous package 1.4.7 builds version 4 and included older version 2
%global librrdver 8

%define rrdcached_user rrdcache
%define rrdcached_group rrdcache

%define perl  %{_bindir}/perl
%define perl_version  %(eval "`%{perl} -V:version`" ; echo $version | sed "s|\.[0-9]*$||")
%define perl_vendorlib %(eval "`%{perl} -V:installvendorlib`"; echo $installvendorlib)
%define perl_vendorarch_64 %(eval "`%{perl} -V:installvendorarch`"; echo $installvendorarch)

%define perl_32  %{_bindir}/perl_32
%define perl_vendorarch_32 %(eval "`%{perl_32} -V:installvendorarch`"; echo $installvendorarch)

Name: rrdtool
Version: 1.8.0
Release: 1
Summary: Round Robin Database Tool to store and display time-series data
License: GPL
Group: Applications/Databases
URL: https://oss.oetiker.ch/rrdtool
Source0: https://oss.oetiker.ch/rrdtool/pub/%{name}-%{version}.tar.gz
Source10: rrdcached.aix.init
Source11: rrdcached.sysconfig

Patch0: %{name}-1.7.2-aix.patch
Patch2: %{name}-1.7.2-aixconf.patch
Patch3: %{name}-1.7.0-ruby-ldflags.patch
Patch4: %{name}-1.8.0-xlcsyntax.patch 
#Patch5: %{name}-1.7.2-python.patch
Patch6: rrdtool-1.7.2-libxml2-hardcode.patch
Patch7: rrdtool-1.7.2-vsyslog.aix.patch
Patch8: rrdtool-1.7.2-doc.patch

BuildRequires: coreutils, make

BuildRequires: cairo-devel >= 1.14.6
BuildRequires: freetype2-devel >= 2.12.1
BuildRequires: gettext >= 0.21
BuildRequires: glib2-devel >= 2.76.3
BuildRequires: libdbi-devel >= 0.9.0
BuildRequires: libpng-devel >= 1.6.37
BuildRequires: libxml2-devel >= 2.10.4
BuildRequires: lua >= 5.4.4
BuildRequires: lua-devel >= 5.4.4
BuildRequires: pango-devel >= 1.40.1
BuildRequires: perl >= 5.38.0
BuildRequires: python3.9 >= 3.9.18
BuildRequires: tcl-devel >= 8.6.13
BuildRequires: zlib-devel >= 1.2.13
BuildRequires: tcl-static >= 8.6.13

Requires: cairo >= 1.14.6
Requires: freetype2 >= 2.12.1
Requires: gettext >= 0.21
Requires: glib2 >= 2.76.3
Requires: libdbi >= 0.9.0
Requires: libpng >= 1.6.37
Requires: libxml2 >= 2.10.4
Requires: pango >= 1.40.1
Requires: zlib >= 1.2.13
Requires: libgcc >= 10.3.0
Requires: xz-libs >= 5.4.3

Requires: dejavu-sans-mono-fonts

%description
RRD is the Acronym for Round Robin Database. RRD is a system to store and 
display time-series data (i.e. network bandwidth, machine-room temperature, 
server load average). It stores the data in a very compact way that will not 
expand over time, and it presents useful graphs by processing the data to 
enforce a certain data density. It can be used either via simple wrapper 
scripts (from shell or Perl) or via frontends that poll network devices and 
put a friendly user interface on it.


%package devel
Summary: RRDtool static libraries and header files
Group: Development/Libraries
Requires: %{name} = %{version}-%{release}
Requires: cairo-devel >= 1.14.6
Requires: freetype2-devel >= 2.12.1
Requires: gettext-devel >= 0.21
Requires: glib2-devel >= 2.76.3
Requires: libdbi-devel >= 0.9.0
Requires: libpng-devel >= 1.6.37
Requires: libxml2-devel >= 2.10.4
Requires: pango-devel >= 1.40.1
Requires: tcl-devel >= 8.6.13
Requires: zlib-devel >= 1.2.13

%description devel
RRD is the Acronym for Round Robin Database. RRD is a system to store and
display time-series data (i.e. network bandwidth, machine-room temperature,
server load average). This package allow you to use directly this library.

If you are compiling a 32-bit program, no special compiler options are
needed.

If you are compiling a 64-bit program, you have to compile and link your
application with "xlc_r -q64" or "gcc -maix64".


%package cached
Summary: Data caching daemon for RRDtool
Group: System/Servers
Requires: %{name} = %{version}-%{release}
Requires: glib2 >= 2.76.3
Requires: libdbi >= 0.9.0
Requires: libxml2 >= 2.10.4
Requires: gettext >= 0.21

%description cached
rrdcached is a daemon that receives updates to existing RRD files,
accumulates them and, if enough have been received or a defined time has
passed, writes the updates to the RRD file. The daemon was written with
big setups in mind which usually runs into I/O related problems. This
daemon was written to alleviate these problems.


%package perl
Summary: Perl RRDtool bindings
Group: Development/Languages
Requires: %{name} = %{version}-%{release}
Requires: perl >= 5.38.0
Provides: %{name}-perl

%description perl
The Perl RRDtool bindings.


%{!?luaver: %global luaver %(lua -e "print(string.sub(_VERSION, 5))")}
%global lualibdir %{_libdir}/lua/%{luaver}
%global lualibdir64 %{_libdir64}/lua/%{luaver}
%global luapkgdir %{_datadir}/lua/%{luaver}

%package lua
Summary: Lua RRDtool bindings
Group: Development/Languages
Requires: %{name} = %{version}-%{release}
Requires: lua >= %{luaver}
Requires: libgcc >= 10.3.0
Requires: glib2 >= 2.76.3
Requires: gettext >= 0.21
Requires: pango >= 1.40.1

%description lua
The %{name}-lua package includes RRDtool bindings for Lua.


%package python
Summary: Python RRDtool bindings
Group: Development/Languages
Requires: %{name} = %{version}-%{release}
Requires: python3.9 >= 3.9.16
Requires: glib2 >= 2.76.3
Requires: gettext >= 0.21
Obsoletes: python-%{name} < %{version}-%{release}
Provides: python-%{name} = %{version}-%{release}

%description python
The Python RRDtool bindings.


%{!?ruby_vendorarchdir: %global ruby_vendorarchdir %(ruby_32 -rrbconfig -e 'puts RbConfig::CONFIG["vendorarchdir"]')}
%{!?ruby_vendorarchdir64: %global ruby_vendorarchdir64 %(ruby_64 -rrbconfig -e 'puts RbConfig::CONFIG["vendorarchdir"]')}

%package ruby
Summary: Ruby RRDtool bindings
Group: Development/Languages
BuildRequires: ruby, ruby-devel
Requires: %{name} = %{version}-%{release}
Requires: ruby >= 3.1.4
Requires: libgcc >= 10.3.0

%description ruby
The %{name}-ruby package includes RRDtool bindings for Ruby.


%package tcl
Summary: Tcl RRDtool bindings
Group: Development/Languages
BuildRequires: tcl-devel >= 8.6.13
Requires: tcl >= 8.6.13
Requires: libgcc >= 10.3.0
Requires: glib2 >= 2.76.3
Requires: gettext >= 0.21
Requires: pango >= 1.40.1
Requires: %{name} = %{version}-%{release}
Obsoletes: tcl-%{name} < %{version}-%{release}
Provides: tcl-%{name} = %{version}-%{release}

%description tcl
The %{name}-tcl package includes RRDtool bindings for Tcl.


%prep
%setup -q
%patch0 -b .aix
%patch2 -b .aixconf
%patch4 -p0
#%patch5 
%patch6
%patch7
%patch8


# Duplicate source for 32 & 64 bits
rm -rf   /tmp/%{name}-%{version}-32bit
cp -pr . /tmp/%{name}-%{version}-32bit
rm -fr *
mv       /tmp/%{name}-%{version}-32bit 32bit
cp -pr 32bit 64bit


# Patch tcl LDFLAGS for 32 and 64 bit versions of bindings/tcl/Makefile.in
%patch3 -p1 -b .ruby-ldflags
%build

export PATH=/opt/freeware/bin:$PATH
export RM="/usr/bin/rm -f"


export CFLAGS="$CFLAGS -I/opt/freeware/include -I/opt/freeware/include/cairo -I/opt/freeware/include/pango-1.0/pango -I/opt/freeware/include/glib-2.0 -I/opt/freeware/include/pango-1.0"
cd 64bit
# first build the 64-bit version

export CC="gcc -maix64 -O2 -D__64BIT__"
export AR="/usr/bin/ar -X64"
export RM="/usr/bin/rm -f"
export OBJECT_MODE=64

export LIBTOOLIZE_OPTIONS="--warnings=all"
export PERLCC=$CC
export PKG_CONFIG_PATH=/opt/freeware/lib64/pkgconfig
export MAKE=gmake
export PKG_CONFIG=/opt/freeware/bin/pkg-config

export PERL=/opt/freeware/bin/perl_64bit
export PYTHON=/opt/freeware/bin/python3.9

export LDFLAGS="-L/opt/freeware/lib64 -L/usr/lib64 -L/opt/freeware/lib -L/usr/lib -lpango-1.0 -lcairo -lgobject-2.0 -lintl -Wl,-blibpath:/opt/freeware/lib64:/opt/freeware/lib:/usr/lib64:/usr/lib:/lib"

export NROFF=/usr/bin/nroff


./configure \
    --prefix=%{_prefix} \
    --libdir=%{_libdir64} \
    --mandir=%{_prefix}/share/man \
    --disable-silent-rules \
    --disable-rpath \
    --enable-shared --disable-static \
    --disable-mmap \
    --enable-perl \
    --with-perl-options='INSTALLDIRS="vendor"' \
    --enable-python \
    --enable-lua \
    --enable-tcl \
    --enable-tcl-site \
    --with-tcllib=%{_libdir64} \
    --enable-ruby \
    --with-pic
gmake 

if [ "%{dotests}" == 1 ]
then
    (gmake -k check || true)
fi

cd ../32bit
# now build the 32-bit version

export CC="gcc -maix32 -D_LARGE_FILES -O2"
export AR="/usr/bin/ar -X32"
export RM="/usr/bin/rm -f"
export OBJECT_MODE=32

export LIBTOOLIZE_OPTIONS="--warnings=all"
export PERLCC=$CC
export PKG_CONFIG_PATH=/opt/freeware/lib/pkgconfig
export MAKE=gmake
export PKG_CONFIG=/opt/freeware/bin/pkg-config_32

export PERL=/opt/freeware/bin/perl_32
export PYTHON=/opt/freeware/libexec/python3.9_32
export RUBY=/opt/freeware/bin/ruby_32
export LDFLAGS="-L/opt/freeware/lib -lpango-1.0 -lgobject-2.0 -lcairo -lintl -Wl,-blibpath:/opt/freeware/lib:/usr/lib:/lib -Wl,-bmaxdata:0x80000000"


./configure \
    --prefix=%{_prefix} \
    --mandir=%{_prefix}/share/man \
    --disable-silent-rules \
    --disable-rpath \
    --enable-shared --disable-static \
    --disable-mmap \
    --enable-perl \
    --with-perl-options='INSTALLDIRS="vendor"' \
    --enable-python \
    --enable-lua \
    --enable-tcl \
    --enable-tcl-site \
    --with-tcllib=%{_libdir} \
    --enable-ruby \
    --with-pic 
gmake

if [ "%{dotests}" == 1 ]
then
    (gmake -k check || true)
fi

%install
[ "${RPM_BUILD_ROOT}" != "/" ] && rm -rf ${RPM_BUILD_ROOT}
export RM="/usr/bin/rm -f"

cd 64bit

export OBJECT_MODE=64
export CC="gcc -maix64 -O2 -D__64BIT__"
export AR="/usr/bin/ar -X64"
export RM="/usr/bin/rm -f"
export LIBTOOLFLAGS="--warnings=all"


gmake DESTDIR=${RPM_BUILD_ROOT} install

mkdir -p  ${RPM_BUILD_ROOT}%{ruby_vendorarchdir64}
cp  bindings/ruby/RRD.so  ${RPM_BUILD_ROOT}%{ruby_vendorarchdir64}/RRD.so

(
  cd ${RPM_BUILD_ROOT}%{_bindir}
  for f in * ; do
    mv -f ${f} ${f}_64
  done
)

# Move perl modules RRDp.pm, etc
mkdir -p $RPM_BUILD_ROOT%{perl_vendorarch_64}
mv $RPM_BUILD_ROOT%{perl_vendorlib}/RRDp.pm $RPM_BUILD_ROOT%{perl_vendorarch_64}/

cd ../32bit

export OBJECT_MODE=32
export CC="gcc -maix32 -O2 -D_LARGE_FILES"
export AR="/usr/bin/ar -X32"
export RM="/usr/bin/rm -f"
export LDFLAGS="-Wl,-bmaxdata:0x80000000"
export LIBTOOLFLAGS="--warnings=all"

gmake DESTDIR=${RPM_BUILD_ROOT} install

(
    cd  ${RPM_BUILD_ROOT}/%{_prefix}/bin
    for fic in $(ls -1| grep -v -e _32 -e _64)
    do
	mv $fic "$fic"_32
	ln -sf "$fic"_64 $fic
    done
)

mkdir -p  ${RPM_BUILD_ROOT}%{ruby_vendorarchdir}
cp  ./bindings/ruby/RRD.so  ${RPM_BUILD_ROOT}%{ruby_vendorarchdir}/RRD.so

/usr/bin/strip -X32_64 ${RPM_BUILD_ROOT}%{_bindir}/* || :

(
  cd ${RPM_BUILD_ROOT}%{_libdir64}
  for f in *.a ; do
    /usr/bin/ar -X64 -x ${f}
  done

  cd ${RPM_BUILD_ROOT}%{lualibdir64}
  for f in *.a ; do
    /usr/bin/ar -X64 -x ${f}
  done

  cd ${RPM_BUILD_ROOT}%{_libdir}
  for f in *.a ; do
    /usr/bin/ar -X32 -x ${f}
  done

  cd ${RPM_BUILD_ROOT}%{lualibdir}
  for f in *.a ; do
    /usr/bin/ar -X32 -x ${f}
  done
)

# add the 64-bit shared objects to the shared library containing already the
# 32-bit shared objects
/usr/bin/ar -X64 -q ${RPM_BUILD_ROOT}%{_libdir}/librrd.a ${RPM_BUILD_ROOT}%{_libdir64}/librrd.so.%{librrdver}
# librrd_th no longer in rrdtool ?
# /usr/bin/ar -X64 -q ${RPM_BUILD_ROOT}%{_libdir}/librrd_th.a ${RPM_BUILD_ROOT}%{_libdir64}/librrd_th.so.%{librrdver}
/usr/bin/ar -X64 -q ${RPM_BUILD_ROOT}%{lualibdir}/rrd.a ${RPM_BUILD_ROOT}%{lualibdir64}/rrd.so.0


# Move perl modules RRDp.pm, etc
mkdir -p $RPM_BUILD_ROOT%{perl_vendorarch_32}
mv $RPM_BUILD_ROOT%{perl_vendorlib}/RRDp.pm $RPM_BUILD_ROOT%{perl_vendorarch_32}/


# We only want .txt and .html files for the main documentation
mkdir -p rpm-doc/docs/
/opt/freeware/bin/cp -ap doc/*.txt doc/*.html rpm-doc/docs/

rm -f examples/Makefile* examples/*.in

# install rrdcached files
mkdir -p ${RPM_BUILD_ROOT}/etc/sysconfig
mkdir -p ${RPM_BUILD_ROOT}/etc/rc.d/init.d
mkdir -p ${RPM_BUILD_ROOT}/etc/rc.d/rc2.d
mkdir -p ${RPM_BUILD_ROOT}/etc/rc.d/rc3.d
mkdir -p ${RPM_BUILD_ROOT}/var/lib/rrdcached
mkdir -p ${RPM_BUILD_ROOT}/var/run/rrdcached
ln -sf '../init.d/rrdcached' ${RPM_BUILD_ROOT}/etc/rc.d/rc2.d/Srrdcached
ln -sf '../init.d/rrdcached' ${RPM_BUILD_ROOT}/etc/rc.d/rc2.d/Krrdcached
ln -sf '../init.d/rrdcached' ${RPM_BUILD_ROOT}/etc/rc.d/rc3.d/Srrdcached
ln -sf '../init.d/rrdcached' ${RPM_BUILD_ROOT}/etc/rc.d/rc3.d/Krrdcached

cp %{SOURCE10} ${RPM_BUILD_ROOT}/etc/rc.d/init.d/rrdcached
chmod 0755 ${RPM_BUILD_ROOT}/etc/rc.d/init.d/rrdcached

cp %{SOURCE11} ${RPM_BUILD_ROOT}/etc/sysconfig/rrdcached
chmod 0644 ${RPM_BUILD_ROOT}/etc/sysconfig/rrdcached

# clean up buildroot
rm -rf ${RPM_BUILD_ROOT}%{_libdir}/perl/5.8.*/ppc-aix*/*/*/.packlist

(
 cd $RPM_BUILD_ROOT
 grep -rl "\/usr\/bin\/python" . | xargs /opt/freeware/bin/sed -i 's|/usr/bin/python3|/opt/freeware/bin/python3.9|g'
 grep -rl "\/usr\/bin\/perl" . | xargs /opt/freeware/bin/sed -i 's|/usr/bin/perl|/opt/freeware/bin/perl|g'
 cd -
 /opt/freeware/bin/sed -i 's|/usr/bin/python|/opt/freeware/bin/python3.9|g' examples/stripes.py
)


%pre cached
# add the "%{rrdcached_group}" group only if it does not yet exist
result=`/usr/sbin/lsgroup %{rrdcached_group} | /usr/bin/awk '{ print $1 }' 2>/dev/null`
if [[ "${result}" != "%{rrdcached_group}" ]] ; then
    /usr/bin/mkgroup %{rrdcached_group} 2> /dev/null || :
fi
# add the "%{rrdcached_user}" user only if it does not yet exist
result=`/usr/sbin/lsuser %{rrdcached_user} | /usr/bin/awk '{ print $1 }' 2>/dev/null`
if [[ "${result}" != "%{rrdcached_user}" ]] ; then
    /usr/bin/mkuser pgrp=%{rrdcached_group} gecos='RRDcached User' \
        login='false' rlogin='false' %{rrdcached_user} 2> /dev/null || :
fi

%preun cached
/etc/rc.d/init.d/rrdcached stop > /dev/null 2>&1

%postun cached
# remove "rrdcache" user and group
/usr/sbin/rmuser -p %{rrdcached_user} || :
/usr/sbin/rmgroup %{rrdcached_group} || :

%clean
[ "${RPM_BUILD_ROOT}" != "/" ] && rm -rf ${RPM_BUILD_ROOT}

%files
%defattr(-,root,system,-)
%doc 32bit/LICENSE
%doc 32bit/CHANGES 32bit/CONTRIBUTORS 32bit/COPYRIGHT
%doc 32bit/NEWS 32bit/THREADS 32bit/TODO
%doc 32bit/rpm-doc/docs/
%{_bindir}/rrdcgi*
%{_bindir}/rrdtool*
%{_bindir}/rrdupdate*
%{_libdir}/*.a
%{_libdir}/*.so*
%{_libdir64}/*.so*
%{_prefix}/share/man/man1/[b,c]*
%{_prefix}/share/man/man1/rpn*
%{_prefix}/share/man/man1/rrd-beginners.1
%{_prefix}/share/man/man1/rrd[a-b,d-i,r-x]*
%{_prefix}/share/man/man1/rrdcgi.1
%{_prefix}/share/man/man1/rrdcreate.1
%{_prefix}/share/man/man1/rrdlast*.1

%files devel
%defattr(-,root,system,-)
%{_includedir}/*
%{_libdir}/pkgconfig/*.pc
%{_libdir64}/pkgconfig/*.pc
%{_prefix}/share/man/man3/librrd.3

%files cached
%defattr(-,root,system,-)
%{_bindir}/rrdcached*
%config /etc/sysconfig/rrdcached
/etc/rc.d/init.d/rrdcached
/etc/rc.d/rc2.d/Srrdcached
/etc/rc.d/rc2.d/Krrdcached
/etc/rc.d/rc3.d/Srrdcached
/etc/rc.d/rc3.d/Krrdcached
%{_prefix}/share/man/man1/rrdcached*.1
%attr(0755,%{rrdcached_user},%{rrdcached_group}) %dir /var/lib/rrdcached
%attr(0755,%{rrdcached_user},%{rrdcached_group}) %dir /var/run/rrdcached

%files perl
%defattr(-,root,system,-)
%{perl_vendorarch_32}/*.pm
%{perl_vendorarch_64}/*.pm
%attr(0755,root,system) %{perl_vendorarch_32}/auto/RRDs/
%attr(0755,root,system) %{perl_vendorarch_64}/auto/RRDs/
%{_datadir}/%{name}

%files lua
%defattr(-,root,system,-)
%doc 32bit/bindings/lua/README
%doc 32bit/rpm-doc/docs/*lua*
%{lualibdir}/*
%{lualibdir64}/*
%{_prefix}/share/man/man1/rrdlua.1

%files python
%defattr(-,root,system,-)
%doc 32bit/bindings/python/COPYING
%doc 32bit/bindings/python/README.md
%{python_sitearch}/rrdtool.cpython-39.so
%{python_sitearch}/rrdtool-*.egg-info
%{python_sitearch64}/rrdtool.cpython-39.so
%{python_sitearch64}/rrdtool-*.egg-info

%files ruby
%doc 32bit/bindings/ruby/README
%{ruby_vendorarchdir}/RRD.so
%{ruby_vendorarchdir64}/RRD.so

%files tcl
%defattr(-,root,system,-)
%doc 32bit/bindings/tcl/README
%{_libdir}/tclrrd*.so
%{_libdir64}/tclrrd*.so
%{_libdir}/%{name}/*.tcl
%{_libdir64}/%{name}/*.tcl

%changelog
* Tue Jan 09 2024 Shubham Gupta <shubham.gupta36@ibm.com> - 1.8.0-1 
- Update to version 1.8.0

* Fri Jan 05 2024 Reshma V Kumar <reskumar@in.ibm.com> 1.7.2-5
- Rebuild with perl 5.38.0

* Wed Oct 04 2023 Ranjit Ranjan <ranjit.ranjan@ibm.com> 1.7.2-4
- Rebuild for ruby 3.1.4.

* Thu Mar 30 2023 Harshith K A <harshith.k.a@ibm.com> 1.7.2-3
- Rebuild for python3.9

* Wed May 11 2022 Reshma V Kumar <reskumar@in.ibm.com> 1.7.2-2
- Rebuild with perl 5.34.1

* Wed Jan 05 2021 Sushree Das <sushree.das@ibm.com> 1.7.2-1
- Updated to 1.7.2

* Wed Sep 30 2020 Reshma V Kumar <reskumar@in.ibm.com> 1.7.0-8
- Build with ruby 2.7.1
- Build with python3
- Stop shipping files in /usr directory

* Thu Jul 02 2020 Reshma V Kumar <reskumar@in.ibm.com> 1.7.0-7
- Build with perl 5.30.2
- Remove .la files

* Fri Oct 11 2019 Reshma V Kumar <reskumar@in.ibm.com> 1.7.0-6
- Rebuild to make 64bit as default

* Thu Aug 29 2019 Reshma V Kumar <reskumar@in.ibm.com> 1.7.0-5
- Rebuild to fix wrong library linking

* Tue Jan 08 2019 Reshma V Kumar <reskumar@in.ibm.com> 1.7.0-4
- Rebuild with gcc
- Rebuild after stripping old libraries

* Fri Mar 09 2018 Ravi Hirekurabar<rhirekur@in.ibm.com> 1.7.0-3
- Built using IBM toolbox environment. 
* Tue Feb 20 2018 Michael Wilson <michael.a.wilson@atos.net> 1.7.0-2
- Add Ruby bindings

* Wed Feb 14 2018 Michael Wilson <michael.a.wilson@atos.net> 1.7.0-1
- Update to 1.7.0
- Modifications inspired by Fedora SPEC file

* Tue Mar 27 2012 Gerard Visiedo <gerard.visido@bull.net> 3.2.0-1
- Initial port on Aix6.1

