#!/usr/bin/env python

# Written by Bram Cohen
# see LICENSE.txt for license information

import sys
assert sys.version >= '2', "Install Python 2.0 or greater"
import BitTornado
import BitQueue,BitCrawler

def update_nsi():
    import re
    old = open('btqueue.nsi','r').read()
    new = re.sub(r'''!define MUI_RELEASE "\d+"''',
                 r'''!define MUI_RELEASE "'''+str(BitQueue.version_build)+r'''"''',old)
    new = re.sub(r'''!define MUI_VERSION "[\d\.]+"''',
                 r'''!define MUI_VERSION "'''+BitQueue.version_num+r'''"''',new)
    if new != old:
        fo = open('btqueue.nsi','w')
        fo.write(new)
        fo.close()

def update_cfg():
    setup_cfg = '''[bdist_rpm]
release = %(release)d.%(pymajor)d.%(pyminor)d
packager = Sugree Phatanapherom <sugree@hpcnc.cpe.ku.ac.th>''' % \
    {'release': BitQueue.version_build,
     'pymajor': sys.version_info[0],
     'pyminor': sys.version_info[1]}

    if os.path.exists('setup.cfg'):
        fi = open('setup.cfg','r')
        old_cfg = fi.read()
        fi.close()
    else:
        old_cfg = ''
    if setup_cfg != old_cfg:
        fo = open('setup.cfg','w')
        fo.write(setup_cfg)
        fo.close()

import os
if os.path.exists('btqueue.nsi'):
    update_nsi()

update_cfg()

from distutils.core import setup, Extension

if __name__ == '__main__':
    setup(
        name = "BTQueue",
        version = BitQueue.version_num,
        author = "Sugree Phatanapherom",
        author_email = "<sugree@gmail.com>",
        url = "http://btqueue.sourceforge.net/",
        license = "Python",
    
        packages = ["BitQueue","BitCrawler","BitTornado","BitTornado.BT1","BitTorrent","khashmir"],

        scripts = ["btqueue.py", "btmakemetafile.py", "btshowmetainfo.py",
                   'btsethttpseeds.py'],
        data_files = [('share/BTQueue',['ip2cc.db','examples/alias.conf']),
                      ('share/doc/BTQueue/examples',['examples/tracker.xml',
                                                     'examples/interest.xml'])],
        )
