#!/usr/bin/python
# This file is part of Checkbox.
#
# Copyright 2014 Canonical Ltd.
# Written by:
#   Daniel Manrique <roadmr@ubuntu.com>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox 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 Checkbox.  If not, see <http://www.gnu.org/licenses/>.
"""
manage_compiz_plugin
====================

This script allows enabling or disabling compiz plugins using
python-compizconfig as a backend. Changes take effect on the fly.

The script depends on:
    python-compizconfig

And unfortunately only works on Python 2.x.
"""

from gettext import gettext as _
import argparse
import gettext
import os
import sys
import time

gettext.textdomain("2013.com.canonical.certification.checkbox")
gettext.bindtextdomain("2013.com.canonical.certification.checkbox",
                       os.getenv("CHECKBOX_PROVIDER_LOCALE_DIR", None))


try:
    import compizconfig
except ImportError:
    raise SystemExit(_("Can't import compizconfig: {}").format(sys.exc_info()[1]))


context = compizconfig.Context()

plugins = sorted(context.Plugins.keys())
parser = argparse.ArgumentParser(description=_("enable/disable compiz plugins"),
                                 epilog=_("Available plugins: {}").format(plugins))
parser.add_argument("plugin", type=str, help=_('Name of plugin to control'))
parser.add_argument("action", type=str, choices=['enable', 'disable'],
                    help=_("What to do with the plugin"))

args = parser.parse_args()


if args.plugin not in context.Plugins.keys():
    raise SystemExit(_("Plugin {} doesn't exist").format(args.plugin))

if args.action == 'enable':
    state = 1
else:
    state = 0

print(_("Setting {plugin} enabled status to {status}").format(plugin=args.plugin,
                                                           status=state))
context.Plugins[args.plugin].Enabled = state
time.sleep(3)
print(_("New status is {status}").format(status=context.Plugins[args.plugin].Enabled))
