%define gopath %{_builddir}/go

Name:           otelcol-contrib
Version:        0.152.0
Release:        2
Summary:        OpenTelemetry Collector Contrib for AIX
License:        Apache-2.0
URL:            https://github.com/open-telemetry/opentelemetry-collector-contrib
Source0:        https://github.com/open-telemetry/opentelemetry-collector-contrib/archive/refs/tags/v%{version}.tar.gz
Source1:        builder-config-aix.yaml
Source2:        runtime-config-aix.yaml
Source3:        README_OTEL_COLLECTOR_AIX.md
Source4:        gopsutil-4.25.11.tar.gz
Source5:	    README_otelcol_krb5.md

# AIX-specific patches
Patch0:         otelcol-contrib-0.152.0-enable-aix-build-targets-for-process-scrapers.patch
Patch1:         otelcol-contrib-0.152.0-enable-aix-support-for-system-scraper.patch
Patch2:			otelcol-contrib-0.152.0-kafka-version.patch

BuildRequires:  golang >= 1.21

%description
OpenTelemetry Collector Contrib is a distribution of the OpenTelemetry Collector
that includes additional components beyond the core collector. This build is
customized for AIX with specific receivers, processors, and exporters.

%prep
%setup -q -n opentelemetry-collector-contrib-%{version}

# Extract custom gopsutil for AIX
mkdir -p %{_builddir}/gopsutil
tar -xzf %{SOURCE4} -C %{_builddir}/gopsutil

# Apply AIX-specific patches
%patch0 -p1
%patch1 -p1
%patch2 -p1

%build
# Setup Go environment
export GOPATH=%{gopath}
export PATH=${GOPATH}/bin:/opt/freeware/lib/golang/bin:$PATH
export GOOS=aix
export GOARCH=ppc64
export CGO_ENABLED=1

# Install OpenTelemetry Collector Builder
echo "Installing OpenTelemetry Collector Builder v%{version}..."
go install go.opentelemetry.io/collector/cmd/builder@v%{version}

# Verify builder installation
if [ ! -f "${GOPATH}/bin/builder" ]; then
    echo "ERROR: Builder installation failed"
    exit 1
fi

# Check builder version
echo "Checking builder version..."
${GOPATH}/bin/builder version || echo "Warning: Could not get builder version"

echo "Builder successfully installed at: ${GOPATH}/bin/builder"

# Build the collector using builder with SOURCE1 config
echo "Building OpenTelemetry Collector with custom configuration..."
${GOPATH}/bin/builder --config=%{SOURCE1}

# Verify the build
if [ ! -f "./otelcol-aix/otelcol-contrib" ]; then
    echo "ERROR: Collector build failed - binary not found"
    exit 1
fi

# Verify binary is executable
test -x ./otelcol-aix/otelcol-contrib || exit 1


%install

# Create installation directories
mkdir -p ${RPM_BUILD_ROOT}%{_bindir}
mkdir -p ${RPM_BUILD_ROOT}%{_docdir}/otelcol
mkdir -p ${RPM_BUILD_ROOT}%{_datadir}/otelcol

# Install the collector binary, documentation, configuration files
install -D -p -m 0755 %{_builddir}/opentelemetry-collector-contrib-%{version}/otelcol-aix/otelcol-contrib ${RPM_BUILD_ROOT}%{_bindir}/otelcol-contrib
install -D -p -m 0644 %{SOURCE2}  ${RPM_BUILD_ROOT}%{_datadir}/otelcol/runtime-config-aix.yaml
install -D -p -m 0644 %{_builddir}/opentelemetry-collector-contrib-%{version}/README.md ${RPM_BUILD_ROOT}%{_docdir}/otelcol/README.md
install -D -p -m 0644 %{SOURCE3}  ${RPM_BUILD_ROOT}%{_docdir}/otelcol/README_OTEL_COLLECTOR_AIX.md
install -D -p -m 0644 %{SOURCE5}  ${RPM_BUILD_ROOT}%{_docdir}/otelcol/README_otelcol_krb5.md


%files
%defattr(-,root,system,-)
%dir %{_docdir}/otelcol
%dir %{_datadir}/otelcol
%{_bindir}/otelcol-contrib
%{_docdir}/otelcol/README.md
%{_docdir}/otelcol/README_OTEL_COLLECTOR_AIX.md
%{_docdir}/otelcol/README_otelcol_krb5.md
%{_datadir}/otelcol/runtime-config-aix.yaml


%changelog
* Tue Aug 25 2026 Sangamesh Mallayya <smallayy@in.ibm.com> - 0.152.0-2
- Enable the Kafka exporter for Kerberos Authentication.

* Tue Jun 18 2026 Lakshmi Surekha Kovvuri <Lakshmi.Kovvuri@ibm.com> - 0.152.0-1
- Initial port to AIX.
