#!/usr/bin/env python
# -*- python -*-
# -*- coding: utf-8 -*-
#
#  @file rtm-skelwrapper
#  @brief CORBA skelton/stub wrapper generator
#  @date $Date: 2007-01-11 08:58:25 $
#  @author Noriaki Ando <n-ando@aist.go.jp>
# 
#  Copyright (C) 2004-2007
#      Task-intelligence Research Group,
#      Intelligent Systems Research Institute,
#      National Institute of
#          Advanced Industrial Science and Technology (AIST), Japan
#      All rights reserved.
# 
#  $Id: rtm-skelwrapper 775 2008-07-28 16:14:45Z n-ando $
# 

import os
import sys
import getopt

libdir_path = os.popen("rtm-config --libdir", "r").read().split("\n")
pyhelper_path = libdir_path[0] + "/py_helper"
sys.path.append(pyhelper_path)

import skel_wrapper

opt_args_fmt = ["help",
		"idl-file=",
		"include-dir=",
		"skel-suffix=",
		"stub-suffix="]


config_inc = """
#include <rtm/config_rtc.h>
#undef PACKAGE_BUGREPORT
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
"""


def usage():
	print """
Usage: rtc-skelwrapper [OPTIONS]

Options:

    [--help or -h]                        Print this help.
    [--idl-file[=IDL_file]]               IDL file name
    [--skel-suffix[=suffix]]              Suffix of server skelton files
    [--stub-suffix[=suffix]]              Suffix of client stub files
    [--include-dir[=dir] ot -I]           include prefix in #include
"""


def main():
	try:
		opts, args = getopt.getopt(sys.argv[1:], "f:I:h", opt_args_fmt)
	except getopt.GetoptError:
		print "Error: Invalid options.", getopt.GetoptError
		usage()
		sys.exit(-1)

	if not opts:
		usage()
		sys.exit(-1)

	include_dir = "rtm/idl/"
	skel_suffix = "Skel"
	stub_suffix = "Stub"
	idl_file    = None

	for opt, arg in opts:
		if opt in ("-h", "--help"):
			usage()

		if opt in ("-f", "--idl-file"):
			idl_file = arg

		if opt in ("-I", "--include-dir"):
			include_dir = arg

		if opt in ("--skel-suffix"):
			skel_suffix = arg

		if opt in ("--stub-suffix"):
			stub_suffix = arg

	if not idl_file:
		print "Error: IDL file is not given."
		usage()
		sys.exit(-1)


	import skel_wrapper

	s = skel_wrapper.skel_wrapper(idl_file, skel_suffix, stub_suffix,
				      include_dir, config_inc)
	s.generate()


if __name__ == "__main__":
	main()

		
