DESCRIPTION="Soikko - Finnish spell checking and hyphenation for OpenOffice"
HOMEPAGE="http://www.lemi.fi/oo2-soikko"
SRC_URI="
http://www.lemi.fi/oo2-soikko/${P}.tar.gz
http://www.lemi.fi/oo2-soikko/${P}gcc3.3.tar.gz
"
#http://www.lemi.fi/oo2-soikko/${P]-src.tar.gz for sources

LICENSE="SISSL-1.1 soikko GPL-2.0 LGPL-2.1"
SLOT="0"
KEYWORDS="-x86"

DEPEND="|| (>=app-office/openoffice-bin-2.0 >=app-office/openoffice-2.0)"

src_unpack() {
	einfo ""
	einfo "Soikko Binary Code License"
	einfo ""
	einfo "Copyright (C) 2002, Pasi Ryhänen <pry@iki.fi>"
	einfo ""
	einfo "Redistribution and use in binary form are permitted provided that the"
	einfo "following conditions are met:"
	einfo ""
	einfo "1. Redistributions in binary form must reproduce the above copyright"
	einfo "   notice, this list of conditions and the following disclaimer in the"
	einfo "   documentation and/or other materials provided with the distribution."
	einfo ""
	einfo "THIS SOFTWARE IS PROVIDED \"AS IS\" AND WITHOUT ANY EXPRESS OR IMPLIED"
	einfo "WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF"
	einfo "MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE."
	einfo ""

	unpack ${A}
}

src_install() {
	dodoc ASENNUS.txt
	insinto /usr/lib/openoffice/share/
	doins install-oo2-soikko
#	insinto ${OODIR}/program
#	doins ${S}/regcomp
#	fperms 755 ${OODIR}/program/regcomp
#	doins ${S}/libspellfi645li.so
#	fperms 755 ${OODIR}/program/libspellfi645li.so
#	doins ${S}/libhyphenfi645li.so
#	fperms 755 ${OODIR}/program/libhyphenfi645li.so
#	doins ${S}/libsoikko.so
#	fperms 755 ${OODIR}/program/libsoikko.so

#	insinto ${OODIR}/share/dict
#	doins ${S}/soikko-sp.fi_FI.dic
#	doins ${S}/soikko-hy.fi_FI.dic
}


pkg_postinst() {
	# From install-oo2-soikko
	if [ $(pidof soffice.bin) ]; then
		die "soikko can't be installed while open office is running"
	fi
	
	# packages and binaries
	COMPONENT="${S}/oo2-soikko-Linux_x86-${PV}.uno.pkg"
	COMPONENT33="${WORKDIR}/${P}gcc3.3-${PV}/oo2-soikko-Linux_x86-${PV}gcc3.3.uno.pkg"
	if has_version '>=app-office/openoffice-bin'; then
		UNOPKG="/usr/lib/openoffice/program/unopkg"
	else
		UNOPKG="/usr/lib/openoffice/program/unopkg" #verify!
	fi
	
	# unregister()
	UNOPKG_LIST="$(${UNOPKG} list --shared 2> /dev/null)"
	if [[ $? != 0 ]] ; then
		die "Couldn't list existing packages"
	fi
	PKGS_TO_UNREGISTER=$(echo "${UNOPKG_LIST}" | grep "^Name: oo2-soikko-Linux_x86-" | sed -e "s/Name: //")
	for PKG in $PKGS_TO_UNREGISTER
	do
		if $UNOPKG remove --shared ${PKG} &>/dev/null
		then
			einfo "Old soikko package unregistered succesfully"
		else
			die "Couldn't unregister existing soikko packages"
		fi
	done

	# select right binary interface
	if grep -U -q CXXABI_1.3 "$UNOPKG".bin
	then
		# use normal binary
		# register()
		if $UNOPKG add --shared ${COMPONENT} &>/dev/null;
		then
			einfo "Soikko registered succesfully"
		else
			die "Couldn't register soikko component"
		fi
		
	else
		# use gcc3.3
		# register()
		if $UNOPKG add --shared ${MODULEDIR}/${COMPONENT33} &>/dev/null;
		then
			einfo "Soikko for gcc-3.3 ABI registered successfully"
		else
			die "Couldn't register soikko for gcc-3.3 ABI"
		fi	
	fi
	
#	for i in libspellfi645li.so libhyphenfi645li.so; do
#		LD_LIBRARY_PATH=${OODIR}/program \
#		 ${OODIR}/program/regcomp -register \
#		 -r ${OODIR}/program/services.rdb -c ${i}
#	done
}

pkg_prerm() {
	if has_version '>=app-office/openoffice-bin'; then
		UNOPKG="/usr/lib/openoffice/program/unopkg"
	else
		UNOPKG="/usr/lib/openoffice/program/unopkg" #verify!
	fi
	#unregister()
	UNOPKG_LIST="$(${UNOPKG} list --shared 2> /dev/null)"
	if [[ $? != 0 ]] ; then
		die "Couldn't list existing packages"
	fi
	PKGS_TO_UNREGISTER=$(echo "${UNOPKG_LIST}" | grep "^Name: oo2-soikko-Linux_x86-" | sed -e "s/Name: //")
	for PKG in $PKGS_TO_UNREGISTER
	do
		if $UNOPKG remove --shared ${PKG} &>/dev/null
		then
			einfo "Soikko packages unregistered succesfully"
		else
			die "Couldn't uninstall existing soikko packages"
		fi
	done

	
#	for i in libspellfi645li.so libhyphenfi645li.so; do
#		LD_LIBRARY_PATH=${OODIR}/program \
#		 ${OODIR}/program/regcomp -revoke \
#		 -r ${OODIR}/program/services.rdb -c ${i}
#	done
}