#!/bin/bash
unset LANG
unset ${!LC_*}
#et -x
#
do_download=false
substitute_for_prjconf=
until test "$#" -eq 0
do
	case "$1" in
		-d) do_download=true ;;
		-o)
		substitute_for_prjconf=$2
		if test -f "${substitute_for_prjconf}"
		then
			echo "substitute file for prjconf exists: ${substitute_for_prjconf}"
			exit 1
		fi
		shift
		;;
		*) ;;
	esac
	shift
done

td=`mktemp -d --tmpdir=/dev/shm`
test -z "${td}" && exit 1
_x() {
	rm -rf "${td}"
}
trap _x EXIT
#
deps="${td}/deps.txt"
plain_deps="${td}/plain_deps.txt"
srcrpm_dir="${td}/srcrpm"
dir_prov_ocaml="${td}/prov"
dir_req_ocaml="${td}/req"
dep_list="${td}/dep_list"
t="${td}/t"
mkdir "${srcrpm_dir}" "${dir_prov_ocaml}" "${dir_req_ocaml}"
#
obs="/usr/bin/osc --config=$HOME/.osc/oscrc -A https://api.opensuse.org"
projects="
devel:languages:ocaml
Virtualization
"
projects="devel:languages:ocaml"
repo="openSUSE_Factory"
arch="x86_64"
binary_dir="binary_for_deps"


for prj in $projects
do
	if pushd "${prj}" > /dev/null
	then
		for pkg in `$obs ls $prj`
		do
			if pushd $pkg > /dev/null
			then
				if ${do_download}
				then
					rm -rf "${binary_dir}"
					$obs getbinaries --destdir="${binary_dir}" "${prj}" "${pkg}" "${repo}" "${arch}"
				fi
				popd > /dev/null
			fi
		done
		#
		for rpm in */${binary_dir}/*.rpm
		do
			name=`rpm -qp --nosignature --qf '%{NAME}' "${rpm}"`
			rpm -qp --nosignature --qf '%{SOURCERPM}' "${rpm}" | sed 's@-[^-]\+-[^-]\+\.[^\.]\+\.rpm$@@' > "${srcrpm_dir}/${name}" &
			rpm -qp --nosignature --provides "${rpm}" | sed -n '/^ocaml\(find\)\?(/{s@[()[:blank:]]\+@§@g;p}' > "${dir_prov_ocaml}/${name}" &
			rpm -qp --nosignature --requires "${rpm}" | sed -n '/^ocaml\(find\)\?(/{s@[()[:blank:]]\+@§@g;p}' > "${dir_req_ocaml}/${name}" &
			wait
		done
		#
		for rpm in ${dir_req_ocaml}/*
		do
			if test -s "${rpm}"
			then
				req_rpm="${rpm##*/}"
				for dep in `cat "${rpm}"`
				do
					provs=(` grep -rl "${dep}" ${dir_prov_ocaml} `)
					if test ${#provs[@]} -eq 0
					then
						echo "Nothing provides ${dep} required by ${req_rpm}" >&2
						continue
					fi
					i=0
					while test $i -lt ${#provs[@]}
					do
						provs[$i]=${provs[$i]##*/}
						: $(( i++ ))
					done
					if test ${#provs[@]} -gt 1
					then
						echo "Multiple providers of ${dep} required by ${req_rpm} (${#provs[@]}): ${provs[@]}" >&2
					fi
					# ocamlfuse > ocaml-runtime
					wrong_lib=false
					i=0
					while test $i -lt ${#provs[@]}
					do
						if test "${provs[$i]}" = "ocaml-runtime"
						then
							wrong_lib=true
							break
						fi
						: $(( i++ ))
					done
					if test "${wrong_lib}" = "true"
					then
						i=0
						while test $i -lt ${#provs[@]}
						do
							if test "${provs[$i]}" = "ocamlfuse"
							then
								provs[$i]="ocaml-runtime"
							fi
							: $(( i++ ))
						done
					fi
					# ocaml-runtime > ocaml-compiler-libs
					wrong_lib=false
					i=0
					while test $i -lt ${#provs[@]}
					do
						if test "${provs[$i]}" = "ocaml-compiler-libs"
						then
							wrong_lib=true
							break
						fi
						: $(( i++ ))
					done
					if test "${wrong_lib}" = "true"
					then
						i=0
						while test $i -lt ${#provs[@]}
						do
							if test "${provs[$i]}" = "ocaml-runtime"
							then
								provs[$i]="ocaml-compiler-libs"
							fi
							: $(( i++ ))
						done
					fi
					#
					for prov in ${provs[@]}
					do
						prov_rpm="${prov##*/}"
						if test "${req_rpm}" != "${prov_rpm}"
						then
							echo "${prov_rpm}"
						fi
					done
				done > "${t}"
				echo "`cat ${srcrpm_dir}/${req_rpm}` ${req_rpm} `sort -u < \"${t}\"|xargs`"
			fi
		done | sort -u > "${dep_list}"
		#
		#cat "${dep_list}"
		while read src_rpm req_rpm prov_rpm
		do
			echo "${src_rpm} ${req_rpm} ${prov_rpm}"
			case "${src_rpm}" in
				ocamlfuse) spec_dir=ocaml-fuse ;;
				*) spec_dir="${src_rpm}" ;;
			esac
			spec="${spec_dir}/${src_rpm}.spec"
			auto_start="# ocaml autodep start for pkg:"
			auto_end="# ocaml autodep end for pkg:"
			auto_hint="# hardcoded rpm dependency for pre 12.1 to compensate for lack of ocaml() provides/requires"
			if test -f "${spec}"
			then
				sed -i "/${auto_start} ${req_rpm}/,/${auto_end} ${req_rpm}/d" "${spec}"
				if test "${src_rpm}" = "${req_rpm}"
				then
					: main
					sed -i "
					/^BuildRoot:/{ a \\
${auto_start} ${req_rpm}\\
${auto_hint}\\
%if 0%{?suse_version} < 1210\\
Requires:       ${prov_rpm}\\
%endif\\
${auto_end} ${req_rpm}
					}
					" "${spec}"
				else
					: sub package
					sed -i "
					/^%package ${req_rpm#${src_rpm}-}/{ a \\
${auto_start} ${req_rpm}\\
${auto_hint}\\
%if 0%{?suse_version} < 1210\\
Requires:       ${prov_rpm}\\
%endif\\
${auto_end} ${req_rpm}
					}
					" "${spec}"
				fi
			else
				echo "Missing rpm specfile: ${spec}" >&2
			fi
		done < "${dep_list}"
		#
		if test -n "${substitute_for_prjconf}"
		then
			for prov in `grep -rl ^ocamlfind "${dir_prov_ocaml}"`
			do
				prov_rpm=${prov##*/}
				sed -n "
				/^ocamlfind/ {
				s@^\([^§]\+\)§\([^§]\+\)§@Substitute: \1(\2) ${prov_rpm}=@p
				}
				" ${prov}
			done | sort >> "${substitute_for_prjconf}"
		fi
		#
		popd > /dev/null
	fi
done
