# -*- mode: python; -*-

import os

Import("env")
Import("has_option")

env = env.Clone()
env['WIX'] = os.environ.get('WIX')
env['WIXPATH'] = r'$WIX\bin'
env['WIXHEAT'] = r'$WIXPATH\heat.exe'
env['WIXCANDLE'] = r'$WIXPATH\candle.exe'
env['WIXLIGHT'] = r'$WIXPATH\light.exe'
env['WIXUIEXT'] = r'$WIXPATH\WixUIExtension.dll'
env['MERGEMODULESBASEPATH'] = os.environ.get('MERGEMODULESBASEPATH')
if env['MERGEMODULESBASEPATH'] == None and os.environ.get('ProgramFiles(x86)') != None:
    env['MERGEMODULESBASEPATH'] = (os.environ.get('ProgramFiles(x86)') +
            r"\Common Files\Merge Modules")

sourcesList = [ "BinaryFragment.wxs",
            "FeatureFragment.wxs",
            "LicensingFragment.wxs",
            "UIFragment.wxs",
            ]

# Need to do this in order to get scons to translate path separators into native format
buildDir = env.Dir("$BUILD_DIR").path
if has_option("use-new-tools"):
	toolBuildDir = "src\mongo-tools"
else:
	toolBuildDir = buildDir + r'\mongo'

enterprisebase = 'src\mongo\db\modules\enterprise'
enterpriseToolBuildDir = buildDir + r'\mongo\db\modules\enterprise'

# Set up parameters to pass to wix -
#
# msi_edition - "Enterprise" or "Standard"
# msi_platform - "x64" or "x86"
# msi_flavor - "2008R2Plus" or ""
#

# Enterprise
if 'enterprise' in env['MONGO_MODULES']:
  msi_edition = 'Enterprise'
  msi_flavor = '2008R2Plus'
  msi_platform = 'x64'
  upgrade_code = 'E7FE8DF3-00F1-4434-97DF-2721E7F712FA'
# Community
else:
  if has_option('ssl'):
    msi_edition = 'SSL'
  else:
    msi_edition = 'Standard'
  if env['TARGET_ARCH'] == 'i386':
    msi_platform = 'x86'
    msi_flavor = ''
    upgrade_code = 'B2E70C13-483E-4E16-B6AA-FCCA7983B767'
  else:
    msi_platform = 'x64'
    if env.get('WIN_VERSION_MIN') == 'ws08r2' or env.get('WIN_VERSION_MIN') == 'win7':
      msi_flavor = '2008R2Plus'
      if msi_edition == 'SSL':
        upgrade_code = '86CB92A1-E631-4C63-89D2-40A7159B938B'
      else:
        upgrade_code = '448C9172-C5E7-42E0-81CB-00DAA191DD98'
    else:
      msi_flavor = 'Legacy'
      upgrade_code = '54BEB8CC-C8F7-4292-A411-BDE0A9F21CA3'

if 'msi' in BUILD_TARGETS and msi_edition == 'SSL' and msi_flavor != '2008R2Plus':
  print "Building the MongoDB SSL MSI is only supported on Windows 2008 R2+ or Windows 7+ platforms."
  print "You must add --win-version-min=ws08r2 to your scons flags"
  exit(1)

if msi_platform == 'x64':
  sourcesList.append("Installer_64.wxs")
else:
  sourcesList.append("Installer.wxs")

sources = ["wxs/" + file for file in sourcesList]
objects = ["$BUILD_DIR/msi/" + file.replace(".wxs", ".wixobj") for file in sourcesList]

full_version = env['MONGO_VERSION'].partition('-')[0]

# major version is the x.y, not the x.y.z
major_version = full_version
mv = major_version.split('.')
major_version = "%s.%s" % (mv[0], mv[1])

# Currently, we are planning to key the same upgrade code for each
# (msi_edition, msi_platform, msi_flavor) combination
# and change MSI ProductId on minor updates, 2.6.0 -> 2.6.1, we let Wix do automatic
# GUID generation for us rather then build a database of GUIDs in our build system
# For major updates, we are going to create a new directory/productid/upgrade_code ie, 2.6 -> 3.0


# candle: compile .wxs files into .wixobjs
env.Command(objects,
            sources,
            '"$WIXCANDLE" -wx'
            # cannot have anything other than x.x.x.x in version string.
            # we should choose a fourth version number that reflects pre-ness.
            ' -dMongoDBMajorVersion=' + major_version +
            ' -dMongoDBVersion=' + full_version +
            ' -dLicenseSource=distsrc'
            r' -dEnterpriseBase=' + enterprisebase + '\\'
            ' -dBinarySource=' + buildDir + r'\mongo'
            ' -dToolBinarySource=' + toolBuildDir +
            ' -dEnterpriseToolBinarySource=' + enterpriseToolBuildDir +
            ' -dMergeModulesBasePath="$MERGEMODULESBASEPATH"'
            ' -dEdition=' + msi_edition +
            ' -d"ProductId=*\"'
            ' -dUpgradeCode=' + upgrade_code +
            ' -dClientSource=' + buildDir + r'\client_build'
            r' -dClientHeaderSource=${INSTALL_DIR}\include\mongo'
            ' -dConfiguration=Release'
            ' -dOutDir=' + buildDir + r'\msi'
            ' -dPlatform=' + msi_platform +
            ' -dFlavor=' + msi_flavor +
            r' -dProjectDir=buildscripts\packaging\msi\\'
            ' -dProjectName=MongoDB'
            ' -dTargetDir=' + buildDir + r'\msi'
            ' -dTargetExt=.msi'
            ' -dTargetFileName=${SERVER_ARCHIVE}'
            r' -dSaslSource=c:\sasl\bin'
            r' -dSnmpSource=c:\snmp\bin'
            r' -dSslSource=' + env['WINDOWS_OPENSSL_BIN'] +
            ' -out ' + buildDir + r'\msi\\'
            ' -arch ' + msi_platform +
            ' -ext "$WIXUIEXT"'
            ' $SOURCES')

#light: link .objs into an msi
pre_msi = "$BUILD_DIR/msi/${SERVER_DIST_BASENAME}.pre.msi"
pre_msi_cmd = env.Command(pre_msi,
            objects,
            '"$WIXLIGHT" -out ${TARGET} -wx -cultures:null -sice:ICE82 '
            ' -ext "$WIXUIEXT"'
            ' ${SOURCES}')

if 'enterprise' in env['MONGO_MODULES']:
    env.Depends(pre_msi_cmd, "#" + enterpriseToolBuildDir + "/mongodecrypt.exe")

msi = "$BUILD_DIR/msi/${SERVER_DIST_BASENAME}.msi"
env.Command(msi,
            pre_msi,
            r'$PYTHON buildscripts\msitrim.py ${SOURCES} ${TARGET}')
env.AlwaysBuild(msi)

env.Alias( "msi" , msi )

