#!/usr/bin/python

import sys, re, shutil, os, hashlib
import imp
import getpass

if __name__ == '__main__':

    d = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))
    os.chdir(d)
    v = imp.load_source('version', 'lib/version.py')
    version = v.ELECTRUM_VERSION
    print "version", version

    # copy dependencies into 'packages' directory
    deps = [
        'aes',
        'ecdsa',
        'pbkdf2',
        'requests', # note: requests-2.5.1 is needed to build with pyinstaller
        'qrcode',
        'google/protobuf',
        'dns',
        'six',
        'jsonrpclib',
    ]
    # don't use /usr/lib
    sys.path = ['/usr/local/lib/python2.7/dist-packages']
    for module in deps:
        f, pathname, descr = imp.find_module(module)
        target = 'packages/' + module + descr[0]
        if os.path.exists(target):
            continue
        d = os.path.dirname(target)
        if d and not (os.path.exists(d)):
            os.makedirs(d)
        if descr[0]:
            shutil.copy(pathname, target)
        else:
            shutil.copytree(pathname, target, ignore=shutil.ignore_patterns('*.pyc'))

    # fix google/__init__.py needed by pyinstaller
    n = 'packages/google/__init__.py'
    if not os.path.exists(n):
        os.system("echo \# do not remove>%s"%n)

    os.system("pyrcc4 icons.qrc -o gui/qt/icons_rc.py")
    os.system("python setup.py sdist --format=zip,gztar")

    print "Packages are ready in dist"