#!/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

from libertine import launcher

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("No container with id '%s'" % config.container_id)
            sys.exit(1)

        from libertine import LibertineContainer
        container = LibertineContainer(container_id=config.container_id)
    else:
        from libertine import NoContainer

        container = NoContainer()

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

if __name__ == '__main__':
    main()
