'''Builds an OSX single application package'''

import os
import shutil

from SCons.Script import *


def build_function(target, source, env):
    # Create package build dir
    build_dir = 'build/pkg'
    if os.path.exists(build_dir): shutil.rmtree(build_dir)

    # Make root dir
    root_dir = os.path.join(build_dir, 'root')
    os.makedirs(root_dir, 0775)

    # Apps
    if 'pkg_apps' in env:
        d = os.path.join(root_dir, 'Applications')
        os.makedirs(d, 0775)
        env.InstallFiles('pkg_apps', d, None)

    # Other files
    if 'pkg_files' in env:
        env.InstallFiles('pkg_files', root_dir)

    # pkg_id
    if 'app_id' in env and not 'pkg_id' in env:
        env.Replace(pkg_id = env.get('app_id') + '.pkg')

    # pkgbuild command
    cmd = ['${PKGBUILD}',
           '--root', root_dir,
           '--id', env.get('pkg_id'),
           '--version', env.get('version'),
           '--install-location', env.get('pkg_install_to', '/'),
           ]
    if 'pkg_scripts' in env: cmd += ['--scripts', env.get('pkg_scripts')]
    if 'pkg_plist' in env: cmd += ['--component-plist', env.get('pkg_plist')]
    cmd += [build_dir + '/%s.pkg' % env.get('package_name')]

    env.RunCommand(cmd)

    # Filter distribution.xml
    dist = None
    if 'pkg_distribution' in env:
        f = open(env.get('pkg_distribution'), 'r')
        data = f.read()
        f.close()
        dist = build_dir + '/distribution.xml'
        f = open(dist, 'w')
        f.write(data % env)
        f.close()

    # productbuild command
    cmd = ['${PRODUCTBUILD}', '--package-path', root_dir]
    if dist: cmd += ['--distribution', dist]
    if 'pkg_resources' in env:
        cmd += ['--resources', env.get('pkg_resources')]
    if 'sign_id_installer' in env:
        cmd += ['--sign', env.get('sign_id_installer')]
        if 'sign_keychain' in env:
            cmd += ['--keychain', env.get('sign_keychain')]
    cmd += [str(target[0])]

    env.RunCommand(cmd)


def generate(env):
    env.SetDefault(PKGBUILD = 'pkgbuild')
    env.SetDefault(PRODUCTBUILD = 'productbuild')

    bld = Builder(action = build_function,
                  source_factory = SCons.Node.FS.Entry,
                  source_scanner = SCons.Defaults.DirScanner)
    env.Append(BUILDERS = {'Pkg' : bld})

    return True


def exists():
    return 1


@@ -1561,18 +1561,87 @@
 TBUILD%7D'
-, 
+%5D%0A    if dist:%0A        cmd += %5B'--distribution', dist%5D%0A        cmd += %5B
 '--packa
@@ -1650,20 +1650,21 @@
 -path', 
-root
+build
 _dir%5D%0A  
@@ -1669,47 +1669,441 @@
     
-if dist: cmd += %5B'--distribution', dist
+else:%0A        print %22WARNING: No distribution specified. Attempting to build %22 %5C%0A          %22using --root. Package will not have Resources and will put %22 %5C%0A          %22wrong package id in receipts.%22%0A        cmd += %5B'--root', root_dir, env.get('pkg_install_to', '/'),%0A                '--id', env.get('pkg_id'),%0A                '--version', env.get('version')%5D%0A        if 'pkg_scripts' in env: cmd += %5B'--scripts', env.get('pkg_scripts')
 %5D%0A  

use productbuild --root if no distribution given