#!/bin/sh

# $FreeBSD$

LINK_USRBIN="yes"
: ${OSVERSION:=`/sbin/sysctl -n kern.osreldate`};
PERL_VERSION="5.16.3"
special_link_list="perl perl5"

do_remove_links()
{
	for binary in ${special_link_list} ; do
		if [ -L "/usr/bin/${binary}" ] ; then
			/bin/rm -f "/usr/bin/${binary}"
		fi
	done
	bins=`/bin/ls /usr/bin/*perl*5.* ${PKG_PREFIX}/bin/*perl*5.* 2>/dev/null`
	for binary in ${bins} ; do
		if [ -L "${binary}" ] ; then
			echo "use.perl: Removing ${binary} installed by an older perl port"
			/bin/rm -f "${binary}"
		fi
	done
}

do_create_links()
{
	for binary in ${special_link_list} ; do
		if [ -f "/usr/bin/${binary}" ] ; then
			echo "use.perl: Backing up /usr/bin/${binary} as /usr/bin/${binary}.freebsd"
			/bin/mv -f "/usr/bin/${binary}" "/usr/bin/${binary}.freebsd"
		fi
		if [ -e "/usr/bin/${binary}" ] ; then
			echo "use.perl: /usr/bin/${binary} is still there, which should not happen"
		elif [ -e "${PKG_PREFIX}/bin/perl${PERL_VERSION}" ] ; then
			/bin/ln -sf "${PKG_PREFIX}/bin/perl${PERL_VERSION}" "/usr/bin/${binary}"
		else
			echo "use.perl: ${PKG_PREFIX}/bin/perl${PERL_VERSION} is not there, a symlink won't do any good"
		fi
	done
}

if [ "$2" = "POST-INSTALL" ] ; then
	if [ ${LINK_USRBIN} = yes ] ; then
		do_remove_links
		do_create_links
	fi
elif [ "$2" = "POST-DEINSTALL" ] ; then
	[ ${LINK_USRBIN} = yes ] && do_remove_links
fi

exit 0
