#!/usr/bin/env bash
set -eu

cd $(dirname ${BASH_SOURCE[0]})/SOURCES

#Get EPEL full list
curl -L -O https://dl.fedoraproject.org/pub/epel/fullfilelist
#Get latest golang src rpm
curl -L -O https://dl.fedoraproject.org/pub/epel/$(grep '^6/SRPMS/golang-[0-9].*src.rpm' fullfilelist)
rpm2cpio golang-*.src.rpm | cpio -diuv
#Patch the spec file to patch the build to work on CentOS 5
sed -ri 's|(^%build)|\1\nsed -i '"'"'s:.*--build-id.*::'"'"' ./src/cmd/go/build.go|' golang*.spec
#Make SPEC CentOS 5 compliant
sed -ri 's|(^Name:.*)|\1\nGroup: Software|' golang.spec
sed -ri 's|(^Name:.*)|\1\nBuildRoot:      %(echo %{_topdir}/BUILDROOT/%{name}-%{version})|' golang.spec
sed -ri 's|(^%package\s.*)|\1\nGroup: Software|' golang.spec
sed -i 's|%ifarch %{ix86}|%if %_arch == i686|' golang.spec
sed -i 's|%ifarch %{arm}|%if %_arch == armv6l|' golang.spec
sed -i 's|%ifarch|%if %_arch ==|' golang.spec
#The test WILL fail, so make the rpm not fail
sed -ri 's;(.*run.bash.*);\1|true;' golang.spec
mv golang.spec ../SPECS/

