#!@PYTHON@
# -*- python -*-
#                           Package   : omniidl
# omniidl.in                Created on: 1999/10/29
#			    Author    : Duncan Grisby (dpg1)
#
#    Copyright (C) 2014 Apasphere Ltd
#    Copyright (C) 1999 AT&T Laboratories Cambridge
#
#  This file is part of omniidl.
#
#  omniidl is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see http://www.gnu.org/licenses/
#
# Description:
#   
#   IDL compiler command

import sys

if sys.hexversion < 0x10502f0:
    sys.stderr.write("\n\n")
    sys.stderr.write("omniidl: WARNING!!\n\n")
    sys.stderr.write("omniidl: Python version 1.5.2 or later is required.\n")
    sys.stderr.write("omniidl: " + sys.executable + " is version " + \
                     sys.version + "\n")
    sys.stderr.write("omniidl: Execution is likely to fail.\n")
    sys.stderr.write("\n\n\n")
    sys.stderr.flush()

import os, os.path

pylibdir   = archlibdir = None
binarchdir = os.path.abspath(os.path.dirname(sys.argv[0]))

# Try a path based on the installation prefix
sppath = "@prefix@/lib/python" + sys.version[:3] + "/site-packages"

if os.path.isdir(sppath):
    sys.path.append(sppath)

# Paths in a traditional omni tree
if binarchdir != "":
    sys.path.insert(0, binarchdir)
    bindir, archname = os.path.split(binarchdir)
    treedir, bin     = os.path.split(bindir)
    if bin == "bin":
        pylibdir    = os.path.join(treedir, "lib", "python")
        vpylibdir   = pylibdir + sys.version[:3] + "/site-packages"
        vpylib64dir = (os.path.join(treedir, "lib64", "python") +
                       sys.version[:3] + "/site-packages")
        archlibdir  = os.path.join(treedir, "lib", archname)

        if os.path.isdir(pylibdir):
            sys.path.insert(0, pylibdir)

        if os.path.isdir(vpylib64dir):
            sys.path.insert(0, vpylib64dir)

        if os.path.isdir(vpylibdir):
            sys.path.insert(0, vpylibdir)

        if os.path.isdir(archlibdir):
            sys.path.insert(0, archlibdir)

    elif archname == "bin":
        pylibdir    = os.path.join(bindir, "lib", "python")
        vpylibdir   = pylibdir + sys.version[:3] + "/site-packages"
        vpylib64dir = (os.path.join(bindir, "lib64", "python") +
                       sys.version[:3] + "/site-packages")
        archlibdir  = os.path.join(bindir, "lib")

        if os.path.isdir(pylibdir):
            sys.path.insert(0, pylibdir)

        if os.path.isdir(vpylib64dir):
            sys.path.insert(0, vpylib64dir)

        if os.path.isdir(vpylibdir):
            sys.path.insert(0, vpylibdir)

        if os.path.isdir(archlibdir):
            sys.path.insert(0, archlibdir)

# Last chance, try a path based on the installation prefixes
sys.path.append("@prefix@/lib/python" + sys.version[:3] + "/site-packages")

paths = [ "@exec_prefix@/lib/python" + sys.version[:3] + "/site-packages",
          "@pythondir@",
          "@pyexecdir@" ]


# Autoconf insists on making our life difficult...
for path in paths:
    path = path.replace("${exec_prefix}", "@exec_prefix@")
    path = path.replace("${prefix}", "@prefix@")
    sys.path.append(path)

try:
    import _omniidl
except ImportError as msg:
    sys.stderr.write("\n\n")
    sys.stderr.write("omniidl: ERROR!\n\n")
    sys.stderr.write("omniidl: Could not open IDL compiler module " \
                     "_omniidlmodule.so\n")
    sys.stderr.write("omniidl: Please make sure it is in directory ")
    sys.stderr.write((archlibdir or binarchdir) + "\n")
    sys.stderr.write("omniidl: (or set the PYTHONPATH environment variable)\n")
    sys.stderr.write("\n")
    sys.stderr.write("omniidl: (The error was '" + str(msg) + "')\n")
    sys.stderr.write("\n\n")
    sys.stderr.flush()
    sys.exit(1)

try:
    import omniidl.main
except ImportError as msg:
    sys.stderr.write("\n\n")
    sys.stderr.write("omniidl: ERROR!\n\n")
    sys.stderr.write("omniidl: Could not open Python files for IDL compiler\n")
    sys.stderr.write("omniidl: Please put them in directory " + \
                     (pylibdir or binarchdir) + "\n")
    sys.stderr.write("omniidl: (or set the PYTHONPATH environment variable)\n")
    sys.stderr.write("\n")
    sys.stderr.write("omniidl: (The error was '" + str(msg) + "')\n")
    sys.stderr.write("\n\n")
    sys.stderr.flush()
    sys.exit(1)

omniidl.main.main()
