import os, shutil, datetime, re, time
import Version

def generateDefaultTheme(dir) :
  sourceDir = dir.abspath
  result = "<!-- WARNING: This file is automatically generated. Any changes will be overwritten. -->\n"
  result += "<RCC version =\"1.0\">"
  result += "<qresource prefix=\"/themes/Default\">"
  for (path, dirs, files) in os.walk(sourceDir) :
    for file in files :
      filePath = os.path.join(path,file)
      result += "<file alias=\"%(alias)s\">%(path)s</file>" % { 
          "alias": filePath[len(sourceDir)+1:],
          "path": filePath
        }
  result += "</qresource>"
  result += "</RCC>"
  return result

Import("env")

myenv = env.Clone()
myenv["CXXFLAGS"] = filter(lambda x : x != "-Wfloat-equal", myenv["CXXFLAGS"])
myenv.UseFlags(env["SWIFT_CONTROLLERS_FLAGS"])
myenv.UseFlags(env["SWIFTOOLS_FLAGS"])
if myenv["HAVE_XSS"] :
  myenv.UseFlags(env["XSS_FLAGS"])
if env["PLATFORM"] == "posix" :
  myenv.Append(LIBS = ["X11"])
if myenv["HAVE_SPARKLE"] :
  myenv.UseFlags(env["SPARKLE_FLAGS"])
myenv.UseFlags(env["SWIFTEN_FLAGS"])
myenv.UseFlags(env["SWIFTEN_DEP_FLAGS"])
myenv.UseFlags(env["BREAKPAD_FLAGS"])
if myenv.get("HAVE_GROWL", False) :
	myenv.UseFlags(myenv["GROWL_FLAGS"])
	myenv.Append(CPPDEFINES = ["HAVE_GROWL"])
if myenv["swift_mobile"] :
	myenv.Append(CPPDEFINES = ["SWIFT_MOBILE"])
if myenv.get("HAVE_SNARL", False) :
	myenv.UseFlags(myenv["SNARL_FLAGS"])
	myenv.Append(CPPDEFINES = ["HAVE_SNARL"])
if env["PLATFORM"] == "win32" :
	myenv.Append(LIBS = ["cryptui"])
myenv.UseFlags(myenv["PLATFORM_FLAGS"])

myenv.Tool("qt4", toolpath = ["#/BuildTools/SCons/Tools"])
myenv.Tool("nsis", toolpath = ["#/BuildTools/SCons/Tools"])
myenv.Tool("wix", toolpath = ["#/BuildTools/SCons/Tools"])
myenv.Tool("textfile", toolpath = ["#/BuildTools/SCons/Tools"])
qt4modules = ['QtCore', 'QtGui', 'QtWebKit']
if env["PLATFORM"] == "posix" :
	qt4modules += ["QtDBus"]
if env["PLATFORM"] != "win32" and env["PLATFORM"] != "darwin" :
	qt4modules += ["QtNetwork"]

myenv.EnableQt4Modules(qt4modules, debug = False)

myenv.Append(CPPPATH = ["."])

if env["PLATFORM"] == "win32" :
  #myenv["LINKFLAGS"] = ["/SUBSYSTEM:CONSOLE"]
  myenv.Append(LINKFLAGS = ["/SUBSYSTEM:WINDOWS"])
  myenv.Append(LIBS = "qtmain")
  if myenv.get("HAVE_SCHANNEL", 0) :
    myenv.Append(LIBS = "Cryptui")
    myenv.Append(CPPDEFINES = "HAVE_SCHANNEL")
    if env["debug"] and not env["optimize"]:
      myenv.Append(LINKFLAGS = ["/NODEFAULTLIB:msvcrt"])

myenv.WriteVal("DefaultTheme.qrc", myenv.Value(generateDefaultTheme(myenv.Dir("#/Swift/resources/themes/Default"))))

sources = [
    "main.cpp",
    "QtAboutWidget.cpp",
    "QtAvatarWidget.cpp",
    "QtUIFactory.cpp",
    "QtChatWindowFactory.cpp",
    "QtChatWindow.cpp",
    "QtClickableLabel.cpp",
    "QtLoginWindow.cpp",
    "QtMainWindow.cpp",
    "QtProfileWindow.cpp",
    "QtNameWidget.cpp",
    "QtSettingsProvider.cpp",
    "QtStatusWidget.cpp",
    "QtScaledAvatarCache.cpp",
    "QtSwift.cpp",
    "QtURIHandler.cpp",
    "QtChatView.cpp",
    "QtChatTheme.cpp",
    "QtChatTabs.cpp",
    "QtSoundPlayer.cpp",
    "QtSystemTray.cpp",
    "QtCachedImageScaler.cpp",
    "QtTabbable.cpp",
    "QtTabWidget.cpp",
    "QtTextEdit.cpp",
    "QtXMLConsoleWidget.cpp",
    "QtHistoryWindow.cpp",
    "QtFileTransferListWidget.cpp",
    "QtFileTransferListItemModel.cpp",
    "QtAdHocCommandWindow.cpp",
    "QtUtilities.cpp",
    "QtBookmarkDetailWindow.cpp",
    "QtAddBookmarkWindow.cpp",
    "QtEditBookmarkWindow.cpp",
    "QtContactEditWindow.cpp",
    "QtContactEditWidget.cpp",
    "ChatSnippet.cpp",
    "MessageSnippet.cpp",
    "SystemMessageSnippet.cpp",
    "QtElidingLabel.cpp",
    "QtFormWidget.cpp",
    "QtFormResultItemModel.cpp",
    "QtLineEdit.cpp",
    "QtJoinMUCWindow.cpp",
    "QtInviteToChatWindow.cpp",
    "QtConnectionSettingsWindow.cpp",
    "Roster/RosterModel.cpp",
    "Roster/QtTreeWidget.cpp",
#    "Roster/QtTreeWidgetItem.cpp",
    "Roster/RosterDelegate.cpp",
    "Roster/GroupItemDelegate.cpp",
    "Roster/DelegateCommons.cpp",
    "Roster/QtRosterWidget.cpp",
    "Roster/QtOccupantListWidget.cpp",
    "EventViewer/EventModel.cpp",
    "EventViewer/EventDelegate.cpp",
    "EventViewer/TwoLineDelegate.cpp",
    "EventViewer/QtEventWindow.cpp",
    "EventViewer/QtEvent.cpp",
    "ChatList/QtChatListWindow.cpp",
    "ChatList/ChatListModel.cpp",
    "ChatList/ChatListDelegate.cpp",
    "ChatList/ChatListMUCItem.cpp",
    "ChatList/ChatListRecentItem.cpp",
    "ChatList/ChatListWhiteboardItem.cpp",
    "MUCSearch/QtMUCSearchWindow.cpp",
    "MUCSearch/MUCSearchModel.cpp",
    "MUCSearch/MUCSearchRoomItem.cpp",
    "MUCSearch/MUCSearchEmptyItem.cpp",
    "MUCSearch/MUCSearchDelegate.cpp",
    "UserSearch/QtUserSearchFirstPage.cpp",
    "UserSearch/QtUserSearchFieldsPage.cpp",
    "UserSearch/QtUserSearchResultsPage.cpp",
    "UserSearch/QtUserSearchDetailsPage.cpp",
    "UserSearch/QtUserSearchWindow.cpp",
    "UserSearch/UserSearchModel.cpp",
    "UserSearch/UserSearchDelegate.cpp",
	"Whiteboard/FreehandLineItem.cpp",
	"Whiteboard/GView.cpp",
	"Whiteboard/TextDialog.cpp",
	"Whiteboard/QtWhiteboardWindow.cpp",
	"Whiteboard/ColorWidget.cpp",
    "QtSubscriptionRequestWindow.cpp",
    "QtRosterHeader.cpp",
    "QtWebView.cpp",
    "qrc_DefaultTheme.cc",
    "qrc_Swift.cc",
    "QtChatWindowJSBridge.cpp",
    "QtMUCConfigurationWindow.cpp",
    "QtAffiliationEditor.cpp",
    "QtUISettingConstants.cpp",
    "QtURLValidator.cpp"
  ]

# Determine the version
myenv["SWIFT_VERSION"] = Version.getBuildVersion(env.Dir("#").abspath, "swift")
if env["PLATFORM"] == "win32" :
	swift_windows_version = Version.convertToWindowsVersion(myenv["SWIFT_VERSION"])
	myenv["SWIFT_VERSION_MAJOR"] = swift_windows_version[0]
	myenv["SWIFT_VERSION_MINOR"] = swift_windows_version[1]
	myenv["SWIFT_VERSION_PATCH"] = swift_windows_version[2]


if env["PLATFORM"] == "win32" :
	res_env = myenv.Clone()
	res_env.Append(CPPDEFINES = [
			("SWIFT_COPYRIGHT_YEAR", "\"\\\"2010-%s\\\"\"" % str(time.localtime()[0])),
			("SWIFT_VERSION_MAJOR", "${SWIFT_VERSION_MAJOR}"),
			("SWIFT_VERSION_MINOR", "${SWIFT_VERSION_MINOR}"),
			("SWIFT_VERSION_PATCH", "${SWIFT_VERSION_PATCH}"),
		])
	res = res_env.RES("#/Swift/resources/Windows/Swift.rc")
  # For some reason, SCons isn't picking up the dependency correctly
	# Adding it explicitly until i figure out why
	myenv.Depends(res, "../Controllers/BuildVersion.h")
	sources += [
			"WinUIHelpers.cpp",
			"CAPICertificateSelector.cpp",
			"WindowsNotifier.cpp",
			"#/Swift/resources/Windows/Swift.res"
		]

if env["PLATFORM"] == "posix" :
	sources += [
			"FreeDesktopNotifier.cpp",
			"QtDBUSURIHandler.cpp",
	]

if env["PLATFORM"] == "darwin" :
	sources += ["CocoaApplicationActivateHelper.mm"]
	sources += ["CocoaUIHelpers.mm"]

if env["PLATFORM"] == "darwin" or env["PLATFORM"] == "win32" :
  swiftProgram = myenv.Program("Swift", sources)
else :
  sources += ["QtCertificateViewerDialog.cpp"];
  myenv.Uic4("QtCertificateViewerDialog.ui");
  swiftProgram = myenv.Program("swift-im", sources)

if env["PLATFORM"] != "darwin" and env["PLATFORM"] != "win32" :
	openURIProgram = myenv.Program("swift-open-uri", "swift-open-uri.cpp")
else :
	openURIProgram = []

myenv.Uic4("MUCSearch/QtMUCSearchWindow.ui")
myenv.Uic4("UserSearch/QtUserSearchWizard.ui")
myenv.Uic4("UserSearch/QtUserSearchFirstPage.ui")
myenv.Uic4("UserSearch/QtUserSearchFieldsPage.ui")
myenv.Uic4("UserSearch/QtUserSearchResultsPage.ui")
myenv.Uic4("QtBookmarkDetailWindow.ui")
myenv.Uic4("QtAffiliationEditor.ui")
myenv.Uic4("QtJoinMUCWindow.ui")
myenv.Uic4("QtHistoryWindow.ui")
myenv.Uic4("QtConnectionSettings.ui")
myenv.Qrc("DefaultTheme.qrc")
myenv.Qrc("Swift.qrc")

# Resources
commonResources = {
	"": ["#/Swift/resources/sounds"]
}

myenv["TEXTFILESUFFIX"] = ""
myenv.MyTextfile(target = "COPYING", source = [myenv.File("../../COPYING.gpl"), myenv.File("../../COPYING.thirdparty")], LINESEPARATOR = "\n\n========\n\n\n")

################################################################################
# Translation
################################################################################

# Collect available languages
translation_languages = []
for file in os.listdir(Dir("#/Swift/Translations").abspath) :
	if file.startswith("swift_") and file.endswith(".ts") :
		translation_languages.append(file[6:-3])

# Generate translation modules
translation_sources = [env.File("#/Swift/Translations/swift.ts").abspath]
translation_modules = []
for lang in translation_languages :
	translation_resource = "#/Swift/resources/translations/swift_" + lang + ".qm"
	translation_source = "#/Swift/Translations/swift_" + lang + ".ts"
	translation_sources.append(env.File(translation_source).abspath)
	translation_modules.append(env.File(translation_resource).abspath)
	myenv.Qm(translation_resource, translation_source)
	commonResources["translations"] = commonResources.get("translations", []) + [translation_resource]

# LUpdate translation (if requested)
if ARGUMENTS.get("update_translations", False) :
	myenv.Precious(translation_sources)
	remove_obsolete_option = ""
	if ARGUMENTS.get("remove_obsolete_translations", False) :
		remove_obsolete_option = " -no-obsolete"
	for translation_source in filter(lambda x: not x.endswith("_en.ts"), translation_sources) :
		t = myenv.Command([translation_source], [], [myenv.Action("$QT4_LUPDATE -I " + env.Dir("#").abspath + remove_obsolete_option + " -silent -codecfortr utf-8 -recursive Swift -ts " + translation_source, cmdstr = "$QT4_LUPDATECOMSTR")])
		myenv.AlwaysBuild(t)

# NSIS installation script
if env["PLATFORM"] == "win32" :
	nsis_translation_install_script = ""
	nsis_translation_uninstall_script = ""
	for lang in translation_languages :
		nsis_translation_install_script += "File \"..\\..\\QtUI\\Swift\\translations\\swift_" + lang + ".qm\"\n"
		nsis_translation_uninstall_script += "delete $INSTDIR\\translations\\swift_" + lang + ".qm\n"
	myenv.WriteVal("../Packaging/nsis/translations-install.nsh", myenv.Value(nsis_translation_install_script))
	myenv.WriteVal("../Packaging/nsis/translations-uninstall.nsh", myenv.Value(nsis_translation_uninstall_script))
	

################################################################################

if env["PLATFORM"] == "darwin" :
  frameworks = []
  if env["HAVE_SPARKLE"] :
    frameworks.append(env["SPARKLE_FRAMEWORK"])
  if env["HAVE_GROWL"] :
    frameworks.append(env["GROWL_FRAMEWORK"])
  commonResources[""] = commonResources.get("", []) + ["#/Swift/resources/MacOSX/Swift.icns"]
  app = myenv.AppBundle("Swift", version = myenv["SWIFT_VERSION"], resources = commonResources, frameworks = frameworks, handlesXMPPURIs = True)
  if env["DIST"] :
    myenv.Command(["#/Packages/Swift/Swift-${SWIFT_VERSION}.dmg"], [app], ["Swift/Packaging/MacOSX/package.sh " + app.path + " Swift/Packaging/MacOSX/Swift.dmg.gz $TARGET $QTDIR"])
    dsym = myenv.Command(["Swift-${SWIFT_VERSION}.dSYM"], ["Swift"], ["dsymutil -o ${TARGET} ${SOURCE}"])
    myenv.Command(["#/Packages/Swift/Swift-${SWIFT_VERSION}.dSYM.zip"], dsym, ["cd ${SOURCE.dir} && zip -r ${TARGET.abspath} ${SOURCE.name}"])
    
if env.get("SWIFT_INSTALLDIR", "") :
  env.Install(os.path.join(env["SWIFT_INSTALLDIR"], "bin"), swiftProgram + openURIProgram)
  env.InstallAs(os.path.join(env["SWIFT_INSTALLDIR"], "share", "pixmaps", "swift.xpm"), "#/Swift/resources/logo/logo-icon-32.xpm")
  icons_path = os.path.join(env["SWIFT_INSTALLDIR"], "share", "icons", "hicolor")
  env.InstallAs(os.path.join(icons_path, "32x32", "apps", "swift.xpm"), "#/Swift/resources/logo/logo-icon-32.xpm")
  env.InstallAs(os.path.join(icons_path, "scalable", "apps", "swift.svg"), "#/Swift/resources/logo/logo-icon.svg")
  for i in ["16", "22", "24", "64", "128"] :
    env.InstallAs(os.path.join(icons_path, i + "x" + i, "apps", "swift.png"), "#/Swift/resources/logo/logo-icon-" + i + ".png")
  env.Install(os.path.join(env["SWIFT_INSTALLDIR"], "share", "applications"), "#/Swift/resources/swift.desktop")
  for dir, resource in commonResources.items() : 
    env.Install(os.path.join(env["SWIFT_INSTALLDIR"], "share", "swift", dir), resource)
    
if env["PLATFORM"] == "win32" :
  if env["DIST"] :
    commonResources[""] = commonResources.get("", []) + [
        #os.path.join(env["OPENSSL_DIR"], "bin", "ssleay32.dll"),  
        #os.path.join(env["OPENSSL_DIR"], "bin", "libeay32.dll"),
        "#/Swift/resources/images",  
      ]
    if env["SWIFTEN_DLL"] :
      commonResources[""] = commonResources.get("", []) + ["#/Swiften/${SWIFTEN_LIBRARY_FILE}"]
    qtimageformats = ["gif", "ico", "jpeg", "mng", "svg", "tiff"]
    qtlibs = ["QtCore4", "QtGui4", "QtNetwork4", "QtWebKit4", "QtXMLPatterns4", "phonon4"]
    windowsBundleFiles = myenv.WindowsBundle("Swift", 
      resources = commonResources,
      qtimageformats = qtimageformats,
      qtlibs = qtlibs)

    #myenv.Append(NSIS_OPTIONS = [
    #    "/DmsvccRedistributableDir=\"" + env["vcredist"] + "\"", 
    #    "/DbuildVersion=" + myenv["SWIFT_VERSION"]
    #  ])
    #myenv.Nsis("../Packaging/nsis/swift.nsi")
    if env["SCONS_STAGE"] == "build" and env.get("wix_bindir", None):
      def convertToRTF(env, target, source) :
        infile = open(source[0].abspath, 'r')
        outfile = open(target[0].abspath, 'w')
        outfile.write('{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\fs16\\f0\\pard\n')
        for line in infile:
          for char in line.decode("utf-8") :
            if ord(char) > 127 :
              # FIXME: This is incorrect, because it only works for latin1.
              # The correct way is \u<decimal utf16 point>? , but this is more
              # work
              outfile.write("\\'%X" % ord(char)) 
            else :
              outfile.write(char)
          outfile.write('\\par ')
        outfile.write('}')
        outfile.close()
        infile.close()
      env.Command(["Swift/COPYING.rtf"], ["COPYING"], convertToRTF)

      wixvariables = {
        'VCCRTFile': env["vcredist"],
        'Version': str(myenv["SWIFT_VERSION_MAJOR"]) + "." + str(myenv["SWIFT_VERSION_MINOR"]) + "." + str(myenv["SWIFT_VERSION_PATCH"])	
      }
      wixincludecontent = "<Include>"
      for key in wixvariables:
        wixincludecontent += "<?define %s = \"%s\" ?>" % (key, wixvariables[key])
      wixincludecontent += "</Include>"
      myenv.WriteVal("..\\Packaging\\Wix\\variables.wxs", env.Value(wixincludecontent))
      myenv.WiX_Heat('..\\Packaging\\WiX\\gen_files.wxs', windowsBundleFiles)
      myenv.WiX_Candle('..\\Packaging\\WiX\\Swift.wixobj', '..\\Packaging\\WiX\\Swift.wxs')
      myenv.WiX_Candle('..\\Packaging\\WiX\\gen_files.wixobj', '..\\Packaging\\WiX\\gen_files.wxs')
      myenv.WiX_Light('#/Packages/Swift/Swift-' + myenv["SWIFT_VERSION"] + '.msi', ['..\\Packaging\\WiX\\gen_files.wixobj','..\\Packaging\\WiX\\Swift.wixobj'])
        
      if myenv["debug"] :
            myenv.InstallAs('#/Packages/Swift/Swift-' + myenv["SWIFT_VERSION"] + '.pdb', "Swift.pdb")


