#!/usr/bin/python

# Software License Agreement (BSD License)
#
# Copyright (c) 2008, Thibault Kruse
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above
#    copyright notice, this list of conditions and the following
#    disclaimer in the documentation and/or other materials provided
#    with the distribution.
#  * Neither the name of Willow Garage, Inc. nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

"""roslint is an extention to unilint for the ROS ecosystem
roslint defines more loctions for scripts to be at, and if you call
it with a ROS packagename or stackname instead of a folder,
it attempts to resolve that.
For the time being, it is shipped with unilint"""

import sys
from unilint.unilint_main import evaluate_options, get_unilint_parser, run_cmd, register_plugin
from unilint.common import UnilintException

from roslint.ros_resource_plugin import RosResourcePlugin, ROS_SOURCE

register_plugin(RosResourcePlugin)

import roslib
roslib.load_manifest('rospack')

import rospkg
from rospkg.common import ResourceNotFound


def roslint_main():
    """
    script main
    """
    try:
        prog = sys.argv[0]
        args = sys.argv[1:]

        parser = get_unilint_parser("usage: %s [OPTIONS] [PATH or STACK or PACKAGE]"%prog)
        (options, args) = parser.parse_args(args)

        options, args, path = evaluate_options(options, args)

        if path is None:
            rst = rospkg.RosStack()
            try:
                ros_path = rst.get_path(args[0])
            except ResourceNotFound:
                ros_path = None
            if ros_path is None:
                rpa = rospkg.RosPack()
                try:
                    ros_path = rpa.get_path(args[0])
                except ResourceNotFound:
                    ros_path = None
            if ros_path is None:
                raise UnilintException('No ROS stack/package named %s, and no such file, directory: %s'%(args[0], path))
            path = ros_path
        if options.selected_plugins is not None:
            if not ROS_SOURCE in options.selected_plugins:
                options.selected_plugins += ',%s'%ROS_SOURCE

        sys.exit(run_cmd(path, options, args))
    except UnilintException as exc:
        print(str(exc))
        sys.exit(1)

if __name__ == "__main__":
    roslint_main()
