#!/usr/bin/env python

import sys
import os
import string

def error():
    sys.stderr.write("PaCOIdlTool: Error in arguments : Use PaCOIdlTool -h for help\n\n")
    sys.exit(1)

def usage():
    print "\nPaCOIdlTool: Usage: PaCOIdlTool interface_file xml_file orb debug optional modes\n"
    sys.exit(1)
    
def main():

    arguments = sys.argv
    if len(arguments) == 1:
        error()
    if arguments[1] == '-h':
        usage()
    if len(arguments) < 5:
        error()
    else :
        try:
            file = open(arguments[1], 'r')
            file.close()
        except IOError:
            sys.stderr.write("\nPaCOIdlTool: Error unable to open : " +  arguments[1] + "\n")
            error()
        try:
            file = open(arguments[2], 'r')
            file.close()
        except IOError:
            sys.stderr.write("\nPaCOIdlTool: Error unable to open : " +  arguments[2] + "\n")
            error()

        # Les deux fichiers sont bon
        # On commence par creer le nouveau fichier Idl
        command = "java -classpath /usr/share/java/PaCO.jar:$CLASSPATH -DPaCOPATH=$PACOPATH PaCOIdl2 " + arguments[1] + " " + arguments[2] + " " + arguments[3] + " " + arguments[4]
        os.system(command)
        # analyse du nom du premier fichier pour trouver le nouveau nom
        index = string.find(arguments[1],".idl")
        name = arguments[1][:index] + "PaCO.idl"
        command = "python $PACOPATH/IDL2Tool/Python/Src/PaCO++StubGenerator.py " +  name + " configuration_file " + arguments[4]
        if len(arguments) >= 5:
            for compteur in range(5,len(arguments)):
                command = command + " " + arguments[compteur]
        os.system(command)

main()

