#! /usr/bin/env python

# Some of this script is from Pete Shinners pygame2exe script.
#(data copying and pygame icon changing) Special thanks to him!


# import modules
from distutils.core import setup
import sys, os, shutil, pygame
import py2exe


#########################
### Variables to edit ###
#########################
# Stuff to show who made it, etc.
copyright = "Copyright (C) 2008, Benedict Carter"
author = "Benedict Carter"
company = "Benedict Carter"
version = "3.14"

script = "main.py"  # Starting .py or .pyw script
dest_file = "Plague"   # Final name of .exe file
dest_folder = "../PlagueWinExe" # Final folder for the files
icon_file = "./res/plague.ico"  # Icon file. Leave blank for the pygame icon.
# add resource data - ./res is my extra resources folder
resource_path = ""
resource_tree = os.walk(resource_path)
extra_data = []

for base in resource_tree:
    #print base
    for file in base[2]:
        if (base[0] == "res" or base[0] == "levels" or base[0] == "res\\sound") and not file.endswith('.db'):
            extra_data.append([dest_folder+"/"+base[0],"./"+base[0]+"/"+file])
        if file.endswith('.rtf'):
            extra_data.append([dest_folder+"/"+base[0],"./"+base[0]+"/"+file])
# add extra modules
extra_modules = []
for base in resource_tree:
    for file in base[2]:
        if base[0] == "" and file.endswith('.py'):
            if file == 'main.py' or file == 'test.py' or file == 'make_win_exe.py':
                pass
            else:
                extra_modules.append(file)

excludes = ["Tkconstants","Tkinter","tcl","numpy"]

dll_excludes = []
#dll_excludes = ["w9xpopen.exe", "msvcr71.dll"]


##################################################################
### Below if you edit variables, you could mess up the program ###
##################################################################

dest_file = dest_file + " " + version

# Run the script if no commands are supplied 
if len(sys.argv) == 1:
    sys.argv.append("py2exe")
    sys.argv.append("-q")


# Use the pygame icon if there's no icon designated
if icon_file is '':
    path = os.path.split(pygame.__file__)[0]
    icon_file = '' + os.path.join(path, 'pygame.ico') 


# Copy extra data files
def installfile(filepair):
    dst = os.path.join(filepair[0])
    name = filepair[1]
    print 'copying', name, '->', dst
    if os.path.isdir(name):
        dst = os.path.join(dst, name)
        if os.path.isdir(dst):
            shutil.rmtree(dst)
        shutil.copytree(name, dst)
    elif os.path.isfile(name):
        shutil.copy(name, dst)
    else:
        print 'Warning, %s not found' % name
        

##############################
### Distutils setup script ###
##############################

# Set some variables for the exe
class Target:
    def __init__(self, **kw):
        self.__dict__.update(kw)
        self.version = version
        self.company_name = company
        self.author = author
        self.copyright = copyright
        self.name = dest_file
        

# Set some more variables for the exe
target = Target(

    script = script,
    icon_resources = [(1, icon_file)],
    dest_base = dest_file,
    extra_modules = extra_modules,
    excludes = excludes
    )


# Run the setup script!
setup(
    options = {"py2exe": {"compressed": 1,
                          "optimize": 2,
                          "bundle_files": 1,
                          "dll_excludes": dll_excludes,
                          "dist_dir": dest_folder,
                          "excludes": excludes
                          }},
    zipfile = None,
    windows = [target],
    )


# install extra data files
print '\n' # Just a space to make it look nicer :)
for d in extra_data:
    installfile(d)

# If everything went okay, this should come up.
print '\n Conversion successful!'
