#!/usr/bin/env python

# System modules
import sys
import os
import shlex
import subprocess
import shutil
import platform

from os.path import join

# Library modules
try:
  from setuptools import setup, find_packages
except ImportError:
  from distutils.core import setup

from distutils.dir_util import copy_tree
from distutils.file_util import copy_file

# Package modules
import pyglass

if sys.version_info[:2] < (2, 7):
  print "Sorry, pyglass requires python version 2.7 or later"
  sys.exit(1)

if platform.system() != 'Darwin':
  print "Sorry, pyglass only runs on OS X"
  sys.exit(1)

LIB_DIR = join('pyglass', 'lib')
BUILD_DIR = join('cocoa', 'build')


def rm_tempdirs():
  ''' Remove temporary build folders '''
  tempdirs = ['build', 'dist', BUILD_DIR, LIB_DIR]
  for tempdir in tempdirs:
    if os.path.exists(tempdir):
      shutil.rmtree(tempdir, ignore_errors=True)


def xcodebuild():
  ''' Build & move the QuickGlass binary to lib '''
  # Build from xcodeproj
  os.chdir('cocoa')
  cmd = 'xcodebuild build'
  subprocess.call(shlex.split(cmd))
  os.chdir('..')


# Compile and copy over libs
rm_tempdirs()
xcodebuild()

os.makedirs(LIB_DIR)
copy_file('cocoa/build/Release/QuickGlass', '%s/QuickGlass' % LIB_DIR)
copy_tree('lib/SketchTool/', '%s/SketchTool/' % LIB_DIR)


setup(
  name='pyglass',
  version=pyglass.__version__,
  url='http://github.com/Pixelapse/pyglass',
  description='Mac OS QuickLook Wrapper',
  long_description=open('README.md').read(),
  author='Shravan Reddy',
  author_email='shravan@pixelapse.com',
  packages=find_packages(),
  package_data={'': ['LICENSE', 'lib/QuickGlass']},
  install_requires=['process'],
  include_package_data=True,
  zip_safe=False,
  license=open('LICENSE').read()
)

rm_tempdirs()

@@ -592,75 +592,232 @@
 1)%0A%0A
-LIB_DIR = join('pyglass', 'lib')%0ABUILD_DIR = join('cocoa', 'build')
+%0Aclass Dir:%0A  BUILD = 'build'%0A  DIST = 'dist'%0A  COCOA = 'cocoa'%0A  COCOA_BUILD = join(COCOA, 'build')%0A  LIB = join('pyglass', 'lib')  # Destination directory for vendor/custom libs%0A  VENDOR = 'vendor'  # Third-party libraries
 %0A%0A%0Ad
@@ -893,43 +893,53 @@
  = %5B
-'build', 'dist', BUILD_DIR, LIB_DIR
+Dir.BUILD, Dir.DIST, Dir.COCOA_BUILD, Dir.LIB
 %5D%0A  
@@ -1080,15 +1080,8 @@
 ild 
-& move 
 the 
@@ -1102,14 +1102,23 @@
 ary 
-to lib
+in Release mode
  '''
@@ -1154,23 +1154,25 @@
 s.chdir(
-'cocoa'
+Dir.COCOA
 )%0A  cmd 
@@ -1251,108 +1251,230 @@
 )%0A%0A%0A
-#
+def copy_vendor_libs():%0A  '''
  Co
-m
 pi
-le and cop
+es third part
 y 
-o
 ve
+ndo
 r libs
-%0Arm_tempdirs()%0Axcodebuild()%0A%0Aos.makedirs(LIB_DIR)%0Acopy_file('cocoa/build
+ into the module '''%0A  copy_tree('%25s/' * Dir.Vendor, '%25s/' %25 Dir.LIB)%0A%0A%0Adef copy_custom_libs():%0A  ''' Copies custom build libs into the module '''%0A  copy_file('%25s
 /Rel
@@ -1489,16 +1489,34 @@
 ckGlass'
+ %25 Dir.COCOA_BUILD
 , '%25s/Qu
@@ -1531,74 +1531,498 @@
 ' %25 
-LIB_DIR)%0Acopy_tree('lib/SketchTool/', '%25s/SketchTool/' %25 LIB_DIR)%0A
+Dir.LIB)%0A%0A%0Adef lib_list():%0A  ''' Returns the contents of 'pyglass/lib' as a list of 'lib/*' items for package_data '''%0A  lib_list = %5B%5D%0A  for (root, dirs, files) in os.walk(Dir.LIB):%0A    for filename in files:%0A      root = root.replace('pyglass/', '')%0A      lib_list.append(os.path.join(root, filename))%0A  return lib_list%0A%0A# Compile custom project%0Arm_tempdirs()%0Axcodebuild()%0A%0A# Copy over libs into Dir.LIB%0Aos.makedirs(Dir.LIB)%0Acopy_custom_libs()%0Acopy_vendor_libs()%0A%0Apackage_libs = lib_list()
 %0A%0Ase
@@ -2146,24 +2146,31 @@
  OS 
-QuickLook Wrappe
+X File Preview Generato
 r',%0A
@@ -2332,37 +2332,20 @@
 '': 
-%5B'LICENSE', 'lib/QuickGlass'%5D
+package_libs
 %7D,%0A 

copy vendor and custom libs into the same location