%if 0%{?fedora} || 0%{?rhel} == 6
%global with_devel 1
%global with_bundled 1
%global with_debug 1
# No test files so far
%global with_check 0
%global with_unit_test 0
%else
%global with_devel 0
%global with_bundled 0
%global with_debug 1
%global with_check 0
%global with_unit_test 0
%endif

%if 0%{?with_debug}
%global _dwz_low_mem_die_limit 0
%else
%global debug_package   %{nil}
%endif

%define copying() \
%if 0%{?fedora} >= 21 || 0%{?rhel} >= 7 \
%license %{*} \
%else \
%doc %{*} \
%endif

%global isgccgoarch 0
%if 0%{?gccgo_arches:1}
%ifarch %{gccgo_arches}
%global isgccgoarch 1
%endif
%endif

%global provider        github
%global provider_tld    com
%global project         mheese
%global repo            journalbeat
%global provider_prefix %{provider}.%{provider_tld}/%{project}/%{repo}
%global import_path     %{provider_prefix}
%global commit          95db868692955ed9260fcd873de02f8d5d8553c4
%global shortcommit     %(c=%{commit}; echo ${c:0:7})

Name:       %{repo}
Version:    5.5.0
Release:    1%{?dist}
Summary:    Journalbeat is a log shipper from systemd/journald to Logstash/Elasticsearch
License:    ASL 2.0
URL:        https://%{provider_prefix}
Source0:    https://%{provider_prefix}/archive/%{commit}/%{repo}-%{shortcommit}.tar.gz
Source1:    journalbeat.service

# If go_arches not defined fall through to implicit golang archs
%if 0%{?go_arches:1}
ExclusiveArch:  %{go_arches}
%else
ExclusiveArch:  %{ix86} x86_64 %{arm}
%endif
# If gccgo_arches does not fit or is not defined fall through to golang
%if %{isgccgoarch}
BuildRequires:   gcc-go >= %{gccgo_min_vers}
%else
BuildRequires:   golang
%endif

%{?systemd_requires}
BuildRequires: systemd
BuildRequires: golang >= 1.7.0
BuildRequires: systemd-devel
Requires: systemd

%description
%{summary}

Provides: journalbeat = %{version}-%{release}

%prep
%setup -q -n %{repo}-%{commit}

%build
# If gccgo_arches does not fit or is not defined fall through to golang
# gccco arches
%if %{isgccgoarch}
%if 0%{?gcc_go_build:1}
export GOCOMPILER='%{gcc_go_build}'
%else
echo "No compiler for SA"
exit 1
%endif
# golang arches (due to ExclusiveArch)
%else
%if 0%{?golang_build:1}
export GOCOMPILER='%{golang_build} -ldflags "$LDFLAGS"'
%else
export GOCOMPILER='go build -ldflags "$LDFLAGS"'
%endif
%endif

export LDFLAGS=""
%if 0%{?with_debug}
%if %{isgccgoarch}
export OLD_RPM_OPT_FLAGS="$RPM_OPT_FLAGS"
function gobuild {
export RPM_OPT_FLAGS="$OLD_RPM_OPT_FLAGS -B 0x$(head -c20 /dev/urandom|od -An -tx1|tr -d ' \n')"
eval ${GOCOMPILER} -a -v -x "$@";
}
%else
export OLD_LDFLAGS="$LDFLAGS"
function gobuild {
export LDFLAGS="$OLD_LDFLAGS -B 0x$(head -c20 /dev/urandom|od -An -tx1|tr -d ' \n')"
eval ${GOCOMPILER} -a -v -x "$@";
}
%endif
%else
function gobuild { eval ${GOCOMPILER} -a -v -x "$@"; }
%endif

# set working directory
mkdir -p src/%{provider}.%{provider_tld}/%{project}
ln -s ../../../ src/%{provider}.%{provider_tld}/%{project}/%{repo}

%if ! 0%{?with_bundled}
export GOPATH=$(pwd):%{gopath}
%else
# build from bundled dependencies
export GOPATH=$(pwd):$(pwd)/Godeps/_workspace:%{gopath}
%endif

# set environment variables
export GO_VERSION=$(go version | cut -d' ' -f3 | sed 's/go//')
export BUILDDATE=$(date +%Y%m%d-%H:%M:%S)

gobuild -o bin/%{repo} %{import_path}

%install
install -d %{buildroot}%{_bindir}
install -D -p -m 0755 bin/%{repo} %{buildroot}%{_bindir}/%{repo}
install -D -p -m 0644 %{SOURCE1} %{buildroot}%{_unitdir}/journalbeat.service
install -D -p -m 0644 etc/journalbeat.yml %{buildroot}%{_sysconfdir}/journalbeat.yml

%files
%{_bindir}/%{repo}
%{_unitdir}/journalbeat.service
%config(noreplace) %{_sysconfdir}/journalbeat.yml

%post
%systemd_post journalbeat.service

%preun
%systemd_preun journalbeat.service

%postun
%systemd_postun_with_restart journalbeat.service

%changelog
* Sat Aug 12 2017 Bernardo Donadio <bdonadio@stone.com.br> 5.5.0-1
- Update to journalbeat v5.5.0

* Fri May 19 2017 Bernardo Donadio <bdonadio@stone.com.br> 5.2-4
- Remove inexistent systemd entry

* Fri May 19 2017 Bernardo Donadio <bdonadio@stone.com.br> 5.2-3
- Install systemd service file

* Fri May 19 2017 Bernardo Donadio <bdonadio@stone.com.br> 5.2-2
- Add systemd scriptlets

* Fri May 19 2017 Bernardo Donadio <bdonadio@stone.com.br> 5.2-1
- new package built with tito