#!/usr/bin/env bash
# Copyright: 2011 Brian Harring <ferringb@gmail.com>
# Copyright: 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

shopt -s extdebug
source "${PKGCORE_EBD_PATH}"/exit-handling.lib || { echo "failed loading libs"; exit -127; }

if ! ${PKGCORE_PREFIX_SUPPORT:=false}; then
	ED=${D}
elif [[ ${ED:-unset} == "unset" ]]; then
	echo "The variable ED is missing from the environment, but is required for prefix mode; failing."
	exit -1
fi

if [[ -z $1 ]]; then
	z=${ED}usr/share/man
else
	z=${ED}$1/man
fi

[[ ! -d ${z} ]] && exit 0

if [[ -z ${PORTAGE_COMPRESS_SUFFIX} ]]; then
	case ${PORTAGE_COMPRESS} in
		gzip)  suffix="gz";;
		bzip2) suffix="bz2";;
		xz)    suffix="xz";;
		*)     echo "prepman error: please set PORTAGE_COMPRESS_SUFFIX in make.conf" 1>&2
		       exit 1;;
	esac
fi

if [[ -z ${prepallman_banner} ]]; then
	echo "man: ${PORTAGE_COMPRESS} ${PORTAGE_COMPRESS_FLAGS}"
fi

for x in $(find "${z}"/ -type d 2>/dev/null); do
	for y in $(find "${x}"/ \( -type f -or -type l \) ! -name '.keep*' -maxdepth 1 -mindepth 1 2>/dev/null); do
		if [[ -L ${y} ]]; then
			# Symlink ...
			mylink=${y}
			linkto=$(readlink "${y}")

			# Do NOT change links to directories
			if [[ -d ${z}/${linkto} ]]; then
				continue
			fi

			if [[ ${linkto##*.} != ${suffix} ]]; then
				linkto=${linkto}.${suffix}
			fi
			if [[ ${mylink##*.} != ${suffix} ]]; then
				mylink=${mylink}.${suffix}
			fi

			echo "   link fixed ${mylink##*/}"
			ln -snf "${linkto}" "${mylink}"
			if [[ ${y} != ${mylink} ]]; then
				echo "   link removed ${y##*/}"
				rm -f "${y}"
			fi
		else
			if [[ ${y##*.} != ${suffix} && ! -d ${y} ]]; then
				echo "   compressing ${y##*/}"
				"${PORTAGE_COMPRESS}" ${PORTAGE_COMPRESS_FLAGS} -f "${y}"
			fi
		fi
	done
done
