#!/bin/bash
#
# Script to build js8call from Fedora upstream
#
# v 1.0 - 30th September 2023

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

#rsync --no-motd $URI/j/js8call* $DOWNLOAD

cd $DOWNLOAD
wget $URI/j/js8call-2.2.0-14.fc36.src.rpm

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


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

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


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

rpmbuild -ba $SPECS/js8call.spec

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


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

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


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

mv $RPMS/x86_64/j* $EPEL/x86_64/j/

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


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