# Calculate exec=/bin/bash mergepkg(sys-apps/calculate-utils)>=3.6.6&&ini(merge-calculate.utils)<3.6.6

declare -A name_url=()
rep_name=( $( /usr/libexec/calculate/cl-variable --value update.cl_update_rep_name | tr "," " " ) )
rep_url=( $( /usr/libexec/calculate/cl-variable --value update.cl_update_rep_url | tr "," " " ) )

convert_layman_to_eselect_repo() {
	repos="${rep_name[*]}"
	laymanfn=/etc/portage/repos.conf/layman.conf
	eselectfn=/etc/portage/repos.conf/eselect-repo.conf
	# move not calculate repositories from layman to eselect repo
	if ! [[ -f $eselectfn ]] && [[ -f $laymanfn ]]
	then
		sed -rn -f - $laymanfn >> $eselectfn <<-EOF
		/^\[/ {
			/\[(${repos// /|})\]/ {
				b;
			}
			p;
			:a
			n;
			/^(sync-type|priority) =/ {
				ba;
			}
			s/layman-type/sync-type/;
			s,location = /var/lib/layman/,location = /var/db/repos/,;
			p;
			/^$/ {
				b;
			}
			ba;
		}
		EOF
		[[ -s $eselectfn ]] || rm -f $eselectfn
	fi
	rm -f $laymanfn

	# clear installed repos by layman
	installedxml=/var/lib/layman/installed.xml
	cat >$installedxml <<-EOF
	<repositories encoding="unicode" version="1.0">
	</repositories>
	EOF

	# move repos from layman to db/repos
	sed -n 's/location = //p' $eselectfn |
	while read line
	do
		repospath="$line"
		laymanpath="${line//\/var\/db\/repos/\/var\/lib\/layman}"
		if [[ -d $laymanpath ]] && ! [[ -d $repospath ]]
		then
			mv $laymanpath $repospath
		fi
	done
}

create_repo_calculate() {
	for k in ${!rep_name[*]}
	do
		name_url[${rep_name[$k]}]="${rep_url[$k]}"
	done

	reposcalculatefn=/etc/portage/repos.conf/zz-calculate.conf
	gentoofn=/etc/portage/repos.conf/gentoo.conf

	rm -f $gentoofn
	if [[ -f $reposcalculatefn ]] && grep -F "[calculate]" $reposcalculatefn &>/dev/null
	then
		return 0
	fi

	echo "Move layman to /var/db/repos"
	pushd /var/lib/layman &>/dev/null

	cat >$reposcalculatefn <<-EOF
	[DEFAULT]
	main-repo = gentoo
	
	[gentoo]
	priority = 50
	sync-uri = ${name_url[gentoo]}
	location = /usr/portage
	auto-sync = Yes
	sync-type = calculate
	EOF

	for dn in *
	do
		if [[ -d $dn ]] && [[ -n ${name_url[$dn]} ]]
		then
			cat >>$reposcalculatefn <<-EOF
			[$dn]
			priority = 50
			sync-uri = ${name_url[$dn]}
			location = /var/db/repos/$dn
			auto-sync = Yes
			sync-type = calculate
			
			EOF
			if ! [[ -d /var/db/repos/$dn ]]
			then
				mv -T /var/lib/layman/$dn /var/db/repos/$dn
				ln -sfT ../../db/repos/$dn /var/lib/layman/$dn
			fi
		fi
	done


	if [[ -d /var/calculate/custom-overlay ]]
	then
		cat >>$reposcalculatefn <<-EOF

		[custom]
		location = /var/calculate/custom-overlay
		EOF
	fi

	popd
}

change_profile() {
	if /usr/libexec/calculate/cl-variable --value main.cl_template_path | grep -q var/lib/layman
	then
		local current_profile="$(readlink -f /etc/portage/make.profile)"
		if [[ -d "${current_profile}/20" ]]
		then
			echo "Changing profile"
			ln -sfT $(realpath --relative-to /etc/portage "${current_profile}/20") /etc/portage/make.profile 
		else
			echo "Failed to upgrade profile to ${current_profile}/20"
		fi
	fi
}

[[ -d /var/db/repos ]] || mkdir -p /var/db/repos

create_repo_calculate
convert_layman_to_eselect_repo
change_profile

exit 0
