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

# Copyright (C) 2014 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 optparse
import lxc
import os
import getpass

from libertine import LibertineContainer, list_libertine_containers

if __name__ == '__main__':
    parser = optparse.OptionParser(description="Legacy X application support for Unity 8")
    parser.add_option(
        "--destroy", action="store", type="string",
        help=("Destroy any existing environment entirely."))
    parser.add_option(
        "--update-container", action="store", type="string",
        help=("Update the packages in the Legacy X Apps LXC."))
    parser.add_option(
        "--create", action="store", type="string",
        help=("Create a new Libertine container."))
    parser.add_option(
        "--container-type", action="store", type="string",
        help=("Type of Libertine container to create. lxc | chroot"))
    parser.add_option(
        "--list", action="store_true",
        help=("List all Libertine containers."))
    parser.add_option(
        "--install-package", action="store", type="string",
        help=("Install a package in the specified Libertine container."))
    parser.add_option(
        "--package-name", action="store", type="string",
        help=("The name of the package to install into the Libertine container."))

    (options, args) = parser.parse_args()

    if options.destroy:
        container = LibertineContainer(options.destroy, options.container_type)
        container.destroy_libertine_container()

    if options.update_container:
        container = LibertineContainer(options.update_container, options.container_type)
        container.update_libertine_container()

    if options.list:
        containers = list_libertine_containers()
        for container in containers:
            print("%s" % container)

    if options.create:
        password = None

        if options.container_type == "lxc":
            print("Your user password is required for creating a Libertine container.")
            password = getpass.getpass()
        container = LibertineContainer(options.create, options.container_type)
        container.create_libertine_container(password)

    if options.install_package and options.package_name:
        container = LibertineContainer(options.install_package, options.container_type)
        container.install_package(options.package_name)
