# --- OVERVIEW ---
#
# This script takes an Awasu template file and replaces each {%INCLUDE%} directive
# with the contents of their include file.
#
# This is useful if you want to modify one of the existing templates. You could
# tweak the files directly but:
#   (1) Some of the files are shared between templates so changes you make
#       will be reflected in all the templates.
#   (2) Your changes will be lost when you install a new version of Awasu.
# 
# Running this script merges everything together into one big file that
# you can save somewhere and modify freely without having to worry about 
# the issues described above.

# --- USAGE ---
#
# This script is written in Python, so you need to have this installed.
# If you don't already have it, you can get it at www.python.org
#
# Then open a command prompt and run it like this:
#   ProcessTemplateIncludes.py TEMPLATE-FILE >SAVE-FILE
# where TEMPLATE-FILE is the template you want to process and SAVE-FILE is 
# where you want to save the results.

# --- COPYRIGHT ---
#
# (c) Awasu Pty. Ltd. 2003-11 (all rights reserved).
# Unauthorized use of this code is prohibited.

# --- LICENSE ---
#
# This software is provided 'as-is', without any express or implied warranty.
#
# In no event will the author be held liable for any damages arising from the use
# of this software.
#
# Permission is granted to anyone to use this software for any non-commercial purpose
# and to alter it and redistribute it freely, subject to the following restrictions:
#
#   (*) The origin of this software must not be misrepresented; you must not
#       claim that you wrote the original software. If you use this software,
#       an acknowledgment is requested but not required.
#
#   (*) Altered source versions must be plainly marked as such, and must not
#       be misrepresented as being the original software. Altered source is
#       encouraged to be submitted back to the original author so it can
#       be shared with the community. Please share your changes.
#
#   (*) This notice may not be removed or altered from any source distribution.

# --- GLOBAL DEFINITIONS ----------------------------------------------

import sys
import os
import re

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

gBaseDir = ""

# ---------------------------------------------------------------------

def processTemplateFile( templateFilename ) :

    # read the template file 
    fp = open( templateFilename , "r" )
    buf = fp.read()

    # replace {%INCLUDE%} directives with the contents of their include file 
    global gBaseDir
    prevBaseDir = gBaseDir
    gBaseDir = os.path.split( templateFilename )[ 0 ]
    buf = re.sub( "{%INCLUDE%\s+(.+?)}" , insertIncludeFile , buf )
    gBaseDir = prevBaseDir

    return buf

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

def insertIncludeFile( mo ) :

    # return the contents of the include file 
    includeFilename = os.path.join( gBaseDir , mo.group(1) )
    pos = includeFilename.find( "isOptional" )
    if pos >= 0 :
        # the include file is optional - check if it exists 
        includeFilename = includeFilename[ : pos ]
        if not os.path.isfile( includeFilename ) :
            return ""
    return processTemplateFile( includeFilename )

# --- MAIN ------------------------------------------------------------

# get the template file to process 
if len( sys.argv ) != 2 :
    print "Usage:" , os.path.split(sys.argv[0])[1] , "<template-file>"
    sys.exit( 1 )
templateFilename = sys.argv[ 1 ]

# process the template file 
buf = processTemplateFile( templateFilename )
print buf

