#!/bin/bash

# This script requires fedora-release-rawhide and perl-XML-Rules

# cfo-dependencies [--arch i386] [--release nn] [--branched] [--rawhide]
# work out entire package set needed to build and install packages in cfo-repo

# Where is the city-fan.org repo
CFO_REPO=/home/paul/cfo-repo
BRANCHED_RELEASE_VER=$(ls -ld $CFO_REPO/yum-repo/fc* | sed -e 's|.*/fc||' | sort -n | tail -n 1)
RAWHIDE_RELEASE_VER=$(expr $BRANCHED_RELEASE_VER + 1)

# Parse options
OPTS=$(getopt --options a:r: --longoptions arch:,release:,branched,rawhide --name "cfo-dependencies" -- "$@")
if [ $? != 0 ]; then
	echo 'cfo-dependencies: usage: cfo-dependencies [--arch i386] [--release nn] [--branched] [--rawhide]' 1>&2
	exit 1
fi
eval set -- "$OPTS"
TARGET_RELEASE=default
TARGET_ARCH=default
while :
do
	case "$1" in
	-a|--arch)	TARGET_ARCH="$2"
			case "$TARGET_ARCH" in
			i386|x86_64)	;;
			*)		echo 'cfo-dependencies: --arch option must be i386 or x86_64' 1>&2
					exit 1
					;;
			esac
			shift 2
			;;
	-r|--release)	TARGET_RELEASE="$2"
			case "$TARGET_RELEASE" in
			[3-9][0-9])	;;
			*)		echo "cfo-dependencies: --release option not recognized: $TARGET_RELEASE"
					exit 1
					;;
			esac
			shift 2
			;;
	--branched)	TARGET_RELEASE=branched
			shift
			;;
	--rawhide)	TARGET_RELEASE=rawhide
			shift
			;;
	--)		shift
			break
			;;
	*)		echo 'Internal error!'
			exit 1
			;;
	esac
done
[ "$TARGET_RELEASE" = "default" ] && TARGET_RELEASE=rawhide
[ "$TARGET_ARCH"    = "default" ] && TARGET_ARCH=$(arch)
EXTRA_REPO_SPEC=""

echo "Processing dependency tree for Fedora $TARGET_RELEASE ($TARGET_ARCH)"

case "$TARGET_RELEASE" in
rawhide)	RELEASE_VER=$RAWHIDE_RELEASE_VER
		EXTRA_REPO_SPEC="--repofrompath koji,https://kojipkgs.fedoraproject.org/repos/f${RELEASE_VER}-build/latest/$TARGET_ARCH/"
		LOCAL_SOURCE_REPO=city-fan.org-rawhide-source
		LOCAL_BINARY_REPO=city-fan.org-rawhide
		BASE_BINARY_REPO=koji
		;;
branched)	RELEASE_VER=$BRANCHED_RELEASE_VER
		EXTRA_REPO_SPEC="--repofrompath koji,https://kojipkgs.fedoraproject.org/repos/f${RELEASE_VER}-build/latest/$TARGET_ARCH/"
		LOCAL_SOURCE_REPO=city-fan.org-source
		LOCAL_BINARY_REPO=city-fan.org
		BASE_BINARY_REPO=koji
		;;
*)		RELEASE_VER=$TARGET_RELEASE
		EXTRA_REPO_SPEC="--repofrompath koji,https://kojipkgs.fedoraproject.org/repos/f${RELEASE_VER}-build/latest/$TARGET_ARCH/"
		LOCAL_SOURCE_REPO=city-fan.org-source
		LOCAL_BINARY_REPO=city-fan.org
		# We need koji for i386 so might as well use it, rather than fedora, for every arch
		BASE_BINARY_REPO=koji
		;;
esac
case $TARGET_ARCH in
i386)		BINARY_ARCHLIST="i386,i586,i686"
		FORCE_ARCH="i686"
		;;
x86_64)		BINARY_ARCHLIST="x86_64"
		FORCE_ARCH="x86_64"
		;;
esac

# Get list of local RPMs and SRPMs; we are after all dependencies of both,
# so that we can build and install them
echo "================================================================="
echo "Get list of RPMs and SRPMs of interest"
echo "================================================================="
LOCAL_PACKAGE_LIST=$(
	dnf repoquery \
		--quiet \
		--releasever=$RELEASE_VER \
		--disablerepo=\* \
		--enablerepo=$LOCAL_SOURCE_REPO \
		--enablerepo=$LOCAL_BINARY_REPO \
		--arch=${BINARY_ARCHLIST},noarch,src \
		--forcearch=$TARGET_ARCH \
		--all --qf '%{name}.%{arch}' |
		sort -u
)

echo "================================================================="
echo "Get list of packages in build group"
echo "================================================================="
# This might be more than the real buildroot but we want all of these anyway
BUILD_GROUP="
	bash
	bzip2
	coreutils
	cpio
	diffutils
	dnf
	dnf-plugins-core
	dnf5
	dnf5-plugins
	fedora-release-common
	findutils
	gawk
	glibc-minimal-langpack
	grep
	gzip
	info
	make
	patch
	redhat-rpm-config
	rpm-build
	sed
	shadow-utils
	tar
	unzip
	util-linux
	which
	xz
"

echo "================================================================="
echo "Get list of dependencies for all packages"
echo "================================================================="
(
	for pkg in $BUILD_GROUP
	do
		echo "	$pkg"
	done
	dnf repoquery \
		--quiet \
		--releasever=$RELEASE_VER \
		$EXTRA_REPO_SPEC \
		--disablerepo=\* \
		--enablerepo=$LOCAL_SOURCE_REPO \
		--enablerepo=$LOCAL_BINARY_REPO \
		--enablerepo=$BASE_BINARY_REPO \
		--arch=${BINARY_ARCHLIST},noarch,src \
		--forcearch=$FORCE_ARCH \
		--qf '	%{name}' \
		--requires --resolve --recursive $BUILD_GROUP $LOCAL_PACKAGE_LIST
) |
sort -u > "package-list-$TARGET_RELEASE-$TARGET_ARCH"
echo "Output written to package-list-$TARGET_RELEASE-$TARGET_ARCH"