# -*- coding: utf-8 -*-
"""
UTc! module: setup

Purpose
=======
 Sets up UTc!'s basic operating environment, making it possible for the game to
 be played on a system.
 
Usage
=====
 'python setup [--build]'
 
 If the optional '--build' flag is set, then src.matrix_c will be built,
 assuming your system has python-dev and gcc installed.
 
Legal
=====
 All code, unless otherwise indicated, is original, and subject to the
 terms of the GPLv3, which is provided in COPYING.
 
 (C) Neil Tallim, 2009
"""
from distutils.core import setup, Extension
import compileall
import os
import py_compile
import re
import shutil
import sys

import src.path_utils as path_utils

if __name__ == '__main__':
	client_path = path_utils.getClientPath()
	if not path_utils.getCurrentPath() == client_path:
		sys.stderr.write("This script must be invoked from 'client/'.\n")
		sys.exit(1)
		
	#Build modules.
	src_path = client_path + "src" + os.sep
	if len(sys.argv) == 2 and sys.argv[1] == "--build":
		sys.argv = [sys.argv[0], 'build_ext', '-i']
		print "Building C modules..."
		setup(
		 name="matrix_c",
		 ext_modules = [Extension("src.matrix_c", ["src/matrix_c.c"])]
		)
	else:
		print "Not building C modules..."
		
	print "Compiling Python modules..."
	py_compile.compile("game.py")
	compileall.compile_dir("src/", rx=re.compile(".svn"), quiet=True)
	
	#Set up user data.
	user_path = path_utils.getUserPath()
	
	keybindings_path = user_path + "keybindings.cfg"
	if not os.path.isfile(keybindings_path):
		print "Setting default keybindings..."
		shutil.copy(client_path + "keybindings" + os.sep + "keybindings.cfg.kbd", keybindings_path)
		