# vi: syntax=python:et:ts=4
#
# SCons build description for the Wesnoth UMC Development IDE
#
# Prerequisites are:
# 1. Sun/Oracle Java JDK
# 2. Eclipse 3.7 environment with all required plugins
# (Can be found at: http://sourceforge.net/projects/wesnoth/files/wesnoth-umcplugin/build_utils/ )

import os, shutil, re, subprocess

# Warn user of current set of build options.
if os.path.exists('.scons-option-cache'):
    optfile = file('.scons-option-cache')
    print "Saved options:", optfile.read().replace("\n", ", ")[:-2]
    optfile.close()

# variables
opts = Variables( ".scons-option-cache" )
opts.AddVariables(
    PathVariable( "eclipsedir", "The directory that contains the eclipse binary and all the needed plugins",
                "", PathVariable.PathIsDir ),
    PathVariable( "updatesdir", "The directory that should contain the updates for the plugin",
                os.getcwd() , PathVariable.PathAccept ),
    PathVariable( "binariesdir", "The directory that should contain the binaries for the plugin",
                os.getcwd() , PathVariable.PathAccept )
)
env = Environment( options = opts )
opts.Save( '.scons-option-cache', env )

eclipse_dir = env[ "eclipsedir" ]
updates_dir = env[ "updatesdir" ]
binaries_dir = env[ "binariesdir" ]

temp_build_dir = os.environ[ "TMP" ] + "/umcdev_build"

print "Clearing temporary build dir ( " + temp_build_dir + " ) ..."
if os.path.exists( temp_build_dir ):
    shutil.rmtree( temp_build_dir )

os.makedirs( temp_build_dir )

# now, we need to find 2 things:
# 1) file: org.eclipse.equinox.launcher_*.jar
# 2) dir: org.eclipse.pde.build_*

equinox_launcher_path = ""
pde_build_dir = ""

for file in os.listdir( eclipse_dir + "/plugins" ):
    if re.match( "org.eclipse.equinox.launcher_.*.jar", file ):
        equinox_launcher_path = eclipse_dir + "/plugins/" + file
    elif re.match( "org.eclipse.pde.build_.*", file ):
        pde_build_dir = eclipse_dir + "/plugins/" + file

if equinox_launcher_path:
    print "Found equinox launcher: " + equinox_launcher_path
else:
    print "Couldn't find equinox launcher. Aborting..."
    Return( )

if pde_build_dir:
    print "Found PDE Build dir: " + pde_build_dir
else:
    print "Couldn't find PDE Build dir. Aborting..."
    Return( )

print "Building..."
subprocess.call( [
    "java",
    "-cp", equinox_launcher_path, "org.eclipse.core.launcher.Main",
    "-data", "workspace",
    "-application", "org.eclipse.ant.core.antRunner",
    "-DbuildDirectory=" + temp_build_dir,
    "-Dbase=" + eclipse_dir,
    "-DbaseLocation=" + eclipse_dir,
    "-DupdatesDir=" + updates_dir,
    "-DbinariesDir=" + binaries_dir,
    "-Ddeltapack=" + eclipse_dir,
    "-Declipse.pdebuild.scripts=" + pde_build_dir + "/scripts",
    "-Declipse.pdebuild.templates=" + pde_build_dir + "/templates",
    "-buildfile", "build.xml" ] )

# Some cleanup
print "Cleaning up..."
# Some cleanup
to_cleanup = [ 
	"../org.wesnoth.feature/build.xml",  
    "../org.wesnoth.dependencies.feature/build.xml",  
	"../org.wesnoth/build.xml",
	"../org.wesnoth.ui/build.xml",
	"../org.wesnoth/javaCompiler...args",
	"../org.wesnoth.ui/javaCompiler...args" ]

for cleanup_file in to_cleanup:
	try: os.remove( cleanup_file )
	except: pass

try: shutil.rmtree( "../org.wesnoth.feature/feature.temp.folder" )
except: pass

try: shutil.rmtree( "../org.wesnoth.dependencies.feature/feature.temp.folder" )
except: pass
    
# Local variables:
# mode: python
# end:
