# Path: kano_profile/apps.py
# def launch_project(app, filename, data_dir, background=False):
#     # This is necessary to support the new official names
#     # TODO: once the apps have been renamed this will not be necessary
#     name_translation = {
#         'make-art': 'kano-draw',
#         'terminal-quest': 'linux-story'
#     }
# 
#     app_tr = name_translation.get(app, app)
# 
#     logger.info("launch_project: {} {} {}".format(app_tr, filename, data_dir))
# 
#     app_profiles = read_json(app_profiles_file)
# 
#     # XML file with complete pathname
#     fullpath = os.path.join(data_dir, filename)
# 
#     # Prepare the command line to open the app with the new project
#     try:
#         cmd = (app_profiles[app_tr]['cmd']
#                .format(fullpath=fullpath, filename=filename))
#     except KeyError as exc:
#         logger.warn(
#             "Can't find app '{}' in the app profiles - [{}]"
#             .format(app_tr, exc)
#         )
#         raise ValueError(_("App '{}' not available").format(app_tr))
# 
#     # Try to load the project if the app is already running, via a signal.
#     _, _, rc = run_cmd('/usr/bin/kano-signal launch-share {}'.format(fullpath))
#     if rc:
#         # Likely the app is not running and the signal could not be sent, so start it now
#         logger.warn("Error sending launch signal, starting the app now, rc={}".format(rc))
#         if background:
#             # TODO: After migrating to launching apps via systemd, shares for make-snake
#             # stopped launching from KW. Created a special case here to avoid
#             # fixing the make-snake cmd through systemd temporarily. FIX THIS.
#             if app == 'make-snake':
#                 run_bg(cmd)
#             else:
#                 run_cmd('systemd-run --user {cmd}'.format(cmd=cmd))
#         else:
#             _, _, rc = run_print_output_error(cmd)
#             return rc
#     else:
#         logger.info("Sent signal to app: {} to open : {}".format(app_tr, fullpath))
# 
#     return 0
# 
# def check_installed(app):
#     """ Check if an app is installed and if not install it through kano apps.
#     *Note* The translation between app and the corresponding kano-apps name is
#     not straight forward. This function uses a mapping between those two.
#     :param app: App name
#     :type app: str
#     :returns: True iff the app is installed and available
#     :rtype: bool
#     """
#     from kano.utils.misc import is_installed
#     from kano.utils.shell import run_cmd
#     cmd_template = 'kano-apps install {app_name}'
#     debpkg_to_kano_appstore = {
#         'make-light': 'powerup'
#     }
#     # Check if is already installed
#     if is_installed(app):
#         return True
# 
#     # Check if we know how to install it
#     if app not in debpkg_to_kano_appstore:
#         logger.error(
#             "Do not know how to translate app '{}' to kano-apps"
#             .format(app)
#         )
#         return False
# 
#     # Install it using kano apps
#     logger.info(
#         "'{}' not installed will attempt to install it as '{}' using kano apps"
#         .format(app, debpkg_to_kano_appstore[app])
#     )
#     cmd = cmd_template.format(app_name=debpkg_to_kano_appstore[app])
#     run_cmd(cmd)
# 
#     # Check whether it has been installed
#     if not is_installed(app):
#         # Something probably went wrong here
#         logger.error(
#             "Attempted to install app '{}' but something went wrong"
#             .format(app)
#         )
#         return False
# 
#     return True


from kano.logging import logger
from kano_profile.apps import launch_project, check_installed
# launch.py
#
# Copyright (C) 2015-2017 Kano Computing Ltd.
# License: http://www.gnu.org/licenses/gpl-2.0.txt GNU GPL v2
#

# args is an array of arguments that were passed through the URL
# e.g., kano:launch:app-name, it will be ["app-name"]



def run(args):
    try:
        ret = args[0]
    except (TypeError, IndexError) as exc:
        logger.error(
            "Can't get first element of {} - [{}]".format(str(args), exc)
        )
        ret = None
    return ret


def launch(app_name, background=False):
    if app_name is not None:
        try:

# Path: kano_profile/apps.py
# def launch_project(app, filename, data_dir, background=False):
#     # This is necessary to support the new official names
#     # TODO: once the apps have been renamed this will not be necessary
#     name_translation = {
#         'make-art': 'kano-draw',
#         'terminal-quest': 'linux-story'
#     }
# 
#     app_tr = name_translation.get(app, app)
# 
#     logger.info("launch_project: {} {} {}".format(app_tr, filename, data_dir))
# 
#     app_profiles = read_json(app_profiles_file)
# 
#     # XML file with complete pathname
#     fullpath = os.path.join(data_dir, filename)
# 
#     # Prepare the command line to open the app with the new project
#     try:
#         cmd = (app_profiles[app_tr]['cmd']
#                .format(fullpath=fullpath, filename=filename))
#     except KeyError as exc:
#         logger.warn(
#             "Can't find app '{}' in the app profiles - [{}]"
#             .format(app_tr, exc)
#         )
#         raise ValueError(_("App '{}' not available").format(app_tr))
# 
#     # Try to load the project if the app is already running, via a signal.
#     _, _, rc = run_cmd('/usr/bin/kano-signal launch-share {}'.format(fullpath))
#     if rc:
#         # Likely the app is not running and the signal could not be sent, so start it now
#         logger.warn("Error sending launch signal, starting the app now, rc={}".format(rc))
#         if background:
#             # TODO: After migrating to launching apps via systemd, shares for make-snake
#             # stopped launching from KW. Created a special case here to avoid
#             # fixing the make-snake cmd through systemd temporarily. FIX THIS.
#             if app == 'make-snake':
#                 run_bg(cmd)
#             else:
#                 run_cmd('systemd-run --user {cmd}'.format(cmd=cmd))
#         else:
#             _, _, rc = run_print_output_error(cmd)
#             return rc
#     else:
#         logger.info("Sent signal to app: {} to open : {}".format(app_tr, fullpath))
# 
#     return 0
# 
# def check_installed(app):
#     """ Check if an app is installed and if not install it through kano apps.
#     *Note* The translation between app and the corresponding kano-apps name is
#     not straight forward. This function uses a mapping between those two.
#     :param app: App name
#     :type app: str
#     :returns: True iff the app is installed and available
#     :rtype: bool
#     """
#     from kano.utils.misc import is_installed
#     from kano.utils.shell import run_cmd
#     cmd_template = 'kano-apps install {app_name}'
#     debpkg_to_kano_appstore = {
#         'make-light': 'powerup'
#     }
#     # Check if is already installed
#     if is_installed(app):
#         return True
# 
#     # Check if we know how to install it
#     if app not in debpkg_to_kano_appstore:
#         logger.error(
#             "Do not know how to translate app '{}' to kano-apps"
#             .format(app)
#         )
#         return False
# 
#     # Install it using kano apps
#     logger.info(
#         "'{}' not installed will attempt to install it as '{}' using kano apps"
#         .format(app, debpkg_to_kano_appstore[app])
#     )
#     cmd = cmd_template.format(app_name=debpkg_to_kano_appstore[app])
#     run_cmd(cmd)
# 
#     # Check whether it has been installed
#     if not is_installed(app):
#         # Something probably went wrong here
#         logger.error(
#             "Attempted to install app '{}' but something went wrong"
#             .format(app)
#         )
#         return False
# 
#     return True


# Path: kano_world/hooks/launch.py
from kano.logging import logger
from kano_profile.apps import launch_project, check_installed

# launch.py
#
# Copyright (C) 2015-2017 Kano Computing Ltd.
# License: http://www.gnu.org/licenses/gpl-2.0.txt GNU GPL v2
#

# args is an array of arguments that were passed through the URL
# e.g., kano:launch:app-name, it will be ["app-name"]



def run(args):
    try:
        ret = args[0]
    except (TypeError, IndexError) as exc:
        logger.error(
            "Can't get first element of {} - [{}]".format(str(args), exc)
        )
        ret = None
    return ret


def launch(app_name, background=False):
    if app_name is not None:
        try:

            if check_installed(app_name):