# Sconstruct file for P4OfficeDiff
import string, os, re, glob, pprint

def filename(p):    # Return filename bit
    return os.path.split(p)[1]

VB6 = r'"C:\Program Files (x86)\Microsoft Visual Studio\VB98\VB6.exe"'
MSBUILD = r'"C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe"'
INNOSETUP = r'"C:\Program Files (x86)\Inno Setup 5\iscc.exe"'

#---------------------------------
# Also VB scanner for dependencies like:
# Module=Register; Register.bas
# Form=TimerForm.frm
# Form=p4vbTestForm.frm
# Class=WinHelp; WinHelp.cls
vbp_module_re = re.compile(r'^Module=\S+; (\S+)\s*$', re.M)
vbp_form_re = re.compile(r'^Form=(\S+)\s*$', re.M)
vbp_class_re = re.compile(r'^Class=\S+; (\S+)\s*$', re.M)

def vbp_scan(node, env, target, arg):
    contents = node.get_contents()
    includes = vbp_module_re.findall(contents)
    includes += vbp_form_re.findall(contents)
    includes += vbp_class_re.findall(contents)
    return includes

vbpscan = Scanner(name = 'vbp',
                function = vbp_scan,
                argument = None,
                skeys = ['.vbp'])

scanners = Environment().Dictionary('SCANNERS')

#---------------------------------
# Scan MSDEV SLN file for dependencies
vcxproj_re = re.compile(r'<File\s+RelativePath="([^"]*)', re.M)
vcxproj_re_exclude = re.compile(r'.*_i\..*')

def vcxproj_scan(node, env, target, arg):
    # print "Node: " + node.name + ': ' + node.get_abspath()
    contents = node.get_contents()
    includes = vcxproj_re.findall(contents)
    # print "Includes: " + ' '.join(includes)
    # Remove files generated by MIDL compiler (e.g. *_i.c or similar)
    includes = filter(lambda x:not vcxproj_re_exclude.match(x), includes)
    # print "Filtered Includes: " + ' '.join(includes)
    return includes

vcxprojscan = Scanner(name = 'vcxproj',
                function = vcxproj_scan,
                argument = None,
                skeys = ['.vcxproj'])

#---------------------------------
# Builder for Setup compiler
mkinstall = Builder(action = INNOSETUP + ' $SOURCES')
msvb = Builder(action = VB6 + ' /make $SOURCES')
msbuild = Builder(action = MSBUILD + ' $SOURCES /p:Configuration=Release')
 
files_dir = 'install/files'
setup = 'install/Output/P4OfficeDiffSetup.exe'
p4officediff = 'src/p4officediff.exe'
update_settings = 'update_settings/Release/update_settings.exe'

# Default target to build
Default(setup)
vars_to_copy = ['PATH', 'TEMP', 'TMP', 'SYSTEMDRIVE', 'SYSTEMROOT']
env_vars = {}
for k in vars_to_copy:
    env_vars[k] = os.environ[k]
env = Environment(BUILDERS = {'mkinstall' : mkinstall,
                              'msbuild' : msbuild,
                              'msvb' : msvb},
                  SCANNERS = scanners + [vbpscan, vcxprojscan],
                  ENV = env_vars)

FILES = []
env.Install(dir = files_dir, source = FILES)

for f in FILES:
    env.Depends(setup, files_dir + '/' + filename(f))

env.msvb(target = p4officediff, source = 'src/p4officediff.vbp')
env.msbuild(target = update_settings, source = 'update_settings/update_settings.vcxproj')

for f in [p4officediff, update_settings]:
    env.Install(dir = files_dir, source = f)
    env.Depends(setup, files_dir + '/' + filename(f))

env.mkinstall(target = setup, source = 'install/P4OfficeDiffSetup.iss')