#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2012-2018 SoftBank Robotics. All rights reserved.
# Use of this source code is governed by a BSD-style license (see the COPYING file).
""" Convert a binary archive into a qiBuild package. """
from __future__ import absolute_import
from __future__ import unicode_literals
from __future__ import print_function

import qisys
import qisys.parsers
from qisys import ui
from qitoolchain.convert import convert_package, convert_from_conan, conan_json_exists
from qitoolchain.conan import Conan


def configure_parser(parser):
    """Configure parser for this action """
    qisys.parsers.default_parser(parser)
    parser.add_argument("--name", required=True, help="The name of the package")
    parser.add_argument("--version", help="The name of the package")
    parser.add_argument("package_path", metavar='PACKAGE_PATH',
                        help="The path to the archive or conan directory to be converted")
    parser.add_argument("--batch", dest="interactive", action="store_false",
                        help="Do not prompt for cmake module edition")
    parser.add_argument("--conan", action="store_true",
                        help="Define if we work on a conan package")
    parser.add_argument("--conan-shared", dest="shared", action="store_true",
                        help="Set to get the shared version of the conan library")
    parser.add_argument("--conan-static", dest="shared", action="store_false",
                        help="Set to get the static version of the conan library")
    parser.add_argument("--conan-channel", dest='channel',
                        help="The channel of the conan package to be converted")
    parser.set_defaults(interactive=True, version="0.0.1", shared=None)


def do(args):
    """ Convert a binary archive into a qiBuild package. """
    name = args.name
    interactive = args.interactive
    package_path = args.package_path
    if args.conan:
        conan = Conan(args.name, args.version, args.channel, args.shared)
        if not conan_json_exists(package_path):
            package_path = conan.create()
        ui.info("Converting Conan package", package_path, "into a qiBuild package")
        res = convert_from_conan(package_path, name, args.version)
    else:
        ui.info("Converting", package_path, "into a qiBuild package")
        res = convert_package(package_path, name, interactive=interactive)
    message = """Conversion succeeded.\n\nqiBuild package:\n  {0}\n
You can add this qiBuild package to a toolchain using:
  qitoolchain add-package -c <config name> {0}
  or
  qitoolchain add-package -t <toolchain name> {0}""".format(res)

    qisys.ui.info(message)
    return res

@@ -1440,37 +1440,37 @@
 static%22, dest=%22s
-hared
+tatic
 %22, action=%22store
@@ -1462,36 +1462,35 @@
 , action=%22store_
-fals
+tru
 e%22,%0A            
@@ -1761,21 +1761,8 @@
 0.1%22
-, shared=None
 )%0A%0A%0A
@@ -1948,16 +1948,326 @@
 .conan:%0A
+        shared = None%0A        if args.shared or args.static:%0A            assert args.shared != args.static, %22--conan-shared and --conan-static are mutualy exlusive, please remove one of them.%22%0A        if args.shared is True:%0A            shared = True%0A        if args.static is True:%0A            shared = False%0A
         
@@ -2319,21 +2319,16 @@
 hannel, 
-args.
 shared)%0A

fix static and shared flags