#!/usr/bin/python3 -s
#
#    Copyright (c) 2009-2020 Tom Keffer <tkeffer@gmail.com>
#
#    See the file LICENSE.txt for your rights.
#
"""Configure the configuration file."""
from __future__ import absolute_import
import sys
import optparse

from weecfg.config import ConfigEngine, Logger

usage = """Usage: wee_config --help
       wee_config --version
       wee_config --list-drivers
       wee_config --reconfigure CONFIG_FILE|--config=CONFIG_FILE
           [--driver=DRIVER]  [--units=(us|metric)]
           [--latitude=yy.y] [--longitude=xx.x] [--altitude=zz.z,(foot|meter)] 
           [--location="Home Sweet Home"] [--register=(y,n)]
           [--output=OUT_CONFIG] [--no-prompt] [--no-backup] [--verbosity=N]
       wee_config --install --dist-config=DIST_CONFIG --output=OUT_CONFIG
           [--driver=DRIVER]  [--units=(us|metric)]
           [--latitude=yy.y] [--longitude=xx.x] [--altitude=zz.z,(foot|meter)] 
           [--location="Home Sweet Home"] [--register=(y,n)]
           [--no-prompt] [--no-backup] [--verbosity=N]
       wee_config --upgrade CONFIG_FILE|--config=CONFIG_FILE
           --dist-config=DIST_CONFIG
           [--output=OUT_CONFIG] [--no-prompt] [--no-backup]
           [--warn-on-error] [--verbosity=N]

User actions:

--version       Show the WeeWX version, then exit.  
--list-drivers  List the available weewx device drivers, then exit.
--reconfigure   Modify an existing configuration file CONFIG_FILE with any
                specified station parameters.  Use this command with the
                --driver option to change the device driver.
                
System actions (not normally done by users):

--install       Install a new configuration file starting with the contents of
                DIST_CONFIG, prompting for station parameters.
--upgrade       Update the contents of configuration file CONFIG_FILE to the
                installed version, then merge the result with the contents of
                configuration file DIST_CONFIG.

Station parameters:

  --driver      --units
  --latitude    --longitude
  --altitude    --location
  --register
"""


def main():

    # Create a command line parser:
    parser = optparse.OptionParser(usage=usage)
    
    # Add the various options:
    parser.add_option("--version", action="store_true",
                      help="Show the weewx version then exit.")
    parser.add_option("--list-drivers", action="store_true",
                      help="List the available device drivers.")
    parser.add_option("--reconfigure", action="store_true",
                      help="Reconfigure an existing configuration file.")
    parser.add_option("--install", action="store_true",
                      help="Install a new configuration file.")
    parser.add_option("--upgrade", action="store_true",
                      help="Update an existing configuration file, then merge "
                      "with contents of DIST_CONFIG.")
    parser.add_option("--config", dest="config_path", metavar="CONFIG_FILE",
                      help="Use configuration file CONFIG_FILE.")
    parser.add_option("--dist-config",
                      help="Use template configuration file DIST_CONFIG.")
    parser.add_option("--output", metavar="OUT_CONFIG",
                      help="Save to configuration file OUT_CONFIG.  If not "
                      "specified then replace existing configuration file.")
    parser.add_option("--driver", metavar="DRIVER",
                      help="Use the driver DRIVER. "
                      "For example, weewx.drivers.vantage")
    parser.add_option("--latitude", metavar="yy.y",
                      help="The station latitude in decimal degrees.")
    parser.add_option("--longitude", metavar="xx.x",
                      help="The station longitude in decimal degrees.")
    parser.add_option("--altitude", metavar="zz,(foot|meter)",
                      help="The station altitude in either feet or meters."
                      " For example, '750,foot' or '320,meter'")
    parser.add_option("--location",
                      help="""A text description of the station."""
                      """ For example, "Santa's workshop, North Pole" """)
    parser.add_option("--units", choices=["us", "metric"], metavar="(metric|us)",
                      help="Set display units to 'metric' or 'us'.")
    parser.add_option("--register", dest='register_this_station', choices=['y', 'n'],
                      metavar="(y/n)",
                      help="Register this station in the weewx registry?")
    parser.add_option("--no-prompt", action="store_true",
                      help="Do not prompt. Use default or specified values.")
    parser.add_option("--no-backup", action="store_true", default=False,
                      help="When replacing an existing configuration file, "
                      "do not create a backup copy.")
    parser.add_option("--warn-on-error", action="store_true", default=False,
                      help="Only warn if an update is not possible.  Default "
                      "behavior is to warn then exit.")
    parser.add_option("--debug", action="store_true",
                      help="Show diagnostic information while running.")
    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_mgr = ConfigEngine(logger=Logger(verbosity=options.verbosity))

    config_mgr.run(args, options)

    sys.exit(0)


if __name__ == "__main__" :
    main()
