#! /usr/bin/env python
# encoding: utf-8

import os
import sys
import Configure
import Utils

# the following two variables are used by the target "waf dist"
APPNAME="cgmail"
VERSION = "0.6.1"

VERSION_MAJOR_MINOR = ".".join(VERSION.split(".")[0:2])

# these variables are mandatory ('/' are converted automatically)
srcdir = '.'
blddir = 'build'

def dist():
	"""Make the dist tarball and print its SHA-1 """
	import Scripting
	Scripting.g_gz = "gz"
	Scripting.dist(APPNAME, VERSION)

def set_options(opt):
	# options for disabling pyc or pyo compilation
	opt.tool_options("python")
	opt.tool_options("misc")
	opt.tool_options("gnu_dirs")

def configure(conf):
	conf.check_tool("python")
	conf.check_python_version((2,5,0))
	conf.check_tool("misc gnu_dirs")

	# BUG: intltool requires gcc
	conf.check_tool("gcc intltool")

	python_modules = """
		gio
		gtk
		gnomekeyring
		dbus
		feedparser
		gnome
		pynotify
		xdg
		wnck
		"""
	for module in python_modules.split():
		conf.check_python_module(module)

	Utils.pprint("NORMAL", "Checking optional dependencies:")

	#~ opt_programs = ["dbus-send"]
	#~ for prog in opt_programs:
		#~ prog_path = conf.find_program(prog)

	#~ try:
		#~ conf.check_python_module("<module name>")
	#~ except Configure.ConfigurationError, e:
		#~ Utils.pprint("RED", "Python module <module name> is recommended")
		#~ Utils.pprint("RED", "Please see README")
		
	opt_pymodules = "gst".split()
	for mod in opt_pymodules:
		try:
			conf.check_python_module(mod)
		except Configure.ConfigurationError, e:
			Utils.pprint("YELLOW", "python module %s is recommended" % mod)

	conf.env["VERSION"] = VERSION
	conf.env["PYC"] = 0
	conf.env["PYO"] = 0

	# Check sys.path
	Utils.pprint("NORMAL", "Installing python modules into: %(PYTHONDIR)s" % conf.env)
	pipe = os.popen("""%(PYTHON)s -c "import sys; print '%(PYTHONDIR)s' in sys.path" """ % conf.env)
	if "False" in pipe.read():
		Utils.pprint("YELLOW", "Please add %(PYTHONDIR)s to your sys.path!" % conf.env)

def new_module(bld, name, sources=None):
	if not sources: sources = name
	obj = bld.new_task_gen("py")
	obj.find_sources_in_dirs(sources)
	obj.install_path = "${PYTHONDIR}/%s" % name
	return obj


def build(bld):

	# modules
	new_module(bld, "cGmail", "cGmail")
	new_module(bld, "cGmail/checkers", "cGmail/checkers")
	new_module(bld, "cGmail/lib", "cGmail/lib")
	new_module(bld, "cGmail/manager", "cGmail/manager")
	new_module(bld, "cGmail/service", "cGmail/service")
	
	bld.install_files("${BINDIR}", "cgmail", chmod=0775)
	bld.install_files("${BINDIR}", "cgmailservice", chmod=0775)

	bld.add_subdirs("po data")


def intlupdate(util):
	"""Extract new strings for localization"""
	os.popen("intltool-extract --type=gettext/glade data/account_add_dialog.ui")
	os.popen("intltool-extract --type=gettext/glade data/accounts_treeview_popupmenu.ui")
	os.popen("intltool-extract --type=gettext/glade data/accounts_window.ui")
	os.popen("intltool-extract --type=gettext/glade data/authdialog.ui")
	os.popen("intltool-extract --type=gettext/glade data/junkfilter_dialog.ui")
	os.popen("intltool-extract --type=gettext/glade data/junkfilter_popupmenu.ui")
	os.popen("intltool-extract --type=gettext/glade data/preferences_dialog.ui")
	os.popen("intltool-extract --type=gettext/glade data/statusicon_popupmenu.ui")
	os.chdir("po")
	for line in open("LINGUAS"):
		""" Run xgettext for all listed languages
		to extract translatable strings and merge with
		the old file """
		if line.startswith("#"):
			continue
		lang = line.strip()

		lang_file = "%s.po" % lang
		if not os.path.exists(lang_file):
			print "Creating %s" % lang_file
			os.popen("xgettext -D .. -f POTFILES.in --output=%s -F" % lang_file)
			os.popen("intltool-update %s" % lang)
		else:
			print "Processing", lang
			os.popen("intltool-update %s" % lang)
			os.popen("msgfmt --check %s" % lang_file)

def shutdown():
	pass

