#!/usr/bin/env python
#
#    Copyright (c) 2009-2015 Tom Keffer <tkeffer@gmail.com> and
#                            Matthew Wall
#
#    See the file LICENSE.txt for your rights.
#
"""Install and remove extensions."""
import optparse
import sys

import weecfg.extension
from weecfg import Logger
from weecfg.extension import ExtensionEngine

# Redirect the import of setup:
sys.modules['setup'] = weecfg.extension

usage = """wee_extension --help
       wee_extension --list
           [CONFIG_FILE|--config=CONFIG_FILE]
       wee_extension --install=(filename|directory)
           [CONFIG_FILE|--config=CONFIG_FILE]
           [--tmpdir==DIR] [--dry-run] [--verbosity=N]
       wee_extension --uninstall=EXTENSION
           [CONFIG_FILE|--config=CONFIG_FILE]
           [--verbosity=N]

Install, list, and uninstall extensions to weewx.

Commands:

--list:      Show installed extensions.
--install:   Install the specified extension.
--uninstall: Uninstall the specified extension."""

def main():
    parser = optparse.OptionParser(usage=usage)
    parser.add_option('--list', action="store_true", dest="list_extensions", 
                      help="Show installed extensions.")
    parser.add_option('--install', metavar="FILENAME|DIRECTORY",
                      help="Install the extension contained in FILENAME or"
                      " DIRECTORY. FILENAME must be an archive that contains"
                      " a packaged extension such as pmon.tar.gz.  DIRECTORY"
                      " is the result of extracting that archive.")
    parser.add_option('--uninstall', metavar="EXTENSION",
                      help="Uninstall the extension with name EXTENSION.")
    parser.add_option("--config", metavar="CONFIG_FILE",
                      help="Use configuration file CONFIG_FILE.")
    parser.add_option('--tmpdir', default='/var/tmp',
                      metavar="DIR", help='Use temporary directory DIR.')
    parser.add_option('--bin-root', metavar="BIN_ROOT",
                      help="Look in BIN_ROOT for weewx executables.")
    parser.add_option('--dry-run', action='store_true',
                      help='Print what would happen but do not do it.')
    parser.add_option('--verbosity', type=int, default=1,
                      metavar="N", help='How much status to display, 0-3')
    
    # Now we are ready to parse the command line:
    (options, _args) = parser.parse_args()

    config_path, config_dict = weecfg.read_config(options.config, _args)
    
    ext = ExtensionEngine(config_path=config_path,
                          config_dict=config_dict,
                          tmpdir=options.tmpdir,
                          bin_root=options.bin_root,
                          dry_run=options.dry_run,
                          logger=Logger(verbosity=options.verbosity))
    
    if options.list_extensions:
        ext.enumerate_extensions()
        
    if options.install:
        ext.install_extension(options.install)
        
    if options.uninstall:
        ext.uninstall_extension(options.uninstall)

    return 0

if __name__=="__main__" :
    main()
    
