#!/bin/bash
#
# Script to build xlog from Fedora upstream
#
# v 1.1 - 27th September 2023
#
# 1.1 - Rebuild against Fedora 36

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

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


#------------------------------------------------------------------------------
# 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=36
URI=rsync://archives.fedoraproject.org/fedora-archive/fedora/linux/releases/$VER/Everything/source/tree/Packages

cd $DOWNLOAD
rsync --no-motd $URI/x/xlog* $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/*

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


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

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

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


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

rpmbuild -ba $SPECS/xlog.spec

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


#------------------------------------------------------------------------------
# Move SRPMS to repo
#
mv $SRPMS/x* $EPEL/SRPMS/x/

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


#------------------------------------------------------------------------------
# Move RPMS to repo
#
mv $RPMS/x86_64/x*-debug* $EPEL/x86_64/debug/x/

mv $RPMS/x86_64/x* $EPEL/x86_64/x/

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


#------------------------------------------------------------------------------
# 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 EPEL
unset VER
unset URI

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

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

exit 0
