#!/usr/bin/env python

# usage: dcompile cfile [[cflags]... ]

import sys
import os
import glob
import string
import getopt

# prefix='/usr'
# exec_prefix='/usr'
# libdir='/usr/lib'
ORIG_CXXFLAGS = '-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fsigned-char -mcpu=750 -mtune=7450'
ORIG_CPPFLAGS = ''
ORIG_LDFLAGS = ''
SHLEXT = '.so'
CXX = 'ppc-mandriva-linux-gnu-g++'
CXXSUFFIX = '.cpp'
CXXFLAGS = os.environ.get('CXXFLAGS','')
LDFLAGS =  os.environ.get('LDFLAGS','')
VERBOSE = False

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

Usage:
       %s [-v|--verbose] <source.cpp> [compile options]
       %s [-h|--help]
    '''% ( aProgramName, aProgramName, aProgramName )

def msg( outstr ):
    print "E-Cell3 dmcompile: " + outstr

def execute( cmdstr ):
    if VERBOSE:
	print cmdstr
    return os.system( cmdstr )

def compile( CXX, CXXCOMPILEFLAGS, SRC ):
    return execute( CXX + ' ' + CXXCOMPILEFLAGS + ' -fPIC -c ' + SRC )

def link( CXX, CPPFLAGS, OBJ, SOBJ, LDFLAGS ):
	cflags = CPPFLAGS
	if sys.platform == 'darwin':
			cflags = cflags + ' -dynamiclib -install_name ' + SOBJ
	else:
			cflags = cflags + ' -shared -Wl,-soname=\"' + SOBJ + '\" ' 

	return execute( CXX + ' ' + cflags + ' ' + OBJ + ' -o ' + SOBJ + ' ' + LDFLAGS)

def cleanup( OBJ ):
    if os.path.isfile( OBJ ):
	os.remove( OBJ )
    
def main():
    opts , args = getopt.getopt( sys.argv[1:], "hv", ["help", "verbose"])   
    global VERBOSE
    
    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

    # check if source file is given
    if len( args ) < 1:
	help()
	msg( "Error: source file was not given." )
	sys.exit(1)

    # check if source file is valid
    if len( glob.glob ( args[0].replace( '\"', '' ) ) ) < 1:
	msg( "Error: source file "+ args[0] + " was not found." )
	sys.exit(1)
		
    SRC = args[0]
    ext = os.path.basename(SRC)
    CLASSNAME, ext = os.path.splitext( ext.replace( '\"', '' ) )
    OBJ = CLASSNAME + '.o'
    SOBJ = CLASSNAME + SHLEXT
    ARGS = ' \"' + string.join( args[1:], '\" \"' ) + '\"'

	# compiler: use env + autoconf + plus anything that was given on the cmd line (?) -- FIXME
    CXXCOMPILEFLAGS = CXXFLAGS + ' ' + ORIG_CXXFLAGS + ' ' + ORIG_CPPFLAGS + ' ' + ARGS

    # linker: use env + autoconf (?) -- FIXME
    LINKFLAGS = LDFLAGS + ' ' + ORIG_LDFLAGS
    
    if compile( CXX, CXXCOMPILEFLAGS, "\"" + SRC + "\"" ) or not os.path.isfile( OBJ ):
	msg( "Error: The compiler failed to create " + OBJ )
	sys.exit(1)
	
    if link( CXX, CXXCOMPILEFLAGS, '\"' + OBJ + '\"' , '\"' + SOBJ + '\"', LINKFLAGS ) or not os.path.isfile( SOBJ ):
	msg( "Error: The compiler failed to create " + SOBJ )
	sys.exit(1)
    
    cleanup( OBJ )

if __name__ == '__main__':
	main()

