# Calculate run=/bin/bash os_arch_machine==x86_64 exists(/lib32)==l ini(overlay-calculate.revision)>=20191116.3

get_packages() {
	awk -F: '{print $1}' | sort -u | sed 's,/CONTENTS,,'
}

check_lib32() {
	cd /var/db/pkg
	grep -rn " /lib32/" */*/CONTENTS 
}

check_usrlib32() {
	cd /var/db/pkg
	grep -rn " /usr/lib32/" */*/CONTENTS 
}

check_llvm32() {
	cd /var/db/pkg
	grep -rnP " /usr/lib/llvm/.*/lib32/" */*/CONTENTS
}

downgrade_for_rebuild() {
	grep -v _rc73 | while read pf;
	do
		pv_new="$(sed 's,-r[0-9]\+,,' <<<$pf)_rc73"
		mv /var/db/pkg/$pf /var/db/pkg/$pv_new	
	done
}

PACKAGES="$((check_lib32;check_usrlib32;check_llvm32) | get_packages)"

if [[ -n $PACKAGES ]]
then
	downgrade_for_rebuild <<<$PACKAGES
else
	rm -f /lib32 /usr/lib32
fi
