import os
import re
import sys
import string
import SCons.Errors
import SCons.Util
import SCons.Action
import SCons.Scanner
import tempfile
import subprocess
import _subprocess
import datetime

from SCons.Script import *

# Implements: PLF013
def setuplogging(env):
	DATETIMENOW = datetime.datetime.now().strftime("%Y_%m_%d_%H%M%S")
	CURRENTLOGDIR =  os.getcwd() + os.sep + env.subst('$LOG_DIR')

	if not os.path.exists(CURRENTLOGDIR):
		os.makedirs(CURRENTLOGDIR)
		
	CURRENTLOGFILE = CURRENTLOGDIR + os.sep + DATETIMENOW + '_currentlog.log'

	sys.stdout = Tee(CURRENTLOGFILE)
	sys.stderr = Tee(CURRENTLOGFILE)

# Implements: PLF013
class Tee(object):
	def __init__(self, logfile=None, mode='w'):
		self.stdout = sys.stdout
		sys.stdout = self
		if logfile:
			self.logfile = open(logfile, mode)
		else:
			self.file = None
	def write(self, s):
		if self.logfile:
			self.logfile.write(s)
		self.stdout.write(s)
	def close(self):
		if self.logfile:
			self.logfile.close()
		sys.stdout = self.stdout
	def flush(self):
		pass
		
def w32api_spawn(sh, escape, cmd, args, e_env):
    
    if cmd == "SHELL":
        return SCons.Platform.win32.spawn(sh,escape,args[1], args[1:],e_env)
    cmdline = cmd + ' ' + string.join(args[1:], ' ')
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
    
    proc = subprocess.Popen(
        cmdline, 
        stdin=subprocess.PIPE, 
        stdout=subprocess.PIPE, 
        stderr=subprocess.STDOUT, 
        startupinfo=startupinfo, 
        shell = False, 
        env = None
        )

    data, err = proc.communicate()
    
    print data
    rv = proc.wait()
    if rv:
        print "====="
        print err
        print "====="
    return rv

def w32api_cmdfilearg_spawn(sh, escape, cmd, args, e_env):
    if cmd == "SHELL":
        return w32api_spawn(sh, escape, cmd, args, e_env)
    cmdline = cmd +' '+string.join(args[1:], ' ')
    tmpfilename = ''
    rv = 0
    if len(cmdline) > 32766:
        tfile = tempfile.mkstemp('_args','scons_cmd_',r'c:\temp')
        tmpfilename = tfile[1]
        for n in range(1 ,len(args)):
            ta = args[n]
            try:
                if ta[0] == '"' :
                    ta = ta[1:-1]
            except:
                pass
            os.write(tfile[0],ta + '\n')
        os.close(tfile[0]);
        cmdline = cmd +' @' + tmpfilename 
        rv = w32api_spawn(sh, escape, cmd, [ args[0] , '"@' + tmpfilename + '"' ],e_env)
    else:
        rv = w32api_spawn(sh, escape, cmd, args,e_env)
    if not rv and tmpfilename:
        os.remove(tmpfilename)
    return rv

def validate_environment(env):

	if not env.has_key('COMPILER_TOOL') or env['COMPILER_TOOL'] == '':
		print 'Compiler tool is undefined, aborting the build.'
		Exit(1)
		
	if not env.has_key('COMPILER_ROOT') or env['COMPILER_ROOT'] == '':
		print 'Compiler root path is undefined, aborting the build.'
		Exit(1)
		
	if not env.has_key('LINKER_FILE') or env['LINKER_FILE'] == '':
		print 'Linker file is undefined, aborting the build.'
		Exit(1)
	
	if not env.has_key('CPU_TARGET') or env['CPU_TARGET'] == '':
		print 'CPU target is undefined, aborting the build.'
		Exit(1)
