# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

inherit subversion webapp eutils

ESVN_REPO_URI="https://svn.roundcube.net/trunk/roundcubemail"
ESVN_PROJECT="roundcubemail"

DESCRIPTION="A browser-based multilingual IMAP client using AJAX - (sources from SVN)"
HOMEPAGE="http://www.roundcube.net"
SRC_URI=""

LICENSE="GPL-2"
KEYWORDS="~x86"

S=${WORKDIR}/${ESVN_PROJECT}

IUSE="mysql postgres sqlite"

RDEPEND="mysql? (>=dev-db/mysql-4)
        sqlite? (dev-db/sqlite)
        postgres? (dev-db/postgresql)
        !mysql? (!sqlite(!postgresql(dev-db/mysql)))
        virtual/httpd-php
        dev-php/PEAR-DB"

pkg_setup() {
        eerror "This is a LIVE SVN ebuild."
        eerror "That means there are NO promises it will work."
        eerror "If it fails to build, FIX THE CODE YOURSELF"
        eerror "before reporting any issues."
        if use mysql ; then
                if ! built_with_use virtual/httpd-php mysql ; then
                        eerror "PHP is missing MySQL support."
                        eerror "Please add 'mysql' to your USE flags, and re-emerge PHP."
                        die "PHP needs mysql support"
                fi
        fi
        webapp_pkg_setup
}

src_unpack() {
        subversion_src_unpack
        mv ${S}/config/db.inc.php.dist ${S}/config/db.inc.php
        mv ${S}/config/main.inc.php.dist ${S}/config/main.inc.php
        cd "${S}"
        find . -type d -name "CVS" -exec rm -rf "{}" ";" 1>/dev/null 2>&1
}

src_install () {
        webapp_src_preinst

        cd "${S}"

        local docs="CHANGELOG README UPGRADING"

        # handle documentation files
        #
        # NOTE that doc files go into /usr/share/doc as normal; they do NOT
        # get installed per vhost!
        dodoc ${docs}
        for foo in ${docs} INSTALL; do
                rm -f ${foo}
        done

        # copy the app's main files
        #
        einfo "Installing main files"
        cp -R . ${D}/${MY_HTDOCSDIR}
        rm -rf ${D}/${MY_HTDOCSDIR}/SQL

        # Directories needed to be owned by web server
        #
        webapp_serverowned ${MY_HTDOCSDIR}/logs
        webapp_serverowned ${MY_HTDOCSDIR}/temp

        # add the post-installation instructions
        #
        webapp_postinst_txt en ${FILESDIR}/postinstall-svn-en.txt
        
        # install the SQL scripts available to us
        # NOTE: We support all the differend SQL backends
        #
        if use mysql ; then
                webapp_sqlscript mysql SQL/mysql.initial.sql
                cp SQL/mysql.update.sql ${T}/mysql.update-0.1-20051007-to-${PV}.sql
                webapp_sqlscript mysql ${T}/mysql.update-0.1-20051007-to-${PV}.sql 0.1-20051007
        fi
        if use postgresql ; then
                webapp_sqlscript postgres SQL/postgres.initial.sql
        fi
        if use sqlite ; then
                webapp_sqlscript sqlite SQL/sqlite.initial.sql
        fi

        # Identify the configuration files that this app uses
        #
        webapp_configfile ${MY_HTDOCSDIR}/config/db.inc.php
        webapp_configfile ${MY_HTDOCSDIR}/config/main.inc.php

        # all done
        #
        # now we let the eclass strut its stuff ;-)
        #
        webapp_src_install
}

pkg_postinst () {
        webapp_pkg_postinst
}