#!/bin/bash
#
# Script to build blueman from Fedora upstream
#
# 1.1 - Rebuild from Fedora 41 on EL9.6 (8th June 2025) [with fix for automake ver]
# 1.0 - Initial build for Fedora 36 (6th June 2024)


#------------------------------------------------------------------------------
# A path, a path!
#
DOWNLOAD=$HOME/Source/
SPECS=$HOME/rpmbuild/SPECS
SRPMS=$HOME/rpmbuild/SRPMS
RPMS=$HOME/rpmbuild/RPMS
BUILD=$HOME/rpmbuild/BUILD
EPEL=$HOME/epel/9

#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Check directories exist, and create them if they do not.
#
if [ ! -d $DOWNLOAD ]; then
        mkdir -p $DOWNLOAD
fi

#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Download source RPMs
#
echo -e "\n\e[1;36mDownloading src.rpms\e[0m\n"

# Path to mirror-server
#
VER=41
URI=rsync://mirror.gaztronics.net/mirror/dl.fedoraproject.org/pub/fedora/linux/releases/$VER/Everything/source/tree/Packages

rsync --no-motd $URI/b/blueman* $DOWNLOAD
rsync --no-motd $URI/p/python-caja* $DOWNLOAD


#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Check for build dependencies
#
echo -e "\n\e[1;34mRunning build dependency check...\e[0m\n"

sudo dnf -y builddep --srpm --skip-unavailable $DOWNLOAD/*

#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Clear old files and install source RPMs
#
echo -e "\n\e[1;35mInstalling src.rpms\e[0m\n"

rm -fr $HOME/rpmbuild

rpm -i --quiet --nosignature $DOWNLOAD/*

#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Patch to use older Automake
#
mkdir $BUILD
tar zxf $HOME/rpmbuild/SOURCES/blueman-2.4.3.tar.gz -C $BUILD/
mkdir -p $BUILD/blueman-2.4.3--orig $BUILD/blueman-2.4.3--patched
cp $BUILD/blueman-2.4.3/configure.ac $BUILD/blueman-2.4.3--orig/
cp $BUILD/blueman-2.4.3/configure.ac $BUILD/blueman-2.4.3--patched/
sed -i 's/1.16.3/1.16.2/g' $BUILD/blueman-2.4.3--patched/configure.ac
cd $BUILD/
diff -r -U4 blueman-2.4.3--orig/configure.ac blueman-2.4.3--patched/configure.ac > $HOME/rpmbuild/SOURCES/gaztronics.patch
sed -i '16 i \
\
\# Gaztronics patch to reduce Automake number for building on EL9\
\Patch1: 	gaztronics.patch' $SPECS/blueman.spec

#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Build blueman
#
echo -e "\n\e[1;33mBuilding Blueman\e[0m\n"

rpmbuild -ba $SPECS/blueman.spec

#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Build python-caja
#
echo -e "\n\e[1;33mBuilding Python Caja\e[0m\n"

rpmbuild -ba $SPECS/python-caja.spec

#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Move SRPMS to repo
#
mv $SRPMS/b* $EPEL/SRPMS/b/
mv $SRPMS/p* $EPEL/SRPMS/p/

#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Move RPMS to repo
#
mv $RPMS/noarch/b* $EPEL/x86_64/b/

mv $RPMS/x86_64/b*-debug* $EPEL/x86_64/debug/b/
mv $RPMS/x86_64/p*-debug* $EPEL/x86_64/debug/p/

mv $RPMS/x86_64/p* $EPEL/x86_64/p/

#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Sign RPMs
#
echo -e "\n\e[0;33mSigning RPMs\e[0m\n"
$HOME/scripts/sign-rpms

# Create repo
#
echo -e "\n\e[1;35mCreating repo data\e[0m\n"
$HOME/scripts/repository

#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Clean-up
#
echo -e "\n\e[1;31mClean-up\e[0m\n"

rm -fr $DOWNLOAD/*

echo -e "\n\e[1;31mUn-set variables\e[0m\n"

unset DOWNLOAD
unset SRPMS
unset RPMS
unset BUILD
unset EPEL
unset VER
unset URI

echo -e "\n\e[1;32mDone!\e[0m\n"

#------------------------------------------------------------------------------

exit 0
