#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright(C) 2010  Christophe Benz
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


from __future__ import with_statement

from setuptools import find_packages, setup

import glob
from optparse import OptionParser
import os
import subprocess
import sys

def check_executable(executable, error):
    with open('/dev/null', 'w') as devnull:
        process = subprocess.Popen(['which', executable], stdout=devnull)
        return_code = process.wait()
    if return_code == 0:
        return True
    else:
        print >>sys.stderr, 'Error: %s is not installed on your system.' % executable
        if error:
            print >>sys.stderr, error
        sys.exit(1)

def build_qt():
    print 'Building Qt applications'
    check_executable('pyuic4', 'To disable Qt applications, use --no-qt.')

    os.system('make -C weboob/applications/qboobmsg/ui')
    os.system('make -C weboob/applications/qhavesex/ui')
    os.system('make -C weboob/applications/qvideoob/ui')
    os.system('make -C weboob/tools/application/qt')

def install_xdg():
    """
    On xdg-compliant systems, install desktop file and icon
    """
    print 'Installing desktop menu files'
    check_executable('xdg-desktop-menu', 'To disable resources installation, use --no-xdg.')

    os.system('xdg-desktop-menu install --novendor desktop/*.desktop')
    for filepath in glob.glob('icons/*'):
        print 'Installing icon %s' % filepath
        os.system('xdg-icon-resource install --size 64 --novendor %s' % filepath)

option_parser = OptionParser()
option_parser.add_option('--xdg', action='store_true', default=True, help='Install desktop files and icons')
option_parser.add_option('--no-xdg', action='store_false', dest='xdg', help='Don\'t install desktop files and icons')
option_parser.add_option('--qt', action='store_true', default=True, help='Install Qt applications')
option_parser.add_option('--no-qt', action='store_false', dest='qt', help='Don\'t install Qt applications')
options, sys.argv = option_parser.parse_args(sys.argv)

qt_scripts = ('qboobmsg', 'qhavesex', 'qvideoob', 'weboob-config-qt')
scripts = os.listdir('scripts')

if options.qt:
    build_qt()
else:
    scripts = set(scripts) - set(qt_scripts)

setup(
    name='weboob',
    version='dev',
    description='Weboob, Web Out Of Browsers - development version',
    author='Romain Bignon',
    author_email='weboob@weboob.org',
    maintainer='Christophe Benz',
    maintainer_email='christophe.benz@gmail.com',
    license='GPLv3',
    url='http://www.weboob.org',
    packages=find_packages(),
    scripts=[os.path.join('scripts', script) for script in scripts],
    install_requires=[
        # 'ClientForm', # python-clientform
        # 'elementtidy', # python-elementtidy
        # 'FeedParser', # python-feedparser
        # 'gdata', # python-gdata
        # 'html5lib', # python-html5lib
        # 'lxml', # python-lxml
        # 'Mako', # python-mako
        # 'mechanize', # python-mechanize
        # 'PIL', # python-imaging
        # 'PyQt', # python-qt4
        # 'python-dateutil', # python-dateutil
        # 'PyYAML', # python-yaml
        # 'Routes', # python-routes
        # 'simplejson', # python-simplejson
        # 'WebOb', # python-webob
        ],
)

if sys.argv[1] in ('install', 'develop') and options.xdg:
    install_xdg()

@@ -905,16 +905,17 @@
 rt sys%0A%0A
+%0A
 def chec
@@ -2147,16 +2147,17 @@
 epath)%0A%0A
+%0A
 option_p
@@ -2855,16 +2855,438 @@
 ripts)%0A%0A
+qt_packages = (%0A    'weboob.applications.qboobmsg',%0A    'weboob.applications.qboobmsg.ui',%0A    'weboob.applications.qhavesex',%0A    'weboob.applications.qhavesex.ui',%0A    'weboob.applications.qvideoob',%0A    'weboob.applications.qvideoob.ui',%0A    'weboob.applications.qweboobcfg',%0A    'weboob.applications.qweboobcfg.ui',%0A    )%0Apackages = find_packages()%0A%0Aif not options.qt:%0A    packages = set(packages) - set(qt_packages)%0A%0A
 setup(%0A 
@@ -3608,29 +3608,24 @@
 ackages=
-find_
 packages
 (),%0A    
@@ -3616,18 +3616,16 @@
 packages
-()
 ,%0A    sc

install qt packages only if --qt