{{ ansible_managed | comment }}
%global version 0.11.0~dev

%define debug_package  %{nil}
%global _logdir	       /var/log/%name
%global _spooldir      /var/spool/%name
%global _libdir	       /var/lib/%name

Name:           cgrates
Version:        %{version}
Release:        {{ releaseTag }}
Summary:        Carrier Grade Real-time Charging System
License:        GPLv3
URL:            https://github.com/cgrates/cgrates
Source0:        https://github.com/cgrates/cgrates/archive/{{ cgrates_branch }}/{{ gitVersion.stdout_lines[0] }}.tar.gz

%if 0%{?fedora} > 16 || 0%{?rhel} > 6
Requires(pre): shadow-utils
Requires(post): systemd
Requires(preun): systemd
Requires(postun): systemd
%else
Requires(post): chkconfig
Requires(preun):chkconfig
Requires(preun):initscripts
%endif

%description
CGRateS is a very fast and easy scalable real-time charging system for Telecom environments.

%prep
%setup -q -n %{name}-%{version} -c
mkdir -p src/github.com/cgrates
ln -sf ../../../$(ls |grep %{name}-) src/github.com/cgrates/cgrates

%pre
getent group %{name} >/dev/null || groupadd -r %{name}
getent passwd %{name} >/dev/null || \
useradd -r -g %{name} -d %{_localstatedir}/run/%{name} -s /sbin/nologin \
-c "CGRateS" %{name} 2>/dev/null || :

%post
%if 0%{?fedora} > 16 || 0%{?rhel} > 6
if [ $1 -eq 1 ] ; then
	# Initial installation
	/bin/systemctl daemon-reload >/dev/null 2>&1 || :
fi
%else
/sbin/chkconfig --add %{name}
%endif
/bin/chown -R %{name}:%{name} %{_logdir}
/bin/chown -R %{name}:%{name} %{_spooldir}
/bin/chown -R %{name}:%{name} %{_libdir}
sudo systemctl restart rsyslog

%preun
%if 0%{?fedora} > 16 || 0%{?rhel} > 6
if [ $1 -eq 0 ] ; then
	# Package removal, not upgrade
	/bin/systemctl --no-reload disable %{name}.service > /dev/null 2>&1 || :
	/bin/systemctl stop %{name}.service > /dev/null 2>&1 || :
fi
%else
if [ $1 = 0 ]; then
	/sbin/service %{name} stop > /dev/null 2>&1
	/sbin/chkconfig --del %{name}
fi
%endif

%build
cd $RPM_BUILD_DIR/%{name}-%{version}/src/github.com/cgrates/cgrates
./build.sh

%install
rm -rf $RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT%{_datarootdir}/%{name}
cp -rpf {{ cgrates_dir }}/data/conf/ $RPM_BUILD_ROOT%{_datarootdir}/%{name}
cp -rpf {{ cgrates_dir }}/data/diameter/ $RPM_BUILD_ROOT%{_datarootdir}/%{name}
cp -rpf {{ cgrates_dir }}/data/postman/ $RPM_BUILD_ROOT%{_datarootdir}/%{name}
cp -rpf {{ cgrates_dir }}/data/radius/ $RPM_BUILD_ROOT%{_datarootdir}/%{name}
cp -rpf {{ cgrates_dir }}/data/tariffplans/ $RPM_BUILD_ROOT%{_datarootdir}/%{name}
cp -rpf {{ cgrates_dir }}/data/tutorial_tests/ $RPM_BUILD_ROOT%{_datarootdir}/%{name}
cp -rpf {{ cgrates_dir }}/data/tutorials/ $RPM_BUILD_ROOT%{_datarootdir}/%{name}
cp -rpf {{ cgrates_dir }}/data/storage/mongo $RPM_BUILD_ROOT%{_datarootdir}/%{name}/storage
cp -rpf {{ cgrates_dir }}/data/storage/mysql $RPM_BUILD_ROOT%{_datarootdir}/%{name}/storage
cp -rpf {{ cgrates_dir }}/data/storage/postgres $RPM_BUILD_ROOT%{_datarootdir}/%{name}/storage
install -D -m 0644 -p {{ cgrates_dir }}/data/conf/%{name}/%{name}.json $RPM_BUILD_ROOT%{_sysconfdir}/%{name}/%{name}.json
install -D -m 0755 -p {{ golang_gopath }}/bin/cgr-console $RPM_BUILD_ROOT%{_bindir}/cgr-console
install -D -m 0755 -p {{ golang_gopath }}/bin/cgr-engine $RPM_BUILD_ROOT%{_bindir}/cgr-engine
install -D -m 0755 -p {{ golang_gopath }}/bin/cgr-loader $RPM_BUILD_ROOT%{_bindir}/cgr-loader
install -D -m 0755 -p {{ golang_gopath }}/bin/cgr-tester $RPM_BUILD_ROOT%{_bindir}/cgr-tester
install -D -m 0755 -p {{ golang_gopath }}/bin/cgr-migrator $RPM_BUILD_ROOT%{_bindir}/cgr-migrator
mkdir -p $RPM_BUILD_ROOT%{_logdir}/cdre/csv
mkdir -p $RPM_BUILD_ROOT%{_logdir}/cdre/fwv
mkdir -p $RPM_BUILD_ROOT%{_spooldir}/cdre/csv
mkdir -p $RPM_BUILD_ROOT%{_spooldir}/cdre/fwv
mkdir -p $RPM_BUILD_ROOT%{_spooldir}/tpe
mkdir -p $RPM_BUILD_ROOT%{_spooldir}/failed_posts
mkdir -p $RPM_BUILD_ROOT%{_spooldir}/analyzers
mkdir -p $RPM_BUILD_ROOT%{_libdir}/history
mkdir -p $RPM_BUILD_ROOT%{_libdir}/cache_dump
mkdir -p $RPM_BUILD_ROOT/etc/logrotate.d
mkdir -p $RPM_BUILD_ROOT/etc/rsyslog.d
install -m 755 {{ cgrates_dir }}/data/conf/logging/logrotate.conf $RPM_BUILD_ROOT/etc/logrotate.d/%{name}
install -m 755 {{ cgrates_dir }}/data/conf/logging/rsyslog.conf $RPM_BUILD_ROOT/etc/rsyslog.d/%{name}.conf
install -D -m 0644 -p src/github.com/cgrates/cgrates/packages/redhat_fedora/%{name}.options $RPM_BUILD_ROOT%{_sysconfdir}/sysconfig/%{name}
%if 0%{?fedora} > 16 || 0%{?rhel} > 6
	install -D -m 0644 -p src/github.com/cgrates/cgrates/packages/redhat_fedora/%{name}.service $RPM_BUILD_ROOT%{_unitdir}/%{name}.service
%else
	install -D -m 0755 -p src/github.com/cgrates/cgrates/packages/redhat_fedora/%{name}.init $RPM_BUILD_ROOT%{_initrddir}/%{name}
%endif

%files
%defattr(-,root,root,-)
%{_datarootdir}/%{name}/*
%{_bindir}/*
%config(noreplace) %{_sysconfdir}/%{name}/%{name}.json
%{_logdir}/*
%{_spooldir}/*
%{_libdir}/*
%{_sysconfdir}/sysconfig/%{name}
/etc/logrotate.d/%{name}
/etc/rsyslog.d/%{name}.conf
%if 0%{?fedora} > 16 || 0%{?rhel} > 6
	%{_unitdir}/%{name}.service
%else
	%{_initrddir}/%{name}
%endif

%clean
rm -rf $RPM_BUILD_DIR/%{name}-%{version}
rm -rf $RPM_BUILD_ROOT
