# Copyright 1999-2004 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2
# $Header:$

DESCRIPTION="Open Groupware Sources all Latest by Haldir"
SRC_URI="ftp://ftp.opengroupware.org/sources/sources-all-latest.tar.bz2"
HOMEPAGE="http://www.opengroupware.org"

KEYWORDS="~x86"
SLOT="0"
LICENSE="GPL-2"
DEPEND=">=dev-db/postgresql-7.4
	>=net-www/apache-2
	>=net-nds/openldap-2.1
	>=sys-libs/zlib-1.2.1
	>=sys-devel/flex-2.5.4
	>=sys-devel/bison-1.875"
IUSE=""
INSTALLPATH="/opt/opengroupware.org"
pkg_setup() {
	groupadd skyrix
	useradd opengroupware -d ${INSTALLPATH} -g skyrix
}

src_unpack() {
	unpack ${A}
	mv ${WORKDIR}/sources-all ${P}
}
src_compile() {

	### [ ThirdParty ] ###

	cd ${S}/ThirdParty/gnustep-make
	econf	--prefix=${INSTALLPATH} \
		--without-system-root \
		--with-network-root=${INSTALLPATH} \
		--with-local-root=${INSTALLPATH} \
		--with-user-root=${INSTALLPATH} \
		--with-library-combo=gnu-fd-nil || die "econf failed"
	sed -i "s#special_prefix = #special_prefix = ${S}#" GNUmakefile
	make -s install  || die "make gnustep-make failed"
	cp  ${S}${INSTALLPATH}/Makefiles/GNUstep.sh ${S}${INSTALLPATH}/Makefiles/GNUstep.sh.backup
	sed -i "s#${INSTALLPATH}#${S}${INSTALLPATH}#" ${S}${INSTALLPATH}/Makefiles/GNUstep.sh
	. ${S}${INSTALLPATH}/Makefiles/GNUstep.sh
	cd ${S}/ThirdParty/gnustep-objc
	make -s install  || die "make gnustep-objc failed"
	cd ${S}/ThirdParty/libFoundation
	econf	--with-gnustep
	make -s install  || die "make libFoundation failed"
	cd ${S}/ThirdParty/libxml2
	. ${S}/ThirdParty/libxml2/cfg-gnustep.sh
	make -s install  || die "make libxml2 failed"
	cd ${S}/ThirdParty/libical
	. ${S}/ThirdParty/libical/cfg-gnustep.sh
	make -s install  || die "make libical failed"
	cd ${S}/ThirdParty/js-1.5
	make -s install  || die "make js-1.5 failed"

	### [ SOPE ] ###

	cd ${S}/SOPE/skyrix-xml
	make -s install  || die "make skyrix-xml failed"
	cd ${S}/SOPE/skyrix-core
	make -s install  || die "make skyrix-core failed"
	cd ${S}/SOPE/skyrix-core/samples
	make -s install  || die "make skyrix-core/samples failed"
	cd ${S}/SOPE/skyrix-sope
	make -s install  || die "make skyrix-sope failed"

	### [ Building the GNUstep Database Library ] ###

	cd ${S}/ThirdParty/gnustep-db/GDLAccess
	make -s install  || die "make GDLAccess failed"
	cd ${S}/ThirdParty/gnustep-db/PostgreSQL72
	make -s install  || die "make PostgreSQL72 failed"

	### [ OpenGroupware.org ] ###

	cd ${S}/Logic
	make -s install  || die "make Logic failed"
	cd ${S}/Database/OGoModel
	make -s install  || die "make OGoModel failed"
	cd ${S}/DocumentAPI
	make -s install  || die "make DocumentAPI failed"
	cd ${S}/WebUI
	make -s install  || die "make WebUI failed"
	cd ${S}/WebUI/Resources
	make -s install  || die "make WebUI/Resources failed"
	cd ${S}/Themes/WebServerResources
	make -s install  || die "make Themes/WebServerResources failed"

	### [ Apache Connector ] ###
	cd ${S}/SOPE/mod_ngobjweb
	make apxs=/usr/sbin/apxs2 HTTPD=/usr/sbin/apache2  || die "make Apache Connector failed"
	mkdir -p ${S}${INSTALLPATH}/WebServer
	cp ${S}/SOPE/mod_ngobjweb/ngobjweb_2.0.*.so ${S}${INSTALLPATH}/WebServer

	### [ ZideStore and iCalSaxDriver ] ###
	cd ${S}/ZideStore
	make -s install  || die "make ZideStore failed"
	cd ${S}/SOPE/skyrix-xml/iCalSaxDriver
	make -s install  || die "make iCalSaxDriver failed"
}


src_install() {
	cp  ${S}${INSTALLPATH}/Makefiles/GNUstep.sh.backup ${S}${INSTALLPATH}/Makefiles/GNUstep.sh
	mkdir -p ${S}${INSTALLPATH}/documents
	mkdir -p ${S}${INSTALLPATH}/DatabaseSetup
	cp ${S}/Database/PostgreSQL/pg-build-schema.psql ${S}${INSTALLPATH}/DatabaseSetup/
	cp ${S}/Database/PostgreSQL/pg-update-schema.psql ${S}${INSTALLPATH}/DatabaseSetup/
	cd ${S}${INSTALLPATH}/WOApps/OpenGroupware.woa
	rm -rf WebServerResources
	ln -sf ../../WebServerResources WebServerResources
	echo ". ${INSTALLPATH}/Makefiles/GNUstep.sh" >> ${S}${INSTALLPATH}/.profile
	mkdir -p  ${D}/opt
	cp -a ${S}${INSTALLPATH} ${D}/opt/
	mkdir -p ${D}/var/log/opengroupware
	mkdir -p ${D}/etc/env.d
	mkdir -p ${D}/etc/init.d
	mkdir -p ${D}/etc/conf.d
	mkdir -p ${D}/etc/apache2/conf/modules.d
	cp ${FILESDIR}/50opengroupware ${D}/etc/env.d/
	cp ${FILESDIR}/opengroupware.startscript ${D}/etc/init.d/opengroupware
	cp ${FILESDIR}/opengroupware.conf.d ${D}/etc/conf.d/opengroupware
	cp ${FILESDIR}/50_opengroupware.conf ${D}/etc/apache2/conf/modules.d/
	mkdir -p ${D}/var/log/opengroupware
	chown -R opengroupware:skyrix ${D}${INSTALLPATH}
}



pkg_postinst() {
	einfo "Execute the following command"
	einfo "ebuild  /var/db/pkg/net-misc/${PF}/${PF}.ebuild config"
	einfo "to setup the initial environment."
	einfo ""
	einfo "Make sure that you enable PostgreSQL TCP/IP Sockets and Port"
	einfo "You can ignore the database 'ogo' already exists error if you"
	einfo "have OpenGroupware already installed, it's just for verification"
	einfo "The update SQL script is supplied in the DataseSetup subdir"
	einfo "Add '-D OGo' in the APACHE2_OPTS"
}


pkg_config() {
	env-update
	sudo -u postgres createdb ogo
	retval=$?
	if [ $retval -eq 0 ]; then
	sudo -u postgres createuser -A -D ogo
	sudo -u postgres psql ogo ogo -f ${INSTALLPATH}/DatabaseSetup/pg-build-schema.psql
	fi
	source ${INSTALLPATH}/Makefiles/GNUstep.sh
	Defaults write NSGlobalDomain LSConnectionDictionary '{databaseName = ogo; hostName = localhost; password =""; port = 5432; userName = ogo}'
	Defaults write NSGlobalDomain LSAdaptor PostgreSQL72
	Defaults write NSGlobalDomain NGBundlePath ${INSTALLPATH}/Library/OpenGroupware.org
	Defaults write NSGlobalDomain LSAttachmentPath ${INSTALLPATH}/documents
	Defaults write NSGlobalDomain LSModelName OpenGroupware.org_PostgreSQL
	chown -R opengroupware:skyrix ${INSTALLPATH}
	source /etc/profile
}