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

inherit eutils autotools

DESCRIPTION="Tool for analysing log files, tcpdump files and hard disk images"
HOMEPAGE="http://www.pyflag.net/cgi-bin/moin.cgi"
SRC_URI="http://downloads.sourceforge.net/${PN}/${P}-pre1.tar.bz2"
LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~x86"
RESTRICT="mirror"
IUSE="doc pst"
COMMON_DEPEND=">=dev-lang/python-2.7.0
		>=app-forensics/sleuthkit-2.03
		net-libs/libpcap
		sys-apps/file
		sys-libs/zlib
		app-antivirus/clamav
		dev-python/mysql-python
		dev-python/pexpect
		dev-python/imaging
		dev-python/python-dateutil
		app-forensics/afflib
		app-forensics/libewf"

RDEPEND="${COMMON_DEPEND}
		virtual/mysql"

DEPEND="pst? ( !net-mail/libpst )
	>=dev-lang/swig-1.3
		${COMMON_DEPEND}"

#
# TODO: init scripts
#

src_unpack() {
	unpack "${A}"
	mv "${S}-pre1" "${S}"
	epatch "${FILESDIR}/pyflag-0.87-11082010.patch" || die "epatch failed"
	cd "${S}"
	if ! use pst;
	then
	 sed -i -e 's/mailtools //' "${S}/src/Makefile.am" || die "pst staff remove failed"
	fi
	if use doc;
	then
		fperms 744 gendoc.sh
	fi
	fperms 755 tests/pyfla*
	fperms 755 utilities/*.py
	eautoreconf || die "autoreconf failed"
}

src_compile() {
	econf || die "econf failed"
        emake || die "emake failed"
	if use doc;
	then
		./gendoc.sh || die "gendoc failed"
	fi
        }

src_install() {
	emake DESTDIR="${D}" install || die "emake install failed"
	if use doc;
	then
		dohtml "${S}"/docs/* || die "dohtml failed"
		dodoc "${S}"/docs/*.txt || die "dodoc failed"
	fi

	#maybe later
	#newinitd "${FILESDIR}/${P}-init"  pyflag
	#newconfd "${FILESDIR}/${P}-conf"  pyflag
}

pkg_postinst() {
	elog "Optionally enter database details in /etc/pyflagrc"
	elog
	elog "To start PyFlag just run \"pyflag\" as a normal user"
	elog "then open your web browser on http://localhost:8000"
	elog
	elog "To create a database user \"emerge --config =${PF}\""
}

pkg_config() {
	ewarn "This creates the approprate adminstrative user for pyflag"
	ewarn "Run pyflag with the entered credentials to create the"
	ewarn "tables and database."
	einfo
	einfo "Enter user to create:"
	read USER
	einfo "Enter user's password:"
	read PASSWORD

	# note - poor privledge separation is used in pyflag
	# recommend not sharing with other security consious databases.
	# it may be possible to imporve this a bit using manual permission setting.
	einfo "Enter mysql root database password:"
	echo GRANT ALL PRIVILEGES ON "*.*" TO $USER@localhost \
		IDENTIFIED BY \"$PASSWORD\"\; FLUSH PRIVILEGES\;  \
		| /usr/bin/mysql -u root -p

	einfo "Do you want this data to be used for all users (y/n)?"
	ewarn "Warning - all credentials will be in a world readable file"
	ewarn "This overwrites settings in ${ROOT}/etc/pyflagrc"
	read  GLOBAL
	if [ "${GLOBAL}" == "y" ] || [ "${GLOBAL}" == "Y" ]; then
		einfo "updating global settings"
		sed -i -e "s:PYFLAG_DBUSER=.*:PYFLAG_DBUSER=$USER:" \
				-e "s:PYFLAG_DBPASSWD=.*:PYFLAG_DBPASSWD=$PASSWORD:" \
				"${ROOT}/etc/pyflagrc"
	fi
}