#!/bin/bash
#
# Script to build hplip from Fedora upstream
#
# 1.0 - Rebuild against Fedora 41 for EL9

#------------------------------------------------------------------------------
# A path, a path!
#
DOWNLOAD=$HOME/Source/
SPECS=$HOME/rpmbuild/SPECS
SRPMS=$HOME/rpmbuild/SRPMS
RPMS=$HOME/rpmbuild/RPMS
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

cd $DOWNLOAD
rsync --no-motd $URI/h/hplip* $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 hplip
#
echo -e "\n\e[1;33mBuilding hplip\e[0m\n"

rpmbuild -ba $SPECS/hplip.spec

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


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

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


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

mv $RPMS/x86_64/h* $EPEL/x86_64/h/
mv $RPMS/x86_64/l* $EPEL/x86_64/l/

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


#------------------------------------------------------------------------------
# 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
