#!/bin/bash
#
# Script to build xastir from Fedora upstream
#
# v 1.2 - 27th September 2023
#
# 1.1 - added perl-GPS
# 1.2 - 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/g/gpsman* $DOWNLOAD
rsync --no-motd $URI/l/libax25* $DOWNLOAD
rsync --no-motd $URI/t/tkimg* $DOWNLOAD
rsync --no-motd $URI/p/perl-GPS* $DOWNLOAD
rsync --no-motd $URI/x/xastir* $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 tkimg
#
echo -e "\n\e[1;33mBuilding tkimg\e[0m\n"

rpmbuild -ba $SPECS/tkimg.spec

# Install dependencies
#
sudo dnf -y install $RPMS/x86_64/tkimg-1.* $RPMS/x86_64/tkimg-devel-*

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


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

rpmbuild -ba $SPECS/gpsman.spec

# Install dependencies
#
sudo dnf -y install $RPMS/noarch/gpsman*

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


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

rpmbuild -ba $SPECS/libax25.spec

# Install dependencies
#
sudo dnf -y install $RPMS/x86_64/libax25-1.* $RPMS/x86_64/libax25-devel-*

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

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

rpmbuild -ba $SPECS/perl-GPS.spec

# Install dependencies
#
sudo dnf -y install $RPMS/x86_64/libax25-1.* $RPMS/x86_64/libax25-devel-*

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


#------------------------------------------------------------------------------
# Build xastir
#

# Remove ancient reference to Fedora 24 that stops build in EL8
# (There is probably a posh-way of doing this...)
#
sed -i 's/%if 0%{?fedora} >= 24//g' $SPECS/xastir.spec
sed -i 's/%else//g' $SPECS/xastir.spec
sed -i 's/%endif//g' $SPECS/xastir.spec
sed -i 's/BuildRequires: lesstif-devel//g' $SPECS/xastir.spec

echo -e "\n\e[1;33mBuilding xastir\e[0m\n"

rpmbuild -ba $SPECS/xastir.spec

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


#------------------------------------------------------------------------------
# Move SRPMS to repo
#
mv $SRPMS/g* $EPEL/SRPMS/g/
mv $SRPMS/l* $EPEL/SRPMS/l/
mv $SRPMS/p* $EPEL/SRPMS/p/
mv $SRPMS/t* $EPEL/SRPMS/t/
mv $SRPMS/x* $EPEL/SRPMS/x/

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


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

mv $RPMS/x86_64/l*-debug* $EPEL/x86_64/debug/l/
mv $RPMS/x86_64/t*-debug* $EPEL/x86_64/debug/t/
mv $RPMS/x86_64/x*-debug* $EPEL/x86_64/debug/x/

mv $RPMS/x86_64/l* $EPEL/x86_64/l/
mv $RPMS/x86_64/t* $EPEL/x86_64/t/
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"

sudo dnf -y erase gpsman motif motif-devel libax25 libax25-devel tkimg

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

