#!/bin/sh
#
#
# $MidnightBSD: mports/lang/perl5.8/files/pkg-glue,v 1.4 2008/06/09 20:20:10 ctriv Exp $


PERL_VERSION='%%PERL_VERSION%%'
MAKE_CONF='%%MAKE_CONF%%'

banner=`date +"%F %T"`
banner="# added by perl $banner"

# We still cleanup make.conf in case the user had something old in there.
if [ "$2" = "POST-INSTALL" ] ; then
	need_remove_links=yes
	need_create_links=yes
	need_cleanup_make_conf=yes
	need_cleanup_manpath=yes
	need_spam_manpath=yes
	need_post_install=yes
elif [ "$2" = "POST-DEINSTALL" ] ; then
	need_remove_links=yes
	need_cleanup_manpath=yes
else
	exit 0;
fi

links="perl perl$PERL_VERSION"

do_remove_links()
{
	echo "Removing stale symlinks from /usr/bin..."
	for binary in $links
	do
		if [ -L "/usr/bin/$binary" ] ; then
			echo "    Removing /usr/bin/$binary"
			/bin/rm -f "/usr/bin/$binary"
		else
			echo "    Skipping /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 "    Removing $binary installed by an older perl port"
			/bin/rm -f "$binary"
		fi
	done
	echo "Done."
}


do_create_links()
{
	echo "Creating symlnks to /usr/bin"
	for bin in $links; do
		echo "	Linking $PKG_PREFIX/bin/$bin to /usr/bin/$bin"
		/bin/ln -sf $PKG_PREFIX/bin/$bin /usr/bin/$bin
	done
	echo "Done."
}

do_post_install()
{
	INCLUDEDIR=/usr/include
	install -d ${PKG_PREFIX}/lib/perl5/site_perl/$PERL_VERSION/%%PERL_ARCH%%/auto
	install -d ${PKG_PREFIX}/lib/perl5/site_perl/$PERL_VERSION/auto
	install -d ${PKG_PREFIX}/lib/perl5/$PERL_VERSION/man/man3
	cd ${INCLUDEDIR} && ${PKG_PREFIX}/bin/h2ph *.h machine/*.h sys/*.h >/dev/null
}

do_cleanup_make_conf()
{
	echo -n "Cleaning up ${MAKE_CONF}..."
	if [ -f ${MAKE_CONF} ] ; then
		/usr/bin/awk 's=0;
			/^#.*by perl/ { s=1; mode=1 }
			/^#/ { s=1; if (mode != 1) { mode=0 } }
			/.*PERL.*=/ { s=1; if (mode == 1) { mode=2 } }
			/^$/ { s=1; if (mode != 2) { mode = 0 } }
			{ if (s != 1) { mode = 0 } if (mode == 0) print }' ${MAKE_CONF} >${MAKE_CONF}.new
		/bin/mv ${MAKE_CONF} ${MAKE_CONF}.bak
		/bin/mv ${MAKE_CONF}.new ${MAKE_CONF}
	fi
	echo " Done."
}

do_cleanup_manpath()
{
	echo -n "Cleaning up /etc/manpath.config..."
	if [ -f /etc/manpath.config ] ; then
		/usr/bin/awk 's=0;
			/^#.*by perl/ { s=1; mode=1 }
			/^#/ { s=1; if (mode != 1) { mode=0 } }
			/^OPTIONAL_MANPATH.*perl5/ { s=1; if (mode == 1) { mode=2 } }
			/^$/ { s=1; if (mode != 2) { mode = 0 } }
			{ if (s != 1) { mode = 0 } if (mode == 0) print }' /etc/manpath.config >/etc/manpath.config.new
		/bin/mv /etc/manpath.config /etc/manpath.config.bak
		/bin/mv /etc/manpath.config.new /etc/manpath.config
	fi
	echo " Done."
}


do_spam_manpath()
{
	echo -n "Spamming /etc/manpath.config..."
	echo "$banner" >>/etc/manpath.config
	echo "OPTIONAL_MANPATH	${PKG_PREFIX}/lib/perl5/$PERL_VERSION/man" >>/etc/manpath.config
	echo "OPTIONAL_MANPATH	${PKG_PREFIX}/lib/perl5/$PERL_VERSION/perl/man" >>/etc/manpath.config
	echo " Done."
}

[ $need_remove_links ] && do_remove_links
[ $need_create_links ] && do_create_links
[ $need_post_install ] && do_post_install
[ $need_cleanup_make_conf ] && do_cleanup_make_conf
[ $need_cleanup_manpath ] && do_cleanup_manpath
[ $need_spam_manpath ] && do_spam_manpath

exit 0

