from distutils.core import setup
from distutils import sysconfig
import py2app
import os
import sys
import subprocess
import shutil
from plistlib import readPlist, writePlist

import vanilla
import defconAppKit
import robofab
import fontTools
import pygments

from drawBotSettings import *

codeSignDeveloperName = None
if "--codesign" in sys.argv:
    try:
        i = sys.argv.index("--codesign")
        codeSignDeveloperName = sys.argv[i+1]
        sys.argv.remove("--codesign")
        sys.argv.remove(codeSignDeveloperName)
    except:
        pass



plist = dict(

	CFBundleDocumentTypes = [
	dict(
		CFBundleTypeExtensions = ["py"],
		CFBundleTypeName = "Python Source File",
		CFBundleTypeRole = "Editor",
		NSDocumentClass = "DrawBotDocument",
	    ),
		
		],
	CFBundleIdentifier = "com.drawbot",
	LSMinimumSystemVersion = "10.6.0",
	CFBundleShortVersionString = version,
	CFBundleVersion = version,
	CFBundleIconFile = "DrawBot.icns",
	NSHumanReadableCopyright = "Copyright by Just van Rossum and Frederik Berlaen.",
    CFBundleURLTypes = [
            dict(
                CFBundleURLName = "com.drawbot",
                CFBundleURLSchemes = [appName.lower()])
        ],
	)


dataFiles = [
        "Resources/English.lproj",
        os.path.dirname(vanilla.__file__),
        ]

for fileName in os.listdir("Resources/Images"):
    baseName, extension = os.path.splitext(fileName)
    if extension.lower() in [".png", ".icns"]:
        fullPath = os.path.join("Resources/Images", fileName)
        dataFiles.append(fullPath)

# build
setup(
    data_files=dataFiles,
    app=[dict(script="DrawBot.py", plist=plist)],
    options = dict(py2app=dict(
                    packages=[
                        os.path.dirname(vanilla.__file__),
                        os.path.dirname(defconAppKit.__file__),
                        os.path.dirname(robofab.__file__),
                        os.path.dirname(pygments.__file__),
                        os.path.dirname(fontTools.__file__),
                        ],
                    )
                )
            )

# fix the icon
path = os.path.join(os.path.dirname(__file__), "dist", "DrawBot.app", "Contents", "Info.plist")
appPlist = readPlist(path)
appPlist["CFBundleIconFile"] = "DrawBot.icns"
writePlist(appPlist, path)

if "-A" not in sys.argv and codeSignDeveloperName:
    # get relevant paths
    distLocation = os.path.join(os.getcwd(), "dist")
    appLocation = os.path.join(distLocation, "%s.app" %appName)

    # ================
    # = code singing =
    # ================
    print "---------------------"
    print "-   code singing    -"
    cmds = ["codesign", "-f", "-s", "Developer ID Application: %s" % codeSignDeveloperName, appLocation]
    popen = subprocess.Popen(cmds)
    popen.wait()
    print "- done code singing -"
    print "---------------------"

@@ -125,16 +125,32 @@
  shutil%0A
+import tempfile%0A
 from pli
@@ -2486,16 +2486,199 @@
 s.app%22 %25
+ appName)%0A    imgLocation = os.path.join(distLocation,  %22img%22)%0A    existingDmgLocation = os.path.join(distLocation,  %22%25s.dmg%22 %25 appName)%0A    dmgLocation = os.path.join(distLocation,  
 appName)
@@ -2678,16 +2678,16 @@
 ppName)%0A
-
 %0A    # =
@@ -2800,26 +2800,26 @@
 %22-   code si
-n
 g
+n
 ing    -%22%0A  
@@ -3003,24 +3003,24 @@
  singing -%22%0A
-
     print %22-
@@ -3021,28 +3021,849 @@
 int %22---------------------%22%0A
+%0A%0A    # ================%0A    # = creating dmg =%0A    # ================%0A%0A    if os.path.exists(existingDmgLocation):%0A        os.remove(existingDmgLocation)%0A    %0A    os.mkdir(imgLocation)%0A    os.rename(os.path.join(distLocation, %22%25s.app%22 %25appName), os.path.join(imgLocation, %22%25s.app%22 %25appName))%0A    tempDmgName = %22%25s.tmp.dmg%22 %25appName%0A%0A    os.system(%22hdiutil create -srcfolder %5C%22%25s%5C%22 -volname %25s -format UDZO %5C%22%25s%5C%22%22 %25(imgLocation, appName, os.path.join(distLocation, tempDmgName)))%0A%0A    os.system(%22hdiutil convert -format UDZO -imagekey zlib-level=9 -o %5C%22%25s%5C%22 %5C%22%25s%5C%22%22 %25(dmgLocation, os.path.join(distLocation, tempDmgName)))%0A%0A    os.remove(os.path.join(distLocation, tempDmgName))%0A%0A    os.rename(os.path.join(imgLocation, %22%25s.app%22 %25appName), os.path.join(distLocation, %22%25s.app%22 %25appName))%0A%0A    shutil.rmtree(imgLocation)%0A%0A

build dmg during setup