#!/usr/bin/python3
# -*- coding: utf-8 -*-

# Copyright (C) 2015-2016 Canonical Ltd.
# Author: Christopher Townsend <christopher.townsend@canonical.com>

# This program 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; version 3 of the License.
#
# 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/>.

import os
import sys

import gettext
gettext.textdomain('libertine')
_ = gettext.gettext

from libertine import launcher, utils

def main():
    config = launcher.Config()

    if config.container_id:
        from libertine import ContainersConfig, utils
        if not ContainersConfig.ContainersConfig().container_exists(config.container_id):
            utils.get_logger().error(utils._("No container with id '{container_id}'").format(container_id=config.container_id))
            sys.exit(1)

        try:
            from libertine import LibertineContainer
            container = LibertineContainer(container_id=config.container_id)
        except ImportError as e:
            container_type = self.containers_config.get_container_type(container_id)
            utils.get_logger().error(utils._("Backend for container '{id}' not installed. Install "
                                             "'python3-libertine-{type}' and try again.").format(id=config.container_id, type=container_type))
            sys.exit(1)
    else:
        from libertine import NoContainer

        container = NoContainer()

    with launcher.Session(config, container) as session:
        if session.start_application():
            session.run()

if __name__ == '__main__':
    main()
