#!/usr/bin/env /usr/bin/ecell3-python

# Eml to Sbml converter

import sys
import os
import types
import getopt
import time
import sets

import ecell.ecs
import ecell.config
from ecell.eml import *
from ecell.convertEML2SBML import *

# --------
#  Main
# --------

if __name__ == '__main__':


    def usage():
        aProgramName = os.path.basename( sys.argv[0] )

        print '''
%(appname)s -- convert a eml file to a SBML file
        
Usage:
    %(appname)s [-h] [-f] [-o SBMLFILE] infile
        
Options:
    -h or --help             : Print this message.
    -f or --force            : Forcefully overwrite the output file
                               even if it already exists.
    -o or --outfile=SBMLFILE : Specify the output file name. '-' means stdout.
        
''' % { 'appname': aProgramName }

    # commandline processing
    try:
        opts, args = getopt.getopt(
            sys.argv[ 1: ], "hfo:",
            [ "help", "force", "outfile=" ] )
    except getopt.GetoptError:
        usage()
        sys.exit( -1 )

    aSbmlFileName = None
    aForceFlag = 0

    for anOption, anArg in opts:
        if anOption in ( "-h", '--help' ):
            usage()
            sys.exit( 0 )

        if anOption in ( "-f", '--force' ):
            aForceFlag = 1            

        if anOption in ( "-o", "--outfile" ):
            aSbmlFileName = anArg
            
    if len( args ) == 0:
        sys.stderr.write( "No input file.\n" )
        sys.exit( -1 )

    anEmlFileName = args[0]

    anEmlFile = open( anEmlFileName )
    anEml = Eml( anEmlFile )
    anEmlFile.close()
    
    aBaseName = os.path.basename( anEmlFileName )
    aBaseName, anExt = os.path.splitext( aBaseName )

    if aSbmlFileName == None:
        if anExt == '.eml' or anExt == '.xml':
            anExt = '.xml'
        else:
            anExt += '.xml'
        aSbmlFileName = aBaseName + anExt

    if aSbmlFileName == '-':
        aSbmlFile = sys.stdout
    else:
        if os.path.isfile( aSbmlFileName ) and aForceFlag == 0:
            sys.stderr.write( "Output file %s exists. Use -f to overwrite.\n"
                              % aSbmlFileName )
            sys.exit( 1 )
        aSbmlFile = open( aSbmlFileName, 'w' )

    aSBMLLevel = raw_input( "SBML Model Level? ( 1 or 2 ) : " )

    if aSBMLLevel == '1' :
        aSBMLVersion = raw_input( "SBML Model Version? ( 1 or 2 ) : " )
        if( aSBMLVersion != '1' and aSBMLVersion != '2'):            
            raise IOError," Such SBML Version is not defined"
    elif aSBMLLevel == '2':
        aSBMLVersion = 1
    else:
        raise IOError," Such SBML Level is not defined"


    # ------------------------------
    #       eml2sbml main  
    # ------------------------------

    aSbmlString = convertToSBMLModel(
        anEml, aBaseName, aSBMLLevel, aSBMLVersion )
    
    if aSbmlString != '':
        aSbmlFile.write( aSbmlString )
    else:
        raise IOError," failed to write the SBML file(.xml)"
    
    aSbmlFile.close()
