#!/usr/bin/env /usr/bin/ecell3-python
#
# dmgen --  a program to docify dynamic module in 
#           E-CELL Simulation Environment Version 3
#
# authors:  Tomoya Kitayama <tomo@e-cell.org>
#           Satya Arjunan <satya@ttck.keio.ac.jp>

import os
import sys
import getopt
import string

PACKAGE='dmtool'
VERSION='0.1'

prefix=os.environ['ECELL3_PREFIX']
exec_prefix=prefix
pythondir=os.environ['PYTHONDIR']
EMPYBIN=pythondir + os.sep + 'ecell' + os.sep + 'em.py'
EMPYOPTIONS="--flatten --raw-error"

OUTSUFFIX='.cpp'
VERBOSE = False

def help():
    ext = os.path.basename( sys.argv[0] )
    aProgramName, ext = os.path.splitext( ext )
    print '''
%s -- Docify dynamic modules for E-Cell Simulation Environment Version 3

Usage:
       %s <classname> [options] files
       %s [-h|--help]

Options:
       -d|--dmfilename file     Specify dmfile (.dm) 
       -t|--template file       Specify template (.em) 
       -p|--py file             Specify python script file (.py)
       -v|--verbose		Be verbose
       -h|--help		Print this message
    '''% ( aProgramName, aProgramName, aProgramName )
    
def msg( outstr ):
    print "E-Cell3 dmgen: " + outstr
    
def execute( cmdstr ):
    if VERBOSE:
	print cmdstr
    return os.system( cmdstr )

def normpath( path ):
    path = string.replace(path, '\\', '/')
    return path
    

def main():
    # -------------------------------------
    # get options and arguments
    # -------------------------------------
    try:
	opts , args = getopt.getopt( sys.argv[2:] , "hvd:t:p:", ["help", \
		"verbose", "dmfilename ", "template ", "py "])
    except:
	help()
	sys.exit(1)

    global VERBOSE
    HAVE_TEMPLATE = False
    HAVE_PY = False
    dmFile = ''
    emFile = ''
    pyFile = ''
    
    # -------------------------------------
    # check argument
    # -------------------------------------
    
    for anOption, anArg in opts:

	# print help message
	if anOption in ( "-h", '--help' ):
	    help()
	    sys.exit(0)
	    
	# be verbose
	if anOption in ( "-v", '--verbose'):
	    VERBOSE = True            
	    
	if anOption in ( "-d", '--dmfilename'):
	    dmFile = anArg
	    
	if anOption in ( "-t", '--template' ):
	    HAVE_TEMPLATE = True
	    emFile = anArg   
	    
	if anOption in ( "-p", '--py'):
	    HAVE_PY = True
	    pyFile = anArg
	    
    # check if input file is given
    if len( args ) < 1:
	help()
	msg( "Error: not enough arguments" )
	sys.exit(1)
	
    if not HAVE_TEMPLATE:
	help()
	msg( "Error: Template file was not specified" )
	sys.exit(1)

    if not HAVE_PY:
	help()
	msg( "Error: PY file was not specified" )
	sys.exit(1)

    basedmFile = os.path.basename(dmFile)
    name, ext = os.path.splitext(basedmFile)
    outFile = name + OUTSUFFIX
    pyFile = normpath(pyFile)
    dmFile = normpath(dmFile)
    
    cmdstr = 'python \"' + EMPYBIN + '\" ' + EMPYOPTIONS + ' -F \"' + pyFile + \
	    '\" -F \"' + dmFile + '\" -o \"' + outFile + '\" \"' + emFile + '\"'
    if execute( cmdstr ):
	msg ( "Error: failed to create " + outFile )
	sys.exit(1)
    
    msg( 'generated ' + outFile )
    
if __name__ == '__main__':
    main()

